[Spring Security] Security Filter 의 종류

| Security Filter

  • Spring Security의 동작은 사실상 Filter로 동작한다고 해도 무방하다.
  • 각 Filter 들은 각자 다른 기능을 하고 있다.
  • 이런 Filter들은 유기적으로 제외, 추가 및 순서를 정할 수 있다.
  • 필터의 종류는 많지만 가장 많이 쓰이는 필터는 아래와 같다
    • SecurityContextPersistenceFilter
    • BasicAuthenticationFilter
    • CsrfFilter
    • RememberMeAuthenticationFilter
    • AnonymousAuthenticationFilter
    • FilterSecurityInterceptor
    • ExceptionTranslationFilter

1. SecurityContextPersistenceFilter 역할

  • SecurityContextPersistenceFilter는 세션에 저장된 SecurityContext가 있다면 그걸 가져다가 넣어주고 없다면 새로 만들어주는 필터이다.
  • 세션은 JSESSIONID를 통해서 로그인을 매번하지 않아도 서비스를 이용할 수 있게 해준다.

2. BasicAuthenticationFilter

  • BasicAuthenticationFilter 적용하면 따로 로그인이라는 과정을 하지 않아도 일회성으로 페이지를 불러올 수 있었다.
  • 이처럼 우리가 로그인 과정없이도 username/password 라는 로그인 데이터를 Base64로 인코딩해서 모든 요청에 포함해서 보내면 BasicAuthenticationFilter는 이걸 인증한다.
  • 그렇기 때문에 세션이 필요없고 요청이 올때마다 인증이 이루어진다. 즉, stateless 하다.
  • stateless는 상태를 저장하지 않는다는 것이다. 다시말해 매번 요청마다 다시 인증을 한다는 의미이다.
  • BasicAuthenticationFilter를 사용하지 않을 것이라면 명시적으로 disable 시켜 주는 것이 좋다
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // basic authentication
        http.httpBasic(); // basic authentication filter  활성화
        http.httpBasic().disable(); // basic authentication filter 비활성화
        // csrf
        http.csrf();
        // remember-me
        http.rememberMe();
        // authorization
        http.authorizeRequests()
                // /와 /home은 모두에게 허용
                .antMatchers("/", "/home", "/signup").permitAll()
                // hello 페이지는 USER 롤을 가진 유저에게만 허용
                .antMatchers("/note").hasRole("USER")
                .antMatchers("/admin").hasRole("ADMIN")
                .antMatchers(HttpMethod.POST, "/notice").hasRole("ADMIN")
                .antMatchers(HttpMethod.DELETE, "/notice").hasRole("ADMIN")
                .anyRequest().authenticated();
        // login
        http.formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/")
                .permitAll(); // 모두 허용
        // logout
        http.logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/");
    }

댓글

Designed by JB FACTORY