HttpSecurity vs WebSecurity
SpringBuilder는 웹 보안을 구성하는 빌더 클래스로서 웹 보안을 구성하는 빈 객체와 설정 클래스들을 생성하는 역할을 한다.
이 빌더의 종류로는 HttpSecurity와 WebSecurity가 있다. 오늘은 이 둘에 대해서 알아보려고 한다.
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring()
.antMatchers("/health", "/health/**")
.antMatchers("/publics/**");
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/health", "/health/**").hasRole('USER') // 무시된다.
.anyRequest().authenticated();
}
WebSecurity는 HttpSecurity의 상위에 있다. WebSecurity의 ignoring에 endpoint를 만들면, Security Filter Chain이 적용되지 않는다.
이 경우 Cross-Site Scripting, XSS 공격, content-sniffing에 취약해진다.
HttpSecurity의 permitAll에 endpoint를 지정하면 인증처리의 결과를 무시하지만 Security Filter Chain이 적용되어 Cross-Site Scripting, XSS 공격, content-sniffing에 대한 검사를 할 수 있다.
위의 예제의 경우 WebSecurity에 ignoring에 설정된 /publics/** 엔드포인트에 대하여 HttpSecurity 부분은 동작하지 않는다.
실무에서 HttpSecurity의 permitAll이 설정된 endpoint에 잘못된 BearerToken 값이 들어오면 Security Filter Chain를 거치면서 에러를 반환하지만, WebSecurity는 ignoring이 설정된 잘못된 BearerToken 값이 들어와도 통과된다.
그래서 WebSecurity는 보안과 전혀 상관없는 로그인 페이지, 공개 페이지(어플리캐이션 소개 페이지 등), 어플리캐이션의 health 체크를 하기위한 API에 사용하고, 그 이외에는 HttpSecurity를 사용하는 것이 좋다.
References
SpringBoot : Security Configuration using HTTPSecurity vs WebSecurity - https://ravthiru.medium.com/springboot-security-configuration-using-httpsecurity-vs-websecurity-1a7ec6a23273