티스토리 뷰

springboot로 전환함에따라 web.xml에서 기술을 하던 Spring Security 관련 필터들에 대한 설정을 할 필요가 없어졌다. 그럼 나머지 필터들은 어떻게 등록을 해야하나?

 

web.xml

<filter>
    <filter-name>CustomFilter</filter-name>
    <filter-class>sample.CustomFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CustomFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 

기존의 모습은 이랬다. CustomFilter라는 놈은 request로부터 IP를 가지고 오는 역할을 한다고 가정한다. 

 

CustomFilter.java

public class CustomFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {

        HttpServletRequest hReq = (HttpServletRequest)request;
        
        String ipaddr = hReq.getHeader("X-Forwarded-For");
        if(ipaddr == null || "".equals(ipaddr)) {
            ipaddr = hReq.getHeader("Proxy-Client-IP");
            if(ipaddr == null || "".equals(ipaddr)) {
                ipaddr = hReq.getRemoteAddr();
            }
        }
 
        request.setAttribute("ipAddr", ipaddr);
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {}

    @Override
    public void destroy() {}
}

 

이 CustomFilter 를 기존처럼 spring security filter 들이 동작하기 전에 동작을 시키고 싶으면 어떻게 해야 할까?

 

security-context.xml

<beans:beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
    <security:http auto-config="true" use-expressions="true">
        ... 생략 ...
        <security:custom-filter before="CHANNEL_FILTER" ref="customFilter"/>
    </security:http>   
    
    <bean id="customFilter" class="sample.CustomFilter" />
</beans:beans>

 

CustomFilter를 bean으로 등록해주고 위처럼 custom-filter 라는 tag에 등록을 시켜주면 된다. 여기서 어디에 배치를 할 수 있을지 결정을 해줄수 있다. before="CHANNEL_FILTER" 를 설명하자면 customFilter는 spring security filter 중 CHANNEL_FILTER라는것 전에 동작하게끔 하라는 뜻이다.

이 CHANNEL_FILTER는 무엇인가? spring security filter의 alias 이다. 선택할수 있는 alias는 다음과 같다. 

 

Alias Filter Class Namespace Element or Attribute
CHANNEL_FILTER ChannelProcessingFilter http/intercept-url@requires-channel
SECURITY_CONTEXT_FILTER SecurityContextPersistenceFilter http
CONCURRENT_SESSION_FILTER ConcurrentSessionFilter session-management/concurrency-control
LOGOUT_FILTER LogoutFilter http/logout
X509_FILTER X509AuthenticationFilter http/x509
PRE_AUTH_FILTER AstractPreAuthenticatedProcessingFilter Subclasses N/A
CAS_FILTER CasAuthenticationFilter N/A
FORM_LOGIN_FILTER UsernamePasswordAuthenticationFilter http/form-login
BASIC_AUTH_FILTER BasicAuthenticationFilter http/http-basic
SERVLET_API_SUPPORT_FILTER SecurityContextHolderAwareRequestFilter http/@servlet-api-provision
JAAS_API_SUPPORT_FILTER JaasApiIntegrationFilter http/@jaas-api-provision
REMEMBER_ME_FILTER RememberMeAuthenticationFilter http/remember-me
ANONYMOUS_FILTER AnonymousAuthenticationFilter http/anonymous
SESSION_MANAGEMENT_FILTER SessionManagementFilter session-management
EXCEPTION_TRANSLATION_FILTER ExceptionTranslationFilter http
FILTER_SECURITY_INTERCEPTOR FilterSecurityInterceptor http
SWITCH_USER_FILTER SwitchUserFilter N/A

출처 : https://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html

 

 

custom filter를 맨 앞에 넣고 싶으면 위처럼 

<security:custom-filter before="CHANNEL_FILTER" ref="customFilter"/>
or
<security:custom-filter before="FIRST" ref="customFilter"/>

 

custom filter를 맨 뒤에 넣고 싶으면

<security:custom-filter after="SWITCH_USER_FILTER" ref="customFilter"/>
or
<security:custom-filter after="LAST" ref="customFilter"/>

 

custom filter를 기존 spring security filter와 교체하고 싶을때는 

<security:custom-filter position="FORM_LOGIN_FILTER" ref="customFilter"/>

 

이런식으로 작성을 해주면 된다. 단 position을 사용할때는 auto-config를 false로 세팅을 해야 한다. 말 그대로 auto-config를 사용하지 않으므로 이에 따른 설정은 별도로 필요하다. 

 

 

끝!

댓글
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31