개발자 키우기

resources 자원 필터 거치지 않게 하기 본문

Back-end/Security

resources 자원 필터 거치지 않게 하기

개발자 키우기 2023. 8. 9. 15:16

resources에는 보통 static ( css / js / images ) 와 같이 인증이나 인가와는 상관없는 자원들이다

 

그렇기 때문에 필터를 거치지 않도록 설정해서 Anonymous 사용자이든 Authentication 사용자든 모두 사용하도록 

 

설정해 보자


첫 번째

 

SecurityFilterChain에서 permitAll() 설정

 

해당 설정은 결국 필터체인을 거치기 때문에 추천하지 않는다

 

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.antMatchers("/", "/css", "/js", "images").permitAll()
.anyRequest().authenticated()
)
.formLogin();
return http.build();
}

두 번째

 

web.ignoring() 설정

 

해당 설정은 필터체인을 거치지 않기 때문에 첫 번째 방법보다 성능상 뛰어나다 ( 이걸 사용하자 )

 

만약 리소스에서 필요한 자원만 설정하고 싶다면 antMathcers를 사용하여 디테일하게 설정 할 수 도 있다

 

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return web -> web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}

 

application을 동작시키면 아래와 같은 로그를 확인 할 수 있다

 

2023-08-10 16:38:52.487  WARN 9384 --- [           main] o.s.s.c.a.web.builders.WebSecurity       : You are asking Spring Security to ignore org.springframework.boot.autoconfigure.security.servlet.StaticResourceRequest$StaticResourceRequestMatcher@116efe65. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

 

위의 경고와 같이 StaticResourceRequestMatcher는 권장하지 않는 방식이니 antMathcers를 사용하여

 

permitAll로 설정하자

'Back-end > Security' 카테고리의 다른 글

SecurityFilterChain hasRole 설정  (0) 2023.08.09
대칭키와 비대칭키 메커니즘  (0) 2023.05.19