[이슈해결] Spring Security 403 Forbidden 에러

MinSeong Kang·2022년 8월 25일
0

이슈해결

목록 보기
2/12

로그인 기능 구현을 위해 Spring Security를 설정하고, 로그인 요청시 다음과 같은 403 Forbidden 에러가 발생했다.

// Security 설정
@Bean
public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
            .formLogin()
            .loginPage("/admin/login")
            .successHandler(new LoginAuthHandler())

            .and()
            .rememberMe()
            .key("key")
            .tokenValiditySeconds(86400 * 30) // 1달
            .rememberMeParameter("remember-me")
            
            .and()
            .logout()
            .deleteCookies("JSESSIONID")
            .logoutRequestMatcher(new AntPathRequestMatcher("/admin/logout"))
            .logoutSuccessUrl("/admin/login")
            .invalidateHttpSession(true);
    return httpSecurity.build();

처음 에러를 맞이했을 때, 403 Forbidden 에러에 대한 해결방안에 대해서 알아보았다.

일반적인 403 Forbidden 에러

  • 403 Forbidden : 어떤 이유로 인해 특정 웹페이지에 엑세스할 수 없기 때문에 요청이 서버에 의해 거부되었음을 알려주기 위해 사용
  • 해결방안 : 페이지 새로고침, 주소 재확인, 브라우저 및 캐시 지우기

다음과 같은 해결방안 실제 발생한 에러를 해결할 수 없었으며, 실제 브라우저로 인한 에러는 아니라고 생각을 했다. 따라서 Spring Security을 통해 로그인 기능을 개발했기 때문에 Spring Security의 403 Forbidden 에러에 대해 찾아보니 바로 해결책을 찾을 수 있었다.


Spring Security 403 Forbidden 에러 해결

Spring Security는 기본적으로 csrf 공격에 대한 방지를 수행한다. 이를 해당 기능을 비활성화함으로써 403 Forbidden 에러는 해결할 수 있었다.

httpSecurity
	.csrf().disable()

하지만, Spring Security에서 제공하는 csrf 공격에 대한 방지 설정을 마음대로 비활성하는 것이 좋은 방법일까 라는 의문이 생겼다.

해당 내용에 대해서 찾아본 결과,
MVC가 세션과 쿠키를 통해 사용자 인증을 수행하기 때문에 csrf 공격에 매우 취약하다고 했다. 따라서 crsf 공격에 대한 방지를 disable 했더라면, 인터셉터 등에서 적절한 방어 코드를 통해 보안 수준을 높이는 것을 추천했다.

또한 쿠키나 세션에 의존적이지 않는 Rest API 경우는 쿠키대신 로컬 스토리지나 요청 헤더를 사용하거나, 세션 대신 JWT를 사용하기 때문에 crsf 공격에 대한 방지를 비활성하는 경우가 많다고 한다.

현재 진행하는 프로젝트는 MVC로 개발을 하고 있기 때문에 해당 내용을 바탕으로 crsf 공격에 대한 방지를 disable 하되, 적절한 방어 코드를 작성해야겠다.


참고문헌

https://stackoverflow.com/questions/19468209/spring-security-configuration-http-403-error
https://junhyunny.github.io/information/security/spring-boot/spring-security/cross-site-reqeust-forgery/

0개의 댓글