이 포스트는 정수원님의 스프링 시큐리티 강의 수강 후 정리한 글입니다.

http.formLogin() // formLogin 형태로
.loginPage("/loginPage") // "/login"을 다음 url로 연결하고
.defaultSuccessUrl("/") // 인증 성공 시 root page로 이동
.failureUrl("/login") // 인증 실패 시 다시 login 페이지로 이동
.usernameParameter("userId") // usernameParameter를 userId로
.passwordParameter("passwd") // passwordParameter를 passwd로
.loginProcessingUrl("/login_proc") // loginprocessingUrl를 login_proc로 커스텀 * AntPathRquestMatcher의 매칭을 바꾸는거임
.successHandler(new AuthenticationSuccessHandler() { // 성공시 authenticationSuccessHandler를 작동시켜서
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
System.out.println("authentication : " + authentication.getName()); // authentication의 이름을 출력
response.sendRedirect("/"); // root url로 응답
}
})
.failureHandler(new AuthenticationFailureHandler() { // 실패 시 authenticationFailurehandler를 작동시켜서
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
System.out.println("exception" + exception.getMessage()); // 에러 문구와 함께
response.sendRedirect("/login"); // 다시 로그인페이지로 응답
}
})
.permitAll() // 모든 사용자가 인증 없이 이용 가능
SecurityContext에 Authentication이 저장되기 때문에 다음 코드를 작성하면 인증 객체를 꺼내 쓸 수 있다.
SecurityContextHolder.getContext.getAuthentication();
정리가 잘 된 글이네요. 도움이 됐습니다.