출처
사용자의 권한 목록 반환 함수
public interface Authentication extends Principal, Serializable {
Collection<? extends GrantedAuthority> getAuthorities();
}
String getAuthority();
Spring Security
는 SimpleGrantedAuthority
라는 GrantedAuthority
구현체를 갖고있다.GrantedAuthority
로 변환될 수 있게 해준다.AuthenticationProvider
인스턴스들은 Authentication
객체에 SimpleGrantedAuthority
를 추가한다.ROLE_
을 포함한다.즉, security context
가 "USER"의 역할을 필요로하는 인가 규칙이 있다면, Spring Security
는 기본적으로 ROLE_USER
를 반환하는 GrantedAuthority#getAuthority
를 찾는다는 것이다.
이러한 기본 설정은 변경할 수 있는데, GrantedAuthorityDefaults
를 사용하여 이를 사용자 정의할 수 있다.
// MethodSecurityExpressionHandler
@Bean
static GrantedAuthorityDefaults grantedAuthorityDefaults() {
return new GrantedAuthorityDefaults("MYPREFIX_");
}
Tip
Spring Security
에서 변경된GrantedAuthorityDefaults
를 적용하기 위해서는,@Configuration
클래스보다GrantedAuthorityDefaults
가 먼저 빈으로 등록되어야하는데,static
키워드는 해당 메서드가@Configuration
클래스 보다 먼저 등록되는 것을 보장하기 위해 사용된다.
Spring Security
는 메서드 호출이나 웹 요청과 같은 secure objects에 대한 접근을 제어하는 interceptor
들을 제공한다.AuthorizationManager
인스턴스에 의해 인가 작업이 이루어진다.XX 버전부터 AuthorizationMager
가 AccessDecisionManager
와 AccessDecisionVoter
를 대체한다.
AuthorizationManager
는 Spring Security
의 request-based, method-based 및 message-based 인증 컴포넌트들에 의해 호출되며 최종 접근 제어 결정을 내리는 역할을 한다.
AuthorizationManager
인터페이스는 아래 2개의 메서드들을 포함한다
AuthorizationDecision check(Supplier<Authentication> authentication, Object secureObject);
default AuthorizationDecision verify(Supplier<Authentication> authentication, Object secureObject)
throws AccessDeniedException {
// ...
}
positive(granted 가 true인) AuthorizationDecision
반환negative(granted 가 false인) AuthorizationDecision
반환null AuthorizationDecision
반환negative AuthorizationDecision
이면throw AccessDeniedException
개발자는 인가의 모든 측면을 제어하기 위해 커스텀 AuthorizationManager
를 구현할 수 있다.
하지만, SpringSecurity
는 각각의 AuthorizationManager
들과 이들과 협력할 수 있는 delegating(위임형) AuthorizationManager
를 함께 제공한다.
AuthorizationManager
구현체
이들을 활용하여, 각 AuthorizationManager
구현체들의 조합이 인가 결정에 대한 투표를 수행할 수 있도록 한다. (?)
AuthorityAuthorizationManager
는 Spring Security
에서 제공되는 가장 일반적인 AuthorizationManager
이다.Authentication
이 지정된 권한들 중 하나라도 포함하고 있다면, positive AuthorizationDecision
을 반환한다.negative AuthorizationDecision
을 반환한다.anonymous
, fully-authenticated
와 remember-me authenticated users
를 구분하는데 사용될 수 있다.AuthorizationManager
를 더 정교한 표현으로 구성하기 위해 도움이 되는 static factory method 존재한다.authenticated()
: 인증된 모든 사용자만 접근할 수 있도록 설정할 때 사용됩니다.permitAll()
: 모든 사용자(인증되지 않은 사용자 포함)가 접근할 수 있도록 허용합니다.denyAll()
: 모든 요청을 거부합니다. → 특정 리소스에 대해 접근을 차단할 때 사용됩니다.hasRole(String role)
: 사용자가 특정 역할(ROLE_)을 가지고 있는지 확인합니다.hasAuthority(String authority)
: 사용자가 특정 권한을 가지고 있는지 확인합니다.anyOf()
: 전달된 여러 AuthorizationManager
중 하나만 만족하면 접근을 허용합니다.allOf()
: 전달된 모든 AuthorizationManager
조건을 만족해야 접근을 허용합니다.custom(Predicate)
: 커스텀 로직을 기반으로 동작하는 인가 매니저를 정의할 수 있습니다.AuthorizationManager
를 구현하여, 원하는 어떤 접근 제어 로직도 구현할 수 있다.Open Policy Agent
또는 소유하고있는 인가 데이터베이스를 쿼리할 수 있는 구현체를 생성할 수 있다.