본문 바로가기
프로그래밍/Spring

spring-boot-starter-security 403 Forbidden 에러 대처

by Mr-후 2019. 3. 26.
반응형

spring-boot-starter-security 403 Forbidden 에러 대처



이건 순전히 어제 밤에 내가 아무것도 모른채 스프링을 하겠다고 덤볐다가 맨땅에 제대로 헤딩한 이슈라 포스팅에 올려 둔다. 


클라이언트 로그인 화면에서  /login Controller로 요청을 보내면 자꾸만 403 Forbidden에러가 발생되었다. 브라우저 URL입력창에서 해당 URL을 입력하면 잘 들어가지는데 왜 form을 submit하면 안되는걸까? 를 가지고 긴 시간 고민 끝에 sprint-boot의 security에 뭔가 있을 것 같다는 생각이 들었다. 

아침에 출근을 해서 보니 클라이언트 웹 파트에도 동일한 문제가 발생되어 확인을 해 본 결과 다음과 같은 코드를 통해 해결을 할 수 있었다. 


먼저 프로젝트 구성은 다음과 같다. 

1. pom.xml안에 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-security</artifactId>

</dependency>

가 추가되어 있고 


import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;

import org.springframework.security.config.annotation.web.builders.WebSecurity;

import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {


@Override

public void configure(WebSecurity web) throws Exception {

web.ignoring().antMatchers("/css/**", "/script/**", "image/**");

}

@Override

protected void configure(HttpSecurity http) throws Exception {

/**

            * 로그인 disable 처리

            */

security.httpBasic().disable();

}

}


처리가 되어 있었는데 이 상태에서 form을 submit시키면 403 Forbidden에러가 나오는 상황. 

해결된 코드는 

@Override

protected void configure(HttpSecurity http) throws Exception {

/**

    * 로그인 disable 처리

    */

//security.httpBasic().disable();

http.cors().and();

http.csrf().disable();

}


와 같이 http.cors().and().http.csrf().disabled() 가 추가 되었다. 

이로서 제대로 작동하는 API 및 웹 서비스 페이지가 되었다. 값진 경험이었고 많은 부분 팀내에서도 신뢰가 쌓인 느낌이다. 




반응형