TIL

2024.02.03 spring security Authorization Manager Method Security

Gisungcu 2024. 2. 3. 14:37

5.6 버전 이전에는 Secured와 PreAuthorize를 동시에 사용할 수 없었지만 5.6 이후에는 동시에 사용할 수 있게 되었음.

 

이유는 다음과 같다.

5.6이전에는 MethodSecurityInterceptor가 해당 메소드의 권한을 검증했음.

https://www.inflearn.com/course/%EC%BD%94%EC%96%B4-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0/dashboard

검증 방식을 가져오는 것을  DelegatingMethodSecurityMetadataSource에게 위임한다.

DelegatingMethodSecurityMetadataSource는 호출한 메소드가 어떤 권한 검증을 해야하는지를 캐시하고 있는데  가장 첫번째 검증 방식을 캐시한다.

DelegatingMethodSecurityMetadataSource

해당 attribute(검증방식)은 계속 반환되어 interceptor까지 가게되어서 PreInvocationAuthorizationAdviceVoter가 투표하는데 사용된다.

AbstractSecurityInterceptor

근데 단점이 있는데 DelegatingMethodSecurityMetadataSource가 캐시하는 것을 보면 map형태로 저장하고 있다는 것이다.

즉 하나의 메소드는 하나의 검증 방법만 사용할 수 있다. 예로 스프링 컨텍스트에서 사용되는 Secured와 PreAuthorize는 같이 사용될 수 없다는 것이다.

 

관련 이슈

 

SEC-1888: Mixing @Secured and @PreAuthorize annotations on a secured object causes one or the other to be silently ignored · Is

Richard Bradley (Migrated from SEC-1888) said: If I secure an object with a mixture of @PreAuthorize and @Secured annotations, then one or the other is silently ignored: @PreAuthorize("isAuthentica...

github.com

 

다만 이것은 5.6 버전 이상에서는 구조가 변경되어 가능해졌다.

과거 구조가 method 실행전에 PreInvocationAuthorizationAdviceVoter가 하나의 권한 검증 조건을 사용했다면 이제는 

 

Secured와 PreAuthorize가 ~~beforeMethodInterceptor를 상속받아서 각자의 검증 방법을 다 통과해야 메소드를 실행시킬 수 있게되었다.

 

 

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

해당 구조로 바뀌다보니 Secured와 PreAuthorize를 동시에 사용할 수 있게 되었다.

 

AuthorizationManagerBeforeMethodInterceptor

 

https://www.baeldung.com/spring-enablemethodsecurity 

 

 

Spring Security Reference

In Spring Security 3.0, the codebase was sub-divided into separate jars which more clearly separate different functionality areas and third-party dependencies. If you use Maven to build your project, these are the modules you should add to your pom.xml. Ev

docs.spring.io