Spring Security学习笔记之ChannelProcessingFilter

ChannelProcessingFilter通常是用来过滤哪些请求必须用https协议, 哪些请求必须用http协议, 哪些请求随便用哪个协议都行.
 
  
 
  
下面是自动配置SpringSecurity过滤器时的配置方式:
 
  

	
	
	
	
	
		
	
	
	...

下面是手动配置SpringSecurity过滤器时的配置方式:
 
  

	
		
			
			
			
			
			
		
	



	
	
		
		   
		   
		   
		   
		 
	



	
		
			
			
		
	



	



	



	



	




	
		
			
		
	

下面看看ChannelProcessingFilter的doFilter()方法:
 
  
public class ChannelProcessingFilter extends GenericFilterBean {

    private ChannelDecisionManager channelDecisionManager;
    private FilterInvocationSecurityMetadataSource securityMetadataSource;
	
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        FilterInvocation fi = new FilterInvocation(request, response, chain);
		
	// 找出当前请求所需要的权限(attr的值可以是: ANY_CHANNEL, REQUIRES_SECURE_CHANNEL, REQUIRES_INSECURE_CHANNEL)
        Collection attr = this.securityMetadataSource.getAttributes(fi);

        if (attr != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Request: " + fi.toString() + "; ConfigAttributes: " + attr);
            }

	    // 判断当前请求与协议是否相符
            channelDecisionManager.decide(fi, attr);

            if (fi.getResponse().isCommitted()) {
                return;
            }
        }

        chain.doFilter(request, response);
    }

    ...
}

ChannelProcessingFilter主要有两个属性:
channelDecisionManager: 判断当前请求与协议是否相符;
securityMetadataSource: 储存请求与协议的对应关系.
 
  
 
  
doFilter()方法主要做了两步:
第一步, 找出当前请求所需要的协议. attr的值可以是ANY_CHANNEL(http和https都可以), REQUIRES_SECURE_CHANNEL(必须是https协议), REQUIRES_INSECURE_CHANNEL(必须是http协议).
第二步, 判断当前请求是否与协议相符. 若不相符, 则修改协议并自动跳转. 若相符, 则跳到下一个过滤器.

你可能感兴趣的:(Spring Security学习笔记之ChannelProcessingFilter)