Spring Boot/Spring Security

[Spring Security] Form Login 인증 필터

수수한개발자 2022. 6. 29.
728x90

UsernamePasswordQuthenticationFilter

UsernamePasswordAuthenticationFilter는 사용자 form 인증처리를 하는 필터입니다.

인증 전에는 정보를 담아서 매니저에게 넘기는 역할을 하고 인증이 성공한 후에는 시큐리티 콘텍스트에 넘기는 역할을 합니다.

사용자가 요청을하면 UsernamePasswordAuthenticationFilter가 인증처리를 합니다. 사용자가 인증을 시도하면 필터가 요청 URL을 확인을 합니다.

현재 요청이 AntPathRequestMatcher를 통해 /login인지 확인을 합니다. 매칭이 되면 로그인 처리를 하는 것이고 아니면 필터로 이동을 합니다. 참고로 이 url은 사용자 정의 보안 기능을 구현할 때 loginProcessingUrl("/login")에 설정하면 바꿀 수 있습니다.

UsernamePasswordAuthenticationFilter 가 하는 가장 큰 역할은 Authentication 객체를 만들어 유저정보를 넣고 인증 처리를 맡기는 것입니다.

이 정보다 일치하면 Authentication 으로가서 유저네임과 패스워드를 담아서 인증해서 넘긴다.

Authenticationmanager는 내부적으로 AuthenticationProvider로 위임을 한다.

AuthenticationProvider가 인증 처리를 담당하는 클래스입니다.

인증 실패와 성공을 리턴하는데 실패하면 AuthenticationException으로 해서 처음으로 (UsernamePasswordAuthenticationFilter)가 받아 처리하게 됩니다.

인증이 성공하면 Authentication 객체를 만들어  매니저로 리턴한다.

그리고 Authenticationmanager는 AuthenticationProvider로부터 받은 객체(Authentication)를를 필터로 넘긴다.

여기서 이 객체는 User객체와 Authorities(권한 정보)를 담은 객체이다.

그리고 UsernamePasswordAuthenticationFilter는 SecurityContext객체에 저장한다.

SecurityContext는 인증 객체를 저장하는 저장소이다.

SecurityContext가 세션에도 저장됩니다..

그래서 SuccessHandler에서 성공 이후의 작업들을 처리합니다.

 

SecurityContextHolder.getContext(). getAuthentication()을 하면 전역적으로 인증 객체를 꺼내서 사용할 수 있습니다.

728x90

댓글