Study/Java Spring Boot

Security추가 후 Post 요청 403에러 해결

kahaha 2023. 3. 11. 23:01

Security 설정후 잘 작동하던 PostMapping 컨트롤러가 작동하지 않았다.

 

CSRF() 로인해서 post 요청이 막히는것.

        http.csrf().disable();

SecurityConfig.java 의 SecurityFilterChain을 상속받은 메서드에 위 코드를 추가해주면 해결된다.

 

@Configuration // 설정값으로 사용
public class SecurityConfig {

    @Bean // SecurityAdapter상속 대신 사용 /최신  // 시큐리티 기본 로그인화면 탈출하기위함
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .csrf().disable() // CSRF() 로인해서 post 요청이 막히는것을 해제해줌 / post 403에러 해결
                .authorizeHttpRequests(auth -> auth //auth를 받아서 어떤 리퀘스트를 열것인지
                        .antMatchers(HttpMethod.POST, "/articles").permitAll() //Role메서드 추가예정
                        .anyRequest().permitAll()) //auth를 받아서 어떤리퀘스트든 다열겠다
                .formLogin().and() //폼로그인 사용
                .build();
    }

Security설정관련 메서드 초반부 작성내용