티스토리 뷰
Spring에서 테스트를 돌리는 중 Unauthorized(401)
가 아닌 Forbidden(403)
이 발생하는 문제가 생겼다.
@RestController
@RequiredArgsConstructor
public class PostController {
private final PostService postService;
@PreAuthorize("isAuthenticated()")
@PostMapping("/api/bbs/{name}/posts")
public void post(@PathVariable String name,
@Valid @RequestBody WritePost request,
@AuthenticationPrincipal ExternalUser externalUser) {
postService.write(name, externalUser.getId(), request);
}
}
위는 게시물을 작성할때 로그인 한 사용자만 가능하도록 한 기능이다.
로그인하지 않은 사용자가 접근하면 아래 Security Config안에 authenticationEntryPoint
에서 정의 한 대로 Unauthorize관련 Exception이 발생해야 한다.
@EnableWebSecurity
public class ClientSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// 생략..
.and()
.exceptionHandling()
.accessDeniedHandler(dogpigAccessDeniedHandler())
.authenticationEntryPoint(dogpigEntryPoint())
// 생략...
}
}
@RequiredArgsConstructor
public class DogpigEntryPoint implements AuthenticationEntryPoint {
private final ObjectMapper objectMapper;
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
String json = DogpigResponse.builder()
.message("로그인이 필요합니다.")
.httpStatus(UNAUTHORIZED)
.build()
.toJson(objectMapper);
response.setContentType(APPLICATION_JSON_UTF8.toString());
response.setStatus(UNAUTHORIZED.value());
response.getWriter().write(json);
}
}
곰곰히 쌀국수를 먹으며 생각했다.
스프링에서 '로그인하지 않은 사용자'가 아닌 '익명 사용자'로 판단하기 때문에 Forbidden이 발생한게 아닐까..?
그래서 Security 설정에 아래와 같이 anonymous를 off하도록 했다.
http.authorizeRequests()
.anonymous().disable()
... 생략
그러고 나니 정상적으로 Unauthorize Exception이 잘 작동했다.
'개발 > Java,Spring' 카테고리의 다른 글
우아한 테크세미나 - 스프링 배치 (2) | 2019.09.27 |
---|---|
SpringSecurity에서 anonymous() (2) | 2019.08.15 |
Spring Boot 2.x Security에서 hasPermission 사용 (0) | 2019.08.11 |
SpringBoot Batch에서 JobParameter로 받을 수 있는 Type (4) | 2019.05.17 |
공지사항
- Total
- 26,801
- Today
- 49
- Yesterday
- 96
링크
TAG
- spring
- 우아한형제들
- splitChunks
- disable
- upstream
- hasPermission
- Spring Security
- 우아한테크세미나
- 오누마당고
- 유효하지 않은 변수
- unzip
- 에키벤
- 슈퍼호쿠토
- nginx
- spring batch
- installed date
- Illegal byte sequence
- 홋카이도
- isAuthorized
- 강대명
- 개발자
- job parameter
- Method Security
- 미나미 치토세
- PreAuthorize
- 스프링배치
- forbidden
- innobackupex
- xbstream
- 설치일자