[오류/해결] Spring Security 도중 css나 정적파일이 제대로 적용되지 않는 현상

이신영·2023년 5월 31일
0

오류 모음집

목록 보기
15/24
post-thumbnail

상황 설명

public class SecurityConfig {

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/user/**").permitAll()
                .antMatchers("/calendar/event/add", "/calendar/event/delete/**","/inquiry/answer/**").hasRole("ADMIN")
                .anyRequest().authenticated()// 이 부분을 추가함
		...

홈페이지에서 비로그인상태면 로그인화면으로 넘기려고 .anyRequest().authenticated()를 추가함 근데..

이런식으로 CSS랑 image가 적용되지않게 나옴
아마 권한문제라서 static이 담긴 resources의 위치인

.antMatchers("/resources/**").permitAll()를 추가해보았는데 안되길래
.antMatchers("/resources/**", "/static/**").permitAll() 으로 변경해봐도 되질않는다.


원인

Spring Security 설정에서 정적 리소스 경로에 대한 접근 권한을 명시적으로 허용하지 않았기 때문에 CSS와 이미지 파일이 로드되지 않은 문제.


문제 해결

.antMatchers("/css/**", "/images/**", "/js/**", "/fullcalendar-6.1.5/**").permitAll() 으로 명시적으로 리소스 접근권한을 허용하는 로직 변경

여기서 아무 게시판이나 링크를 누르면?

라고 정상적으로 작동함


후기

내 문제의 대부분이 경로, 변수명, 데이터타입인걸로보아 아직도 갈길이 멀었다고 느낀다..

profile
후회하지 않는 사람이 되자 🔥

0개의 댓글