BackEnd/Security · JPA
[ Security ] 01. 시큐리티 사용하기 - WebSecurityConfigurerAdapter 권한 설정 (스프링 부트 버전 2.7.0이전)
jimin-log
2023. 5. 23. 13:22
시큐리티 설정 파일 - WebSecurityConfigurerAdapter
1. Security 권한 설정
Security의 권한은 config에서 설정해줄수 있다.
스프링 부트 버전 2.7.0 이전에는 ' WebSecurityConfigurerAdapter ' 를 상속받아서 할 수 있다.
1) WebSecurityConfigurerAdapter를 상속 받는 클래스 생성.
WebSecurityConfigurerAdapter : SecurityFilterChain
- 필터 체인은 반드시 하나 이상이고 url 패턴에 따라 적용되는 필터 체인을 다르게 할 수 있다.
- 본래의 메인 필터를 반드시 통과해야만 서블릿에 들어갈 수 있는 단점을 보완 하기위해서 필터 체인 proxy를 두었다고 할수 있다.
- web resource의 경우 패턴을 따르더라도 필터를 무시하고 통과시켜주기도 한다.
💡두개 이상의 필터 체인을 구성하려면?
- WebSecurityConfiguererAdapte를 상속 받는 클래스를 추가로 생성한다.
- 두개의 필터 체인에 순서를 정해줘야한다. @Order(1)
필터 종류 (이외 OAuth2 나 Saml2, Cas, X509 등에 관한 필터 ... ) - disable()로 제거 할 수 도 있다. | |
HeaderWriterFilter | Http 해더를 검사한다. 써야 할 건 잘 써있는지, 필요한 해더를 더해줘야 할 건 없는가? |
CorsFilter | 허가된 사이트나 클라이언트의 요청인가? |
CsrfFilter | 내가 내보낸 리소스에서 올라온 요청인가? |
LogoutFilter | 지금 로그아웃하겠다고 하는건가? |
UsernamePasswordAuthenticationFilter | username / password 로 로그인을 하려고 하는가? 만약 로그인이면 여기서 처리하고 가야 할 페이지로 보내 줄께. |
ConcurrentSessionFilter | 여거저기서 로그인 하는걸 허용할 것인가? |
BearerTokenAuthenticationFilter | Authorization 해더에 Bearer 토큰이 오면 인증 처리 해줄께. |
BasicAuthenticationFilter | Authorization 해더에 Basic 토큰을 주면 검사해서 인증처리 해줄께. |
RequestCacheAwareFilter | 방금 요청한 request 이력이 다음에 필요할 수 있으니 캐시에 담아놓을께. |
SecurityContextHolderAwareRequestFilter | 보안 관련 Servlet 3 스펙을 지원하기 위한 필터라고 한다.(?) |
RememberMeAuthenticationFilter | 아직 Authentication 인증이 안된 경우라면 RememberMe 쿠키를 검사해서 인증 처리해줄께 |
AnonymousAuthenticationFilter | 아직도 인증이 안되었으면 너는 Anonymous 사용자야 |
SessionManagementFilter | 서버에서 지정한 세션정책을 검사할께. |
ExcpetionTranslationFilter | 나 이후에 인증이나 권한 예외가 발생하면 내가 잡아서 처리해 줄께. |
FilterSecurityInterceptor | 여기까지 살아서 왔다면 인증이 있다는 거니, 니가 들어가려고 하는 request 에 들어갈 자격이 있는지 그리고 리턴한 결과를 너에게 보내줘도 되는건지 마지막으로 내가 점검해 줄께. |
📑 SecurityConfig.java
package com.sp.fc.web.config;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Order(1)
@EnableWebSecurity(debug = true) //어떤 필터를 타고 있는지 확인가능
@EnableGlobalMethodSecurity(prePostEnabled = true) //prePostEnabled로 권한체크
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}