발단
spring security 를 적용하여 web 의 static resource 에 대한 접근시 권한과 무관하게끔 처리 하기 위한 설정으로 ignoring 을 사용하였는데 제목과 같은 문구가 발생하여 이를 제거하며 최적화한 과정을 정리합니다.
환경
- Spring Boot 3.1 <= ..
- Spring Security 6.1 <= ..
# Spring Boot 3.1 버전 이상의 환경에서의 적용을 설명합니다.
문제 내용
@Bean
fun webSecurityCustomizer(): WebSecurityCustomizer {
return WebSecurityCustomizer { web: WebSecurity ->
web.ignoring().requestMatchers("/api/v1/test1")
}
}
- 위 코드는 Web 과 관련된 처리를 Spring 측에서 인터페이스로 제공하고 있어 사용하였습니다.
- Spring Boot 2 버전에서 사용하던 방식으로 Spring Boot 3 로 마이그레이션 하는 과정에서 그대로 사용하는 방식중 하나였습니다.
Spring Security without the WebSecurityConfigurerAdapter
In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter, as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common
spring.io
- 링크에서 위 코드에 대한 설명이 작성되어있습니다.
문제 검색
WARN when ignoring antMatchers - please use permitAll · Issue #10938 · spring-projects/spring-security
When I use web.ignoring().antMatchers() I'd like to see a DEBUG message instead of a WARNING for each ignored pattern. I'm confused by the message saying it's not recommended and I should use permi...
github.com
- 문구로 검색시 위와 같은 github 문서를 찾아볼 수 있었습니다.
- 간단히 설명해서 아래 내용과 같습니다.
ignoring 사용시 spring security 가 지원하는 보안적 처리가 진행되지 않으니, permitAll 을 사용하는 것이 좋습니다.
이전에는 ignoring 사용시 문구가 없었는데 왜 이제 발생하게 되는걸까?
- 문제 검색 링크를 살펴보면 ignoring 을 성능 최적화의 방법중 하나로 소개하며 하나의 이슈를 언급하고 있습니다.
- 해당 이슈에서 세션 사용시 모든 요청에 대해 spring security 를 참조하게 되고 이는 성능적 저하가 발생한다고 하고 있습니다.
- 즉, 버전이 상승하며 이전에 사용했던 방식보다 나은 방식이 있다고 가르쳐주려 문구가 출력되고 있는 것 입니다.
문제 해결
시도1: `문제 검색` 링크의 제안으로 처리
- `문제 검색`의 링크에서는 이런 문구와 함께 해결책을 제시하고 있습니다.
`#10913` 이슈가 해결되는 동안에 spring security 를 사용하면서 성능적 이슈를 해결하고 싶다면 제안해준 코드로 처리하는 게 좋다. 이 방식은 세션으로부터 security context 에 대한 접근 없이 static resource 에 대한 보안을 유지한다.
- `#10913` 이슈는 spring security 5.8.1 버전에서 처리되었습니다. 그런데 글 초기에 언급하였던 제 환경에서는 `spring security 6.1 <= ..` 을 사용하고 있음으로 `문제 검색`에서 언급한 방식으로 해결할 수 없습니다.
시도2: Spring Security 공식 문서 (6.1.10 - SNAPSHOT, Favor of permitAll over ignoring)
Authorize HttpServletRequests :: Spring Security
While using a concrete AuthorizationManager is recommended, there are some cases where an expression is necessary, like with or with JSP Taglibs. For that reason, this section will focus on examples from those domains. Given that, let’s cover Spring Secu
docs.spring.io
- ignoring 사용보보다 permitAll 을 선호하자 라는 소제목으로 문서에서 설명하고 있습니다. 이는 Spring Security 6.1 이상의 문서에서 다루고 있으며 제가 사용하고자 하는 버전과 호한됨으로 이 시도를 통해 해결할 수 있었습니다.
해결 코드
/**
* 설명
* - default security filter chain
* - static resource 를 처리
* */
@Bean
fun resources(http: HttpSecurity): SecurityFilterChain {
return http
.authorizeHttpRequests { httpReq ->
httpReq
.requestMatchers("/path/to/resource/**").permitAll()
.requestMatchers(*arrayOf("/path/to/resource/1", "/path/to/resource/2")).permitAll()
}
.build()
}
/**
* 설명
* - second security filter chain
* - user 경로에 대해 개별 권한 처리
* */
@Bean
@Order(1)
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
return http
.securityMatcher (*arrayOf("/user/**"))
.authorizeHttpRequests { httpReq ->
httpReq
.requestMatchers("/user/test1").permitAll()
.requestMatchers("/user/test2").authenticated()
.requestMatchers("/user/test3").hasRole("USER")
.anyRequest().permitAll()
}
.build()
}