WebIgnore
Client 의 Request 후 Server 의 SpringSecurity 작동
- js / css / image 파일 등의 정적자원에 대해 SpringSecurity는 보안을 검사하고 있음
- 사실상 정적 자원은 보안필터를 거칠 필요가 없는 경우가 많음
- 즉, 보안을 적용하지 않는 리소스를 설정하는 방법이다!
어떻게 설정하는가.
스프링 시큐리티 설정클래스에서 재정의
- configure(WebSecurity web)
web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
// 정적자원에 대한 경로 요청경로에 대해서 보안을 하지 않는다.
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
의존(학습시 사용한 것.)
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
타임리프에서 지원하는 SpringSecurity 문법, 태그에 대한 확장팩
객체 간의 값을 복사하여 다른 객체에 맵핑시켜주는 라이브러리
<dependency>
<groupId>org.modelmapper</groupId> <!--객체간의 값을 복사하여 매핑해주는 라이브러리 -->
<artifactId>modelmapper</artifactId>
<version>2.3.0</version>
</dependency>
전체적인 설정클래스 구조
- configure(WebSecurity web) 메서드의 재정의 부분, 정적자원에 대한 인증과정 생략
- configure(AuthenticationManagerBuilder auth) 선언적 방법, 메모리 방식의 방법 사용
- PasswordEncoder 빈을 지정 (PasswordEncorderFactories > DelegatingPasswordEncoder) 스프링 시큐리티 암호화 과정에서 사용 됨
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
String password = passwordEncoder().encode("1111");
auth.inMemoryAuthentication().withUser("user").password(password).roles("USER");
auth.inMemoryAuthentication().withUser("manager").password(password).roles("MANAGER","USER");
auth.inMemoryAuthentication().withUser("admin").password(password).roles("ADMIN","MANAGER","USER");
}
@Bean
public PasswordEncoder passwordEncoder(){
return PasswordEncoderFactories.createDelegatingPasswordEncoder();//암호화 인코더
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/mypage").hasRole("USER")
.antMatchers("/messages").hasRole("MANAGER")
.antMatchers("/config").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin();
}
}
permitAll 과 ignoring 은 어떤 차이인가.
permitAll 은 비슷하게 자격이없어도 정적자원에 대한 접근을 허가 한다.
- 차이점은 permitAll() 은 보안필터를 거친다.
- ignoring() 은 보안필터를 거치지 않는다.
즉, 마지막에 위치한 FilterSecurityInterceptor 에서 정적자원에 대한 검사를 하느냐 마느냐 의 차이라고 봐야겠다.
'springframework > 시작하자SpringSecurity' 카테고리의 다른 글
26.인증 프로세스 구현, CutonUserDetailsService (0) | 2020.09.27 |
---|---|
26.PasswordEncoder (0) | 2020.09.27 |
24.SpringSecurity 필터, 아키텍쳐 정리 (0) | 2020.09.23 |
23.AccessDesicionManager, AccessDecisionVotor (0) | 2020.09.22 |
22.Authorization, FilterSecurityInterceptor (0) | 2020.09.22 |