728x90
FilterInterceptor 필터에서 해당 자원에 접근할 자격이 있는지를 최종 판단한다.
1. 인증이 없는 사용자가 접근 시 인증을 다시 받도록 동작시킨다 -> AjaxLoginUrlAuthenticationEntryPoint
2. 권한이 없는 사용자가 접근시 권한이 없다는 예외를 발생시킨다. ->AjaxAccessDeniedHandler
AjaxLoginUrlAuthenticationEntryPoint
- ExceptionTranslationFilter에서 인증 예외 시 호출
- AuthenticationEntryPoint 인터페이스 구현
- 인증 오류 메시와 401 상태 코드 반환
- response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
-
public class AjaxLoginUrlAuthenticationEntryPoint implements AuthenticationEntryPoint { //인증이 안된 사용자가 접근 @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { //클라이언트가 인증이 안된 상태로 접근 -> 401에러 response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "unAuthorized"); } } 
익명사용자 접근
AjaxAccessDeniedHandler
- 특정한 권한이 있는 사용자만 접근하도록 해주는 클래스
- ExceptionTranslationFilter에서 인가 예외 시 호출
- AccessDeniedHandler 인터페이스 구현
- 인가 오류 메시지와 403 상태 코드 반환
- response.sendError(HttpServletResponse. SC_FORBIDDEN, “forbidden");
public class AjaxAccessDeniedHandler implements AccessDeniedHandler { //권한 에러 @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access is denied"); } }
권한 없는 사용자 접근

@Bean
public AccessDeniedHandler ajaxAccessDeniedHandler() {
return new AjaxAccessDeniedHandler();
}728x90
'스프링 시큐리티 > 실전프로젝트 - 인증 프로세스 Ajax 인증 구현' 카테고리의 다른 글
| ch07. Ajax 로그인 구현 & CSRF 설정 (0) | 2022.01.21 |
|---|---|
| ch06. Ajax Custom DSLs 구현하기 (0) | 2022.01.21 |
| ch04. 인증 핸들러 - AjaxAuthenticationSuccessHandler, AjaxAuthenticationFailureHandler (0) | 2022.01.20 |
| ch03. 인증 처리자 - AjaxAuthenticationProvider (0) | 2022.01.20 |
| ch02. 인증 필터 - AjaxAuthenticationFilter (0) | 2022.01.20 |