@Bean
SecurityFilterChain configure(HttpSecurity http, AuthenticationProvider authenticationProvider) throws Exception {
http.httpBasic(Customizer.withDefaults());
http.authenticationProvider(authenticationProvider);
http.authorizeHttpRequests(c->
c.requestMatchers(HttpMethod.POST,"/user").permitAll().anyRequest().authenticated()
);
return http.build();
}
이렇게 엔드포인트에 대해서 인증을 하지 않도록 설정을 했는데도
포스트맨에서 api를 호출할 때 계속 401 에러가 떴다.
구글링을 해보니 에러(예외)가 터졌을 때도 스프링 시큐리티의 인증요청 때문에 막혔던 거 같다.
@Bean
SecurityFilterChain configure(HttpSecurity http, AuthenticationProvider authenticationProvider) throws Exception {
http.httpBasic(Customizer.withDefaults());
http.authenticationProvider(authenticationProvider);
http.authorizeHttpRequests(c->
c.requestMatchers(HttpMethod.POST,"/user").permitAll().
requestMatchers("/error").permitAll().anyRequest().authenticated()
);
return http.build();
}
이렇게 해주니 포스트맨에서
이런 결과가 나왔다. 애초에 막혔던 거구나...
근데 왜 forbidden이지 싶었는데 찾아보니 csrf 때문인거같다.
우선, 스프링시큐리티인액션 책에 이걸 다루는 부분이 있어서 그 때 정리를 하겠다. csrf를 비활성화해주니 정상 작동했다!
참고로 포스트맨에 인증 처리할 떄는
이렇게 한다.