Spring

[Spring] Oauth 연동 - Security6.x 정리

obin01 2025. 1. 15. 21:26

1. Filter

용도 : 요청과 응답에 대해 전역적으로 처리할 작업에 주로 사용, 요청시 헤더를 검사해 인증 토큰 여부 및 검증하는데 사용

동작 : DispatcherServlet 외부에서 요청이 전달되기 전과 후에 FilterChain을 통해 동작

메소드

init(), doFilter(), destroy()

  • init() - filter가 생성될 때 수행되는 메소드
  • doFilter() - 요청, 응답이 filter를 거칠때 수행되는 메소드, 파라미터로는 request, response, filterChain이 있음
  • destroy() - filter가 사라질때 수행되는 메소드

 

2. Spring Security

용도 : 인증과 인가에 대한 부분을 Filter의 흐름에 따라 처리하는데 사용

동작과정

  1. 사용자 요청
  2. AuthenticationFilter가 요청을 받아서 UsernamePasswordAuthenticationToken토큰을 생성
  3. Authentication Manager에게 토큰을 전달
  4. 실제 인증처리 하는 Authentication Provider에게 토큰을 전달
  5. UserDetailsService의 loadUserByUsername메소드 실행하여 파라미터인 인증용 객체로 로그인 정보 조회
  6. AuthenticationProvider 인터페이스에서 해당 로그인 정보가 맞는지 확인
  7. 성공시 AuthenticationSuccessHandler, 실패시 AuthenticationFailureHandler 실행

📌 UserDetails

DB에서 불러올때 UserDetails 인터페이스를 상속받아서 사용

 

3. Spring Filter Chain

동작과정

1. SecurityContextPersistenceFilter : 주로 세션에 저장된 정보를 사용하거나 요청마다 새로운 컨텍스트를 생성

2. HeaderWriterFilter : 보안 관련 HTTP 헤더를 설정하여 응답의 보안성을 강화

3. CsrfFilter : CSRF 토큰이 포함되어 있는지 검증

4. LogoutFilter : 로그아웃 URL에 대한 요청을 처리, 현재 세션의 인증 정보를 제거

5. UsernamePasswordAuthenticationFilter : usernamepassword 를 기반으로 인증 요청 처리, 성공하면 Authentication 객체를 생성하고 SecurityContext에 저장

6. DefaultLoginPageGeneratingFilter : 스프링 시큐리티의 기본 로그인 폼을 자동으로 생성

7. DefaultLogoutPageGeneratingFilter : 기본 로그아웃 페이지 제공

8. BearerTokenAuthenticationFilter : HTTP 요청 헤더에서 Bearer 토큰을 추출하고 검증, JWT 또는 OAuth2 액세스 토큰 인증

9. BasicAuthenticationFilter : HTTP Basic 인증 처리, 성공시 인증 정보를 추출하여 SecurityContext에 저장

10. RequestCacheAwareFilter : 인증이 필요한 요청이 인증되지 않은 상태에서 시도되었을 때 해당 요청을 캐시하고 인증 후 원래 요청으로 리다이렉트 처리

11. SecurityContextHolderAwareRequestFilter : 보안 컨텍스트와 요청 객체 연동

12. AnonymousAuthenticationFilter : 인증되지 않은 요청에 대해 익명 사용자 Authentication 객체를 생성하고 접근 가능하게 설정

13. SessionManagementFilter : 세션의 유효성을 확인, 세션을 무효화, 동시 세션 제어나 세션 고정 보호 같은 기능 제공

14. ExceptionTranslationFilter : 인증 또는 권한 문제로 발생하는 예외를 처리

15. FilterSecurityInterceptor : 요청 URL, HTTP 메서드, 사용자 권한을 기반으로 접근 권한 확인

 

4. 바뀐점

📌 Spring boot 3.x 부터 security가 6.x 이상으로 적용

1. WebSecurityConfigurerAdapter 대신에 SecurityFilterChain 사용

2. antMatchers(), mvcMatchers(), regexMatchers() 대신에 requestMatchers() 사용

3. requestMatchers() 에서 AntPathRequestMatcher를 명시적으로 사용하는 대신, 문자열을 직접 전달하는 방식으로 사용

4. antMatcher(),  requestMatcher() 대신에 securityMatcher() 사용

5. configure로 WebSecurityConfigurerAdapter 상속받아 ignore처리하던 부분을 WebSecurityCustomizer 으로 사용

6. formLogin() 에서 로그인 페이지 경로와 로그인 처리 경로의 분리

7. AuthenticationManager를 AuthenticationConfiguration에서 가져와 설정하는 방식이 HttpSecurity를 통해 직접 설정하는것으로 변경

 

 

 

 

 

참고

https://docs.spring.io/spring-security/reference/5.8/migration/servlet/config.html?utm_source=chatgpt.com

 

Configuration Migrations :: Spring Security

In 6.0, @Configuration is removed from @EnableWebSecurity, @EnableMethodSecurity, @EnableGlobalMethodSecurity, and @EnableGlobalAuthentication. To prepare for this, wherever you are using one of these annotations, you may need to add @Configuration. For ex

docs.spring.io

https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/form.html?utm_source=chatgpt.com

 

Form Login :: Spring Security

When the username and password are submitted, the UsernamePasswordAuthenticationFilter creates a UsernamePasswordAuthenticationToken which is a type of Authentication, by extracting the username and password from the HttpServletRequest instance. The Userna

docs.spring.io

https://upsw-p.tistory.com/57

 

Spring Security와 Spring Security의 Filter를 알아보자!

Spring Security Spring Security의 이론이 무엇인가요! 스프링 시큐리티를 이용하면 개발시 피룡한 사용자의 인증, 권한, 보안 처리를 간단하지만 강력하게 구현할 수 있습니다. 일반적인 웹 환경에서

upsw-p.tistory.com