인증/인가 예외발생시 처리
인증이란?
- 사용자가 해당 사이트에 등록이 되어있는지
- 사용자의 로그인 정보가 있는지 확인
인가란?
- 인증이 완료된 사용자가 해당 url에 접근할 권한이 있는지
- 예를 들어 USER가 ADMIN의 관리페이지에 접근할 수 없는 것에 대한 확인
스프링 시큐리티에 인증 인가에 대한 처리(*스프링 시큐리티는 인증 먼저 확인 하고 인가를 확인한다)
http
.authenticationEntryPoint(jwtEntryPoint) // 인증실패시 처리
.accessDeniedHandler(jwtAuthorityHandler) // 인가실패시 처리
인증에 대한 처리
@Slf4j
@Component
public class JwtEntryPoint implements AuthenticationEntryPoint{
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
log.error("Unauthorized error 인증 되지 않은, 가입되지 않은 사용자 접근 : {}", authException.getMessage());
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error: Unauthorized");
}
}
인가에 대한 처리
@Slf4j
@Component
public class JwtAuthorityHandler implements AccessDeniedHandler{
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.error("권한이 없는 사용자 로그인 : {}", accessDeniedException.getMessage());
response.sendError(response.SC_FORBIDDEN, "Error: forbidden");
}
}
'Spring' 카테고리의 다른 글
[SpringBoot] SendGrid 사용법 (0) | 2023.03.07 |
---|---|
[Spring] Springboot 프로젝트 생성 (0) | 2023.01.23 |
[SpringBoot] yml 파일 여러개 사용하기 (0) | 2022.12.05 |
[SpringBoot] SQS 의존성 에러 (0) | 2022.12.04 |
[Redis] redis로 token값 저장하기(2) (0) | 2022.12.01 |