springframework/시작하자SpringSecurity

25.정적 자원 관리 - WebIgnore 설정

Jungsoomin :) 2020. 9. 27. 20:43

WebIgnore

 

Client 의 Request 후 Server 의 SpringSecurity 작동

  1. js / css / image 파일 등의 정적자원에 대해 SpringSecurity보안을 검사하고 있음
  2. 사실상 정적 자원보안필터를 거칠 필요가 없는 경우가 많음
  3. 즉,  보안을 적용하지 않는 리소스를 설정하는 방법이다!

어떻게 설정하는가.

 

스프링 시큐리티 설정클래스에서 재정의

  • 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 에서 정적자원에 대한 검사를 하느냐 마느냐 의 차이라고 봐야겠다.