Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- spring
- JPA
- server
- 어노테이션
- HTTP
- Static
- 상태코드
- 쿼리
- 스프링
- 스웨거
- restAPI
- 스프링오류
- application.yml
- Swagger
- Java
- RDBMS
- API
- 인텔리제이오류
- 시큐리티
- SQL
- SpringSecurity
- 자바
- 스프링시큐리티
- HTTP상태코드
- 오버라이딩
- MariaDB
- JWT
- 의존성주입
- 스프링RESTAPI
- 서버
Archives
- Today
- Total
취뽀몽
[Spring Error] 회원가입 403 Forbidden, Spring Security 본문
@RestController
@RequiredArgsConstructor
public class SignupController {
private final UserService userService;
@PostMapping("/users/new-user") // 회원가입
public ApiResponse<SignupResponse> join(@RequestBody SignupRequest request) {
return ApiResponse.success(SuccessStatus.SIGNUP_SUCCESS, userService.create(request));
}
}
회원가입 API를 짜고 포스트맨을 돌려봤는데,
403 포비든 오류가 떴다.
403 Forbidden은 클라이언트가 서버에 요청을 보냈지만, 서버가 요청을 거부하는 경우다. 클라이언트가 요청한 리소스에 대한 권한이 없거나 서버가 클라이언트의 요청을 이해했지만 권한이 없는 경우에 발생한다.
회원가입에서는 권한이 필요한 부분이 없는데 왜 403 Forbidden이 발생했는지 SecurityConfig 파일을 살펴봤다.
Spring Security를 사용하기 위해서는 SecurityConfig를 설정해줘야하는데 나는 다음과 같이 설정해뒀다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/signup").permitAll() // 회원가입은 인증하지 않은 모든 사용자 허용
.antMatchers("/login").permitAll() // 로그인은 인증하지 않은 모든 사용자 허용
.anyRequest().authenticated() // 그 외의 요청은 인증된 사용자만 접근 가능
.and()
.addFilterBefore(new JwtFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
분명 회원가입은 인증하지 않은 유저도 허용되게 해놨는데 왜 403 Forbidden이 떴지? 라고 생각하는 찰나,,
컨트롤러의 회원가입 주소는 /users/new-user인데 SecurityConfig에는 /signup으로 해둔 것이 눈에 들어왔다.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/users/new-user").permitAll() // 회원가입은 인증하지 않은 모든 사용자 허용
.antMatchers("/login").permitAll() // 로그인은 인증하지 않은 모든 사용자 허용
.anyRequest().authenticated() // 그 외의 요청은 인증된 사용자만 접근 가능
.and()
.addFilterBefore(new JwtFilter(tokenProvider), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
바로 위와 같이 변경해주고 다시 포스트맨을 돌려봤다.
잘 돌아간다! 그럼 데이터베이스에도 저장이 될까?
잘 저장되는 것을 확인할 수 있다.
정말 간단한 문제라 포스팅을 쓸지 말지 고민했는데 아직 Spring Security에 익숙하지 않은 부분이 많아서 남겨두기로 했다.
저와 같이 회원가입에 403오류가 뜬다면 SecurityConfig 파일을 확인해보시길... 은근
http.csrf().disable()
이 부분에 disable() 처리를 안 해줘서 오류가 발생하는 분들도 많은 것 같다.
꼭 확인해보시길!