Spring/Security

스프링 시큐리티 - 1

훈지런 2022. 12. 2. 18:25

FormLogin 인증 필터

graph TB A((user)) --> |Request| B[UsernamePasswordAuthenticationFilter] -->|요청정보가 매칭되는지 확인| C{AntPathRequestMatcher} --> |NO| D[chain.doFilter] C{AntPathRequestMatcher} --> |YES| E[Authentication] --> |인증| F{AuthenticationManager} --> |위임| G[AuthenticationProvider] --> |인증성공| F G --> |인증실패| H[AuthenticationException] --> B F --> I[Authentication] --> J[SecurityContext]
  1. UsernamePasswordAuthenticationFilter에서 처리함.
  2. AntPathRequestMatcher()에서 요청정보가 매칭되는지 확인. (Default : /login) 불일치시 필터로 넘어감
  3. Authentication 객체를 만들어서 유저 정보를 보관하고 AuthenticationManager에게 인증 맡김
  4. AuthenticationManager로부터 AuthenticationProvider
  5. Authentication에 인증정보와 권한정보를 받음
  6. SecurityContext에 내용 저장

LogoutFilter

로그아웃은 원칙적으로(Default) POST방식으로 처리가 됨.

  • SecurityContextLogoutHandler가 세션 무효화, 쿠키삭제, 시큐리티 컨텍스트를 삭제함.
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .logout()
        //...
        .deleteCookies("쿠키명")    // 로그아웃할때 쿠키도 삭제됨
    return http.build();
}

Remember Me 인증

세션이 만료되고 웹 브라우저가 종료되어 어플리케이션이 사용자를 기억하는 기능

쿠키를 확인하고 토큰기반의 인증을 사용해서 유효성 검사

jSessionId를 삭제해도(나중에 다시 접속해도) 인증정보를 유지함.
remember-me쿠키를 확인해서 유저계정을 확인해서 그 계정을 통해서 인증을 시도함.`

  • 기본 파라미터명 = remember-me
  • 기본 쿠키 만료시간 = 14일

userDetailService(클래스)는 시스템에 있는 사용자 계정을 조회하는 과정

@Autowired
private UserDetailService userDetailService;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .rememberMe()
        // ...
        userDetailService(userDetailService);
    return http.build();
}
Remember Me 인증 필터

RememberMeAuthenticationFilter가 사용자 요청을 처리하는 조건

  1. 인증객체가 null 인 경우 :
    • 인증받은 사용자는 SecurutyContext안에 인증정보가 존재
    • 사용자 세션이 만료되었거나 세션이 끊켜서 세션안에서 SecurutyContext를 찾지못하는 경우
    • null이 아닌경우는 인증이 되어있다는 뜻
  2. 발급받은 Remember Me 쿠키를 헤더에담아두고 접속할때

RememberMeAuthenticationFilter인터페이스 구현체

  • TokenBasedRememberMeServices : 메모리에있는 저장한 내용을 비교해서 인증처리
  • PersistentTokenBaseRememberMeServices : DB에 저장하고 DB에 저장한 값과 비교해서 인증처리

'Spring > Security' 카테고리의 다른 글

스프링 시큐리티 - 3  (0) 2022.12.07
스프링 시큐리티 - 2  (0) 2022.12.05