카테고리 없음
[스프링 시큐리티] 기본 API & Filter 이해(2) 폼 인증방식
코딩하는 백구
2022. 12. 30. 20:43
🌿 폼 인증 방식
프로세스
- 서버 자원은 인증된 사용자만 사용할 수 있기 때문에 인증이 안되면 로그인 페이지로 리다이렉트 된다.
- username&password 를 입력하여 다시 인증을 시도한다.
- Spring Security 가 세션 ID 를 생성하고 이 세션에 인증 결과, 내용을 담은 토큰을 생성, 저장한다.
- Authentication 객체에 저장한다.
- 인증을 받은 이후 다시 서버 자원에 접근을 시도하면
- Spring Security 가 현재 사용자가 가진 세션으로 부터 인증 토큰의 존재 여부를 판단한다.
- 사용자는 해당 세션으로 계속 자원에 접근하게 된다.
폼 로그인 인증 API
http.formLogin()
.loginPage("/lgoinPage") // 사용자 정의 로그인 페이지
.defaultSuccessUrl("/home") // 로그인 성공 후 이동 페이지
.failureUrl("/login.html?error=true") // 로그인 실패 후 이동 페이지
.usernameParameter("username") // 아이디 파라미터명 설정 (UI 인증 요청 시, 파라미터명)
.passwordParameter("password") // 패스워드 파라미터명 설정 (UI 인증 요청 시, 파라미터명)
.loginProcessingUrl("/login") // 로그인 Form Action Url (UI 인증 요청 시, form 속성)
// 로그인 성공 후 핸들러
.successHandler(new AuthenticationSuccess() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// authentication : 인증 결과가 담겨 있음
log.debug("authentication: " + authentication.getName();
response.sendRedirect("/");
}
})
// 로그인 실패 후 핸들러
.failureHandler(new AuthenticationFailureHandler() {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
log.error("exception: " + exception.getMessage());
response.sendRedirect("/loginPage");
}
})
.permitAll() // /loginPage 로 접근하는 사용자는 인증을 받지 않아도 된다.