728x90
- 스프링 시큐리티 인가 설정 시 선언적 방식이 아닌 프로그래밍 방식으로 구현할 수 있으며 access(AuthorizationManager) API 를 사용
- access()에는 AuthorizationManager<RequestAuthorizationContext> 타입의 객체를 전달할 수 있으며 사용자의 요청에 대한 권한 검사를 access()에 지정한 AuthorizationManager가 처리
- access() 에 지정한 AuthorizationManager 객체는 RequestMatcherDelegatingAuthorizationManager의 매핑 속성에 저장
적용
- 특정한 엔드포인트에 대한 권한 검사를 수행하기 위해 AuthorizationManager를 구현하여 설정
- "/user", "/myPage", "/admin" 요청 패턴의 권한 검사는 AuthorityAuthorizationManager 가 처리
- "/api" 요청 패턴의 권한 검사는 CustomAuthorizationManager가 처리
CustomAuthorizationManager.java
public class CustomAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {
private static final String REQUIRED_ROLE = "ROLE_SECURE";
@Override
public AuthorizationDecision check(Supplier<Authentication> authentication, RequestAuthorizationContext object) {
Authentication auth = authentication.get();
// 인증 정보가 없거나 인증되지 않은 경우
if (auth == null || !auth.isAuthenticated()) {
return new AuthorizationDecision(false);
}
// "ROLE_SECURE" 권한을 가진 사용자인지 확인
boolean hasRequiredRole = auth.getAuthorities().stream()
.anyMatch(grantedAuthority -> REQUIRED_ROLE.equals(grantedAuthority.getAuthority()));
return new AuthorizationDecision(hasRequiredRole);
}
}
- 인증 객체가 null 이거나 인증되지 않은 경우 접근 거부
- boolean 변수에 인증 객체에 있는 권한 목록 중 하나라도 REQUIRED_ROLE과 일치한 지 확인
- boolean 변수를 AuthorizationDecision()에 넣어 반환
728x90
'Language > Spring Security' 카테고리의 다른 글
메서드 기반 인가 관리자 - PreAuthorizeAuthorizationManager 외 클래스 구조 이해 (0) | 2024.08.01 |
---|---|
인가 설정 응용 - RequestMatcherDelegatingAuthorizationManager (0) | 2024.08.01 |
요청 기반 인가 관리자 - AuthorityAuthorizationManager 외 클래스 구조 이해 (0) | 2024.08.01 |
인가 관리자 이해 - AuthorizationManager (0) | 2024.08.01 |
인가 - Authorization (0) | 2024.08.01 |