스프링 부트에서 static 내부 css 경로가 안먹힐 때

Posted by sJun on November 16, 2020 ·

Spring Security를 사용하고 있는 경우에 분명히 static 내부 css 폴더안에 css 파일을 넣고 경로 지정을 했는데도 적용이 안되는 경우가 있다.

SecurityConfig 파일에 다음과 같은 권한을 추가하면 된다.

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/css/**").permitAll() // 이부분
                .anyRequest().authenticated();

        http
                .formLogin()
                .loginPage("/signIn")
                .permitAll();
    }
}

나도 똑같은 시행 착오를 겪어서 내 코드를 똑같이 긁어왔다.

나머지 코드는 전혀 상관없고

.antMatchers로 “/css/**” 경로에 대해서 permitAll을 이용해 이 경로에 대해서는 인증을 요구 하지 않도록 조정한다.


Security를 사용하는 경우가 아니라면 다음 링크를 참조

여기


  • 11/19 추가

antMtchers를 이용해도 되지만 더 좋은 방법이 있는 걸 알았다.

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}

WebSecurity 매개변수를 받는 configure 메서드를 오버라이드 하면 된다.

코드를 보면 알겠지만 static 폴더 내부에 있는 자원에 대해서는 보안 필터를 적용하지 않겠다는 뜻이다.

이 메서드만 오버라이딩 해주면 static 폴더 내부의 js / css / image 파일 등.. 리소스에 대한 접근이 가능해진다.