접근불가 페이지 만들기
Spring Security 권한 설정 중 어노테이션에서 들어갈 값은 constant해야 함
@Secured(value = UserRoleEnum.Authority.ADMIN)//constant해야 함
@GetMapping("/api/admin/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
}
Enum형식:
public enum UserRoleEnum {
USER(Authority.USER), // 사용자 권한
ADMIN(Authority.ADMIN); // 관리자 권한
private final String authority;
UserRoleEnum(String authority) {
this.authority = authority;
}
public String getAuthority() {
return this.authority;
}
public static class Authority {
public static final String USER = "ROLE_USER";
public static final String ADMIN = "ROLE_ADMIN";
}
}
UserDetailsImpl 권한부여:
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
UserRoleEnum userRole = user.getRole();
String authority = userRole.getAuthority();
//SimpleGrantedAutority의 규칙이 ROLE_어쩌구 임
SimpleGrantedAuthority simpleAuthority = new SimpleGrantedAuthority(authority);
Collection<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(simpleAuthority);
return authorities;
}
강제로그인 처리:
// 4. 강제 로그인 처리
UserDetails userDetails = new UserDetailsImpl(kakaoUser);
Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
'TIL' 카테고리의 다른 글
2022.08.03.TIL (0) | 2022.08.04 |
---|---|
2022.08.02.TIL (0) | 2022.08.03 |
2022.07.30.TIL (0) | 2022.07.30 |
2022.07.29.TIL (0) | 2022.07.29 |
2022.07.28.TIL (0) | 2022.07.28 |