๐Ÿ’ Spring/Spring Security

8. JWT๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „ Filter ๋“ฑ๋ก ํ…Œ์ŠคํŠธ

iseunghan 2021. 11. 1. 13:32
๋ฐ˜์‘ํ˜•

์ด ํฌ์ŠคํŠธ๋Š” ๋ฐ์–ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ๋‹˜์˜ ์œ ํŠœ๋ธŒ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚˜์„œ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

 

 

Spring Security Filter ๊ตฌ์กฐ

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://atin.tistory.com/590

ํ•„ํ„ฐ๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์ „ SpringFilterChain ๊ตฌ์กฐ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์•Œ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.

 

Filter๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž

public class MyFilter1 implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("Filter 1");
        filterChain.doFilter(servletRequest, servletResponse);  // ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ๋„˜์–ด๊ฐ€๋ผ๋Š” ์˜๋ฏธ
    }
}
  • Servlet ์•„๋ž˜์— ์žˆ๋Š” Filter ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ํ•„ํ„ฐ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‹ค์‹œ ๋‹ค์Œ ํ•„ํ„ฐ๋กœ ๋„˜๊ฒจ์ฃผ๋Š” doFilter ํ˜ธ์ถœ์„ ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.

 

Filter ๋“ฑ๋ก

  • addFilter(Filter filter)
  • addFilterBefore(Filter filter, Class<? extends Filter> beforeFilter)
  • addFilterAfter(Filter filter, Class<? extends Filter> afterFilter)

 

addFilter

http.addFilter(new MyFilter1());

 

์—๋Ÿฌ ๋ฉ”์„ธ์ง€:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalArgumentException: The Filter class me.iseunghan.jwttutorial.filter.MyFilter1 does not have a registered order and cannot be added without a specified order. Consider using addFilterBefore or addFilterAfter instead.

MyFilter1์€ SpringSecurityFilterChain์— ๋“ฑ๋ก๋˜์ง€ ์•Š์•˜์œผ๋‹ˆ ๋“ฑ๋กํ•˜๊ณ  ์‹ถ์œผ๋ฉด addFilterBefore or addFilterAfter๋ฅผ ์‚ฌ์šฉํ•˜๋ผ!

 

์‚ฌ์šฉํ•ด๋ด…์‹œ๋‹ค.

 

addFilterBefore

http.addFilterBefore(new MyFilter1(), UsernamePasswordAuthenticationFilter.class);

UsernamePasswordAuthenticationFilter ์ง์ „์— MyFilter๊ฐ€ ๊ฑธ๋ฆฌ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

addFilterBefore

http.addFilterAfter(new MyFilter1(), UsernamePasswordAuthenticationFilter.class);

 

UsernamePasswordAuthenticationFilter ์ดํ›„์— MyFilter๊ฐ€ ๊ฑธ๋ฆฌ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

 

FilterConfig ์ƒ์„ฑ

๊ตณ์ด SecurityConfg์—์„œ ์ถ”๊ฐ€ํ•˜์ง€ ๋ง๊ณ , ๋”ฐ๋กœ FilterConfig๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋“ฑ๋กํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<MyFilter1> filter1() {
        FilterRegistrationBean<MyFilter1> bean = new FilterRegistrationBean<>(new MyFilter1());
        bean.addUrlPatterns("/*");  // ๋ชจ๋“  ์š”์ฒญ์— ๋Œ€ํ•ด์„œ ํ•„ํ„ฐ ์ ์šฉ
        bean.setOrder(0);   // ๋‚ฎ์€ ์ˆซ์ž์ผ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„

        return bean;
    }
}
  • FilterRegistrationBean์„ ์ƒ์„ฑํ•˜์—ฌ MyFilter1์„ ๋นˆ์œผ๋กœ ๋“ฑ๋ก์‹œ์ผœ์ฃผ๋ฉด ๋์ž…๋‹ˆ๋‹ค.
    • urlPatterns "/**" ๊ฐ€ ์•„๋‹Œ ์  ์ฃผ์˜!

 

ํ•„ํ„ฐ๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋˜๋Š”๊ฐ€?

SpringSecurityFilter๊ฐ€ ๋จผ์ € ์‹คํ–‰๋ ๊นŒ์š”? ์•„๋‹ˆ๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑํ•ด์ค€ MyFilter๊ฐ€ ๋จผ์ € ์‹คํ–‰๋ ๊นŒ์š”?

 

ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ

  • MyFilter1 ๊ณผ MyFilter2๋ฅผ ์ƒ์„ฑ
  • MyFilter1์€ FilterConfig์— ๋“ฑ๋ก! (์œ„ ์ฝ”๋“œ์™€ ๋™์ผ : ์šฐ์„ ์ˆœ์œ„ 0์œผ๋กœ ์„ค์ •)
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<MyFilter1> filter1() {
		...
        bean.setOrder(0);   // ๋‚ฎ์€ ์ˆซ์ž์ผ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„
		..
  • MyFilter2๋Š” SecurityConfig์— ๋“ฑ๋ก! (Username..Filter ์ด์ „์— ์‹คํ–‰๋˜๋„๋ก ์„ค์ •)
http.addFilterBefore(new MyFilter2(), UsernamePasswordAuthenticationFilter.class)

 

์‹คํ–‰ ๊ฒฐ๊ณผ

Filter2๊ฐ€ Filter1 ๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋˜์—ˆ๋‹ค. 

 

์šฐ๋ฆฌ๊ฐ€ ์ƒ์„ฑํ•œ ํ•„ํ„ฐ๋ณด๋‹ค SpringSecurityFilter๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.

 

SpringSecurityFilter๋ณด๋‹ค(๋˜๋Š” ํŠน์ • ํ•„ํ„ฐ ์ด์ „์— ๋˜๋Š” ์ดํ›„์—) ๋จผ์ € ์‹คํ–‰๋˜๊ฒŒ ํ•˜๋ ค๋ฉด?

SpringSecurity ์„ค์ •์—์„œ ํ•„ํ„ฐ๋ฅผ ๋“ฑ๋กํ•ด์ค€๋‹ค.

  • addFilterBefore(Filter filter, Class<? extends Filter> beforeFilter)
  • addFilterAfter(Filter filter, Class<? extends Filter> afterFilter)
๋ฐ˜์‘ํ˜•