Spring security - Problem z weryfikacją roli

0

Hej, zacząłem raczkować w security i mam pewien problem posiadam klasę User:

package pl.saqie.SpringWebProject.app.user.model;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.Collection;
import java.util.Collections;

@Entity
@NoArgsConstructor
@Getter
@Setter
@ToString
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    @Enumerated(EnumType.STRING)
    private UserRole userRole;
    private boolean locked = false;
    private boolean enabled = true;


    public User(String firstName, String lastName, String email, String password, UserRole userRole) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.userRole = userRole;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(UserRole.USER.name());
        return Collections.singletonList(simpleGrantedAuthority);
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return email;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }
}

Oraz UserRole:

package pl.saqie.SpringWebProject.app.user.model;

public enum UserRole {
    USER,
    ADMIN
}

Chce w kontrolerze stworzyć endpoint tylko np. dla admina więc próbowałem tak:
.antMatchers(HttpMethod.GET, "/user/list").hasRole("ADMIN")
lecz bez skutku otrzymuje cały czas 403 Forbidden

spróbowałem również tak w kontrolerze:
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/list")
public String getListOfUsers(Model model){
model.addAttribute("users", userService.getAllUsers());
return "view/users";
}

również bez skutku otrzymuje 403 forbidden, dodam jeszcze że dodanie adntoacji @EnableGlobalMethodSecurity(prePostEnabled = true) w klasie konfigruacyjnej również nic nie zmienia

Mógłby ktoś coś poradzić bo już nawet nie mam pomysłów co może być nie tak :/

3

Trzeba by przedebugować. Na pierwszy rzut oka metoda getAuthorities() ma zahardkodowaną rolę USER.

1 użytkowników online, w tym zalogowanych: 0, gości: 1