본문 바로가기
개발/SpringSecurity

SpringSecurity 인증 기본 동작 및 설정

by 궁즉변 변즉통 통즉구 2021. 12. 14.
반응형

1. 로그인(인증) 성공 시

SpringSecurity를 이용해 Form인증을 할 경우 성공 시 org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler 가 동작하고 사용자가 처음 접근했던 페이지로 리다이렉트 시킴

 

사용자가 로그인 전 접속했던 페이지가 아닌 다른 페이지로 설정할 경우 defaultSeccessUrl 사용

http.formLogin().defaultSeccessUrl("/login-success")

로그인 성공 후 특정 URL을 호출하여 다른 로직을 한번 더 실행하고 싶은 경우 successForwardUrl 사용

http.formLogin().successForwardUrl("/login-success-handler");

 

로그인 성공 후 Controller가 아닌 분리된 로직 처리를 할 경우 successHandler 빈 등록해서 사용

http.formLogin().successHandler(loginSuccessHandler())

loginSuccessHandler 빈은 SimpleUrlAuthenticationSuccessHandler 또는 SavedRequestAwareAuthenticationSuccessHandler 상속받아서 구현

 

 

2. 로그인(인증) 실패 시

로그인(인증) 성공 시와 동일하게 failureForwardUrl(""), failureHandler(loginFailureHandler()) 사용해서 처리

loginFailureHandler 빈은 SimpleUrlAuthenticationFailureHandler 상속받아서 구현

 

3. 로그아웃 성공 시 

logoutSuccessHandler(logoutSuccessHandler()) 사용해서 처리 가능

logoutSuccessHandler 빈은 LogoutSuccessHandler 인터페이스 구현

http.formLogin().logout() 
    .logoutUrl("/logout") 
    .logoutSuccessHandler(logoutSuccessHandler())

 

 

4. 인증되지 않은 요청 처리

기본적으로 Form인증을 사용하는 경우 org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint가 설정되어 로그인 URL로 리다이렉트 처리

LoginUrlAuthenticationEntryPoint는 AuthenticationEntryPoint 인터페이스를 구현하고 있음


변경 시 authenticationEntryPoint(customAuthenticationEntryPoint()) 설정으로 처리

customAuthenticationEntryPoint 빈은 마찬가지로 AuthenticationEntryPoint 인터페이스 구현

http.exceptionHandling().authenticationEntryPoint(customAuthenticationEntryPoint())




반응형

댓글