스프링 시큐리티는 다양한 인증 방법을 제공하는데 그중 가장 기본인 form을 이용한 로그인 방식이다.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.formLogin() //폼로그인 방식 사용
.loginPage("/login") //로그인 페이지 설정
.loginProcessingUrl("/login_proc") //로그인 Form Action url
.usernameParameter("username")//아이디 파라미터
.passwordParameter("password")//패스워드 파라미터
.successHandler(authenticationSuccessHandler)//로그인 성공시 핸들러
.failureHandler(authenticationFailureHandler);//로그인 실패시 핸들러
return http.build();
}
}
폼로그인 방식을 사용하면 UserPasswordAuthenticationFilter가 활성화 된다
1.Client에게 요청이 들어오면 UserPasswordAuthenticationFilter
가 Servlet전에 요청을 가로챈다
2.로그인 폼데이터 전송할때 url인지 확인(다른 요청이면 이 필터는 pass)
3.Username,password를 이용하여 AuthenticationToken
생성 후 AutenticaitonManager
에게 인증 요청
4.AutenticationManager
는 AuthenticationProvider
(폼 인증을 처리하는 Provider)에게 인증 처리를 넘긴다
5.인증에 실패시 예외처리/인증에 성공시 user의 정보가 들어가있는 객체와 권한정보를 넣은 AuthenticationToekn
를 반환
6.이 정보를 SecurityContext
(인증객체를 보관하는 저장소)에 저장
이 과정을 거치면 쿠키를 통해 JSESSIONID가 Client에게 넘어간다. 로그인 유지는 SESSIONID를 통해 SecurityContext로 direct로 확인한다.