티스토리 뷰

웹에서 css, js 등의 정적파일을 서버로부터 가져오기 위해서는 스프링 시큐리티 설정을 통해 해당 요청을 허용해야 합니다.

 

해당 코드는 스프링 부트 3으로 작성한 코드입니다.

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    protected DefaultSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests((auth) ->
                auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                        .requestMatchers(HttpMethod.GET, "/").permitAll()
                        .anyRequest().authenticated()
        );

        return http.build();
    }

}

 

PathRequest.toStaticResources().atCommonLocations()를 이용하면 해결됩니다.