Spring Boot

[Spring Security] docs: 권한(Authorization) 부여 아키텍쳐 알아보기 (1)

kiritoni 2025. 2. 11. 19:53
반응형

GrantedAuthority 객체

  • `GrantedAuthority` 객체는 주체(Principal)에게 부여된 권한을 나타낸다.
  • `GrantedAuthority` 객체는 `AuthenticationManager`에 의해 `Authentication` 객체에 삽입된다.
  • 이후 권한 결정을 내릴 때, `AccessDecisionManager` 인스턴스가 이를 읽어 사용한다.

 

String getAuthority();

 

이 메서드는 `GrantedAuthority` 인터페이스의 메서드로, `AuthorizationManager` 인스턴스가

특정 `GrantedAuthority`의 문자열 표현을 얻는 데에 사용된다. 

이 문자열 표현을 반환하여 `AuthorizationManager` 구현체가 권한을 해석한다. 

 

SimpleGrantedAuthority

  • `GrantedAuthority`의 구현체이다. 
  • 사용자가 지정한 문자열을 `GrantedAuthority`로 변환시켜준다. 
  • 보안 아키텍쳐에 포함된 `AuthenticationProvider` 인스턴스는 `SimpleGrantedAuthority`를 사용하여 `Authentication` 객체를 채운다.
  • 기본적으로 역할 기반의 권한 부여 규칙에는 `ROLE_ `이라는 접두사가 포함된다. 
  • 예를 들어, Security Context에 "USER"권한이 필요하다는 권한 부여 규칙이 있다면, Spring Security 는 기본적으로 `GrantedAuthority#getAuthority`가 "ROLE_USER"를 반환하는지 확인한다.
  • 커스텀을 할 수도 있는데, `GrantedAuthorityDefaults`를 사용하여 이 규칙에서 사용할 접두사를 정의할 수 있다. 
@Bean
static GrantedAuthorityDefaults grantedAuthorityDefaults() {
	return new GrantedAuthorityDefaults("MYPREFIX_");
}

 

`GrantedAuthorityDefaults` 노출 및 초기화
Spring Security의 `@Configuration` 클래스들이 초기화되기 전에 Spring이 `GrantedAuthorityDefaults`를 먼저 게시하도록 하려면, static 메서드로 해당 빈을 노출해야 한다.

 

 

호출 처리 (Invocation Handling)

  • Spring Security는 보안 객체(메서드 호출이나 웹 보안 요청 등)에 대한 접근을 제어하는 인터셉터를 제공한다. 
  • 호출 전(pre-invocation)에는 `AuthorizationManager`가 인스턴스 호출을 진행할 수 있는지 결정해준다. 
  • 호출 후(post-invocation)에는  `AuthorizationManager`가 반환 값이 허용되는지 결정한다. 

 

 

AuthorizationManager (권장됨)

[Spring Security Docs 6.4.2 기준] 기존의 `AccessDecisionManager`와 `AccessDecisionVoter`를 `AuthorizationManager`로 변경하는 것이 권장된다.

 

AuthorizationManager의 역할

  • Spring Security의 요청 기반(request-based), 메서드 기반(method-based), 메시지 기반(message-based) 권한 부여 구성 요소들이 호출한다.
  • 최종적인 접근 제어 결정을 내리는 책임을 진다.
AuthorizationDecision check(Supplier<Authentication> authentication, Object secureObject);

default void verify(Supplier<Authentication> authentication, Object secureObject)
        throws AccessDeniedException {
    // ...
}

 

`check `메서드

  • 권한 결정을 내리는 데 필요한 모든 관련 정보를 전달받는다. 
  • `secureObject`를 받으면, 해당 호출 시 전달된 인자를 검사할 수 있다. 
  • 예를 들어, `secureObject`가 `MethodInvocation`인 경우, 고객(Customer) 인자를 확인하여 보안 로직을 구현할 수 있다. `AuthorizationManager`는 아래와 같은 권한 결정을 반환합니다:
  • 긍정적(Positive) `AuthorizationDecision`: 접근 허가
  • 부정적(Negative) `AuthorizationDecision`: 접근 거부
  • `null AuthorizationDecision`: 결정 보류

`verify` 메서드

  • `check` 메서드를 호출하고, 부정적인 권한 결정이 내려지면 `AccessDeniedException`을 발생시킨다.

 

위임 기반(Delegate-based) AuthorizationManager 구현

직접 `AuthorizationManager`를 구현해도 되지만,

Spring Security는 위임(delegating) 매커니즘을 제공한다. 

 

`RequestMatcherDelegatingAuthorizationManager`

  • 요청을 가장 적절한 위임 `AuthorizationManager`와 매칭한다.

메서드 보안용 인터셉터

  • `AuthorizationManagerBeforeMethodInterceptor`: 호출 권한 검사
  • `AuthorizationManagerAfterMethodInterceptor`: 호출 권한 검사

 

클래스 다이어그램

AuthorityAuthorizationManager

Spring Security에서 가장 일반적으로 사용되는 `AuthorizationManager`이다.
이 매니저는 현재 `Authentication` 객체에서 특정 권한(Authority)을 확인한다.

  • 현재 `Authentication`에 구성된 권한이 하나라도 포함되어 있으면 긍정적(Positive) 권한 결정을 반환한다.
  • 포함되지 않으면 부정적(Negative) 권한 결정을 반환한다.

 

AuthenticatedAuthorizationManager

다른 매니저로는 `AuthenticatedAuthorizationManager`가 있다.
이 매니저는 익명 사용자, 완전 인증된 사용자, 그리고 remember-me로 인증된 사용자를 구분하는 데 사용된다.

  • 예를 들어, 많은 사이트는 remember-me 인증 상태에서는 제한된 접근만 허용하고, 사용자가 완전한 접근 권한을 얻으려면 다시 로그인하여 신원을 확인하도록 요구한다.

AuthorizationManagers

Spring Security는 개별 AuthorizationManager를 더 정교한 권한 표현식으로 구성할 수 있는 정적 팩토리 메서드도 제공한다.
이를 통해 여러 권한 조건을 논리적으로 결합하거나 확장하여 복잡한 보안 정책을 수립할 수 있다.

커스텀 AuthorizationManager

커스텀 `AuthorizationManager`를 직접 구현할 수도 있다.

  • 이 구현은 애플리케이션에 특화된 비즈니스 로직이나 보안 관리 로직을 포함할 수 있다.
  • 예를 들어, Open Policy Agent(OPA)와 같은 외부 정책 시스템을 쿼리하거나, 자체 권한 데이터베이스를 조회하는 로직을 구현할 수 있다.

 

[출처]

https://docs.spring.io/spring-security/reference/servlet/authorization/architecture.html

 

Authorization Architecture :: Spring Security

It is a common requirement that a particular role in an application should automatically "include" other roles. For example, in an application which has the concept of an "admin" and a "user" role, you may want an admin to be able to do everything a normal

docs.spring.io

 

반응형