Spring Security

사전 개념

  1. 필터

    image.png

  2. Security 용어

  3. 세션 - 쿠키

    1. 유저가 로그인을 시도 (http request)
    2. AuthenticationFilter 에서부터 user DB까지 타고 들어감
    3. DB에 있는 유저라면 UserDetails로 꺼내서 유저의 session 생성
    4. spring security의 인메모리 세션저장소인 SecurityContextHolder 에 저장
    5. 유저에게 session ID와 함께 응답을 내려줌
    6. 이후 요청에서는 요청쿠키에서 JSESSIONID를 까봐서 검증 후 유효하면 Authentication를 쥐어준다.

Spring Security 내부 구조

image.png

// 객체 코드
SecurityContext context = SecurityContextHolder.getContext(); // Security Context
Authentication authentication = context.getAuthentication(); // authentication
authentication.getPrincipal();
authentication.getAuthorities();
authentication.getCredentials();
authentication.getDetails();
authentication.isAuthenticated();

Spring Security 기본 로그인 인증 처리 과정

image.png

로그인 인증 처리 순서 → http.formLogin() 을 설정

  1. 로그인 요청이 들어오면 AuthenticationFilter(UsernamePasswordAuthenticationFilter)를 거친다.