【SpringSecurity】Spring Security过滤器链
【SpringSecurity】Spring Security过滤器链
- 1. SpringSecurity常用过滤器介绍
- 2. SpringSecurity过滤器链加载原理
- 2.1 DelegatingFilterProxy
- 2.2 FilterChainProxy
- 2.3 SecurityFilterChain
1. SpringSecurity常用过滤器介绍
接下来我们就来看看常见的过滤器。
1. org.springframework.security.web.context.SecurityContextPersistenceFilter首当其冲的一个过滤器,作用之重要,自不必多言。SecurityContextPersistenceFilter主要是使用SecurityContextRepository在session中保存或更新一个SecurityContext,并将SecurityContext给以后的过滤器使用,来为后续filter建立所需的上下文。SecurityContext中存储了当前用户的认证以及权限信息。2. org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter此过滤器用于集成SecurityContext到Spring异步执行机制中的WebAsyncManager3. org.springframework.security.web.header.HeaderWriterFilter向请求的Header中添加相应的信息,可在http标签内部使用security:headers来控制4. org.springframework.security.web.csrf.CsrfFiltercsrf又称跨域请求伪造,SpringSecurity会对所有post请求验证是否包含系统生成的csrf的token信息,如果不包含,则报错。起到防止csrf攻击的效果。5. org.springframework.security.web.authentication.logout.LogoutFilter匹配URL为/logout的请求,实现用户退出,清除认证信息。6. org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter认证操作全靠这个过滤器,默认匹配URL为/login且必须为POST请求。7. org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter
如果没有在配置文件中指定认证页面,则由该过滤器生成一个默认认证页面。8. org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter由此过滤器可以生产一个默认的退出登录页面9. org.springframework.security.web.authentication.www.BasicAuthenticationFilter此过滤器会自动解析HTTP请求中头部名字为Authentication,且以Basic开头的头信息。10. org.springframework.security.web.savedrequest.RequestCacheAwareFilter通过HttpSessionRequestCache内部维护了一个RequestCache,用于缓存HttpServletRequest11. org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter针对ServletRequest进行了一次包装,使得request具有更加丰富的API12. org.springframework.security.web.authentication.AnonymousAuthenticationFilter当SecurityContextHolder中认证信息为空,则会创建一个匿名用户存入到SecurityContextHolder中。spring security为了兼容未登录的访问,也走了一套认证流程,只不过是一个匿名的身份。13. org.springframework.security.web.session.SessionManagementFilterSecurityContextRepository限制同一用户开启多个会话的数量14. org.springframework.security.web.access.ExceptionTranslationFilter异常转换过滤器位于整个springSecurityFilterChain的后方,用来转换整个链路中出现的异常15. org.springframework.security.web.access.intercept.FilterSecurityInterceptor获取所配置资源访问的授权信息,根据SecurityContextHolder中存储的用户信息来决定其是否有权限。
当然,过滤器肯定不止这些,只是这些比较常见罢了,具体运行中会使用哪些过滤器主要是和我们配置的信息有关,不一定所有的过滤器都会参与。
2. SpringSecurity过滤器链加载原理
在web项目中,我们都知道一般情况都是通过web.xml设置过滤器,并且初始化。
但是springSecurity并没有设置那么多过滤器啊,只是设置了DelegatingFilterProxy,为什么却执行了那么多过滤器呢?
下面我们就来看看吧
2.1 DelegatingFilterProxy
我们在web.xml中配置了一个名称为springSecurityFilterChain的过滤器DelegatingFilterProxy,接下我直接对DelegatingFilterProxy源码里重要代码进行说明,其中删减掉了一些不是很重要的代码。
public class DelegatingFilterProxy extends GenericFilterBean {@Nullableprivate String contextAttribute;@Nullableprivate WebApplicationContext webApplicationContext;@Nullableprivate String targetBeanName;private boolean targetFilterLifecycle;@Nullableprivate volatile Filter delegate;//注:这个过滤器才是真正加载的过滤器private final Object delegateMonitor;//注:doFilter才是过滤器的入口,直接从这看!public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {Filter delegateToUse = this.delegate;if (delegateToUse == null) {synchronized(this.delegateMonitor) {delegateToUse = this.delegate;if (delegateToUse == null) {WebApplicationContext wac = this.findWebApplicationContext();if (wac == null) {throw new IllegalStateException("No WebApplicationContext found: noContextLoaderListener or DispatcherServlet registered?");}//第一步:doFilter中最重要的一步,初始化上面私有过滤器属性delegatedelegateToUse = this.initDelegate(wac);} this.delegate = delegateToUse;}} //第三步:执行FilterChainProxy过滤器this.invokeDelegate(delegateToUse, request, response, filterChain);}//第二步:直接看最终加载的过滤器到底是谁protected Filter initDelegate(WebApplicationContext wac) throws ServletException {//debug得知targetBeanName为:springSecurityFilterChainString targetBeanName = this.getTargetBeanName();Assert.state(targetBeanName != null, "No target bean name set");//debug得知delegate对象为:FilterChainProxyFilter delegate = (Filter)wac.getBean(targetBeanName, Filter.class);if (this.isTargetFilterLifecycle()) {delegate.init(this.getFilterConfig());} return delegate;} protected void invokeDelegate(Filter delegate, ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {delegate.doFilter(request, response, filterChain);}
}
第二步debug结果如下:

由此可知,DelegatingFilterProxy通过springSecurityFilterChain这个名称,得到了一个FilterChainProxy过滤器,最终在第三步执行了这个过滤器。
2.2 FilterChainProxy
public class FilterChainProxy extends GenericFilterBean {private static final Log logger = LogFactory.getLog(FilterChainProxy.class);private static final String FILTER_APPLIED = FilterChainProxy.class.getName().concat(".APPLIED");private List<SecurityFilterChain> filterChains;private FilterChainProxy.FilterChainValidator filterChainValidator;private HttpFirewall firewall;//咿!?可以通过一个叫SecurityFilterChain的对象实例化出一个FilterChainProxy对象//这FilterChainProxy又是何方神圣?会不会是真正的过滤器链对象呢?先留着这个疑问!public FilterChainProxy(SecurityFilterChain chain) {this(Arrays.asList(chain));} //又是SecurityFilterChain这家伙!嫌疑更大了!public FilterChainProxy(List<SecurityFilterChain> filterChains) {this.filterChainValidator = new FilterChainProxy.NullFilterChainValidator();this.firewall = new StrictHttpFirewall();this.filterChains = filterChains;} //注:直接从doFilter看public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {boolean clearContext = request.getAttribute(FILTER_APPLIED) == null;if (clearContext) {try {request.setAttribute(FILTER_APPLIED, Boolean.TRUE);this.doFilterInternal(request, response, chain);} finally {SecurityContextHolder.clearContext();request.removeAttribute(FILTER_APPLIED);}} else {//第一步:具体操作调用下面的doFilterInternal方法了this.doFilterInternal(request, response, chain);}} private void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {FirewalledRequest fwRequest = this.firewall.getFirewalledRequest((HttpServletRequest)request);HttpServletResponse fwResponse = this.firewall.getFirewalledResponse((HttpServletResponse)response);//第二步:封装要执行的过滤器链,那么多过滤器就在这里被封装进去了!List<Filter> filters = this.getFilters((HttpServletRequest)fwRequest);if (filters != null && filters.size() != 0) {FilterChainProxy.VirtualFilterChain vfc = new FilterChainProxy.VirtualFilterChain(fwRequest, chain, filters);//第四步:加载过滤器链vfc.doFilter(fwRequest, fwResponse);} else {if (logger.isDebugEnabled()) {logger.debug(UrlUtils.buildRequestUrl(fwRequest) + (filters == null ? " has nomatching filters" : " has an empty filter list"));} fwRequest.reset();chain.doFilter(fwRequest, fwResponse);}} private List<Filter> getFilters(HttpServletRequest request) {Iterator var2 = this.filterChains.iterator();//第三步:封装过滤器链到SecurityFilterChain中!SecurityFilterChain chain;do {if (!var2.hasNext()) {return null;}chain = (SecurityFilterChain)var2.next();} while(!chain.matches(request));return chain.getFilters();}
}
第二步debug结果如下图所示,十五个过滤器都在这里了!

当然出现15个得原因是因为我配置文件使用得最简单得设置,没有关闭csrf那些。
再看第三步,怀疑这么久!原来这些过滤器还真是都被封装进SecurityFilterChain中了。
2.3 SecurityFilterChain
最后看SecurityFilterChain,这是个接口,实现类也只有一个,这才是web.xml中配置的过滤器链对象!
/
/接口
public interface SecurityFilterChain {boolean matches(HttpServletRequest var1);List<Filter> getFilters();
} //实现类
public final class DefaultSecurityFilterChain implements SecurityFilterChain {private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);private final RequestMatcher requestMatcher;private final List<Filter> filters;public DefaultSecurityFilterChain(RequestMatcher requestMatcher, Filter... filters) {this(requestMatcher, Arrays.asList(filters));} public DefaultSecurityFilterChain(RequestMatcher requestMatcher, List<Filter> filters) {logger.info("Creating filter chain: " + requestMatcher + ", " + filters);this.requestMatcher = requestMatcher;this.filters = new ArrayList(filters);} public RequestMatcher getRequestMatcher() {return this.requestMatcher;} public List<Filter> getFilters() {return this.filters;} public boolean matches(HttpServletRequest request) {return this.requestMatcher.matches(request);} public String toString() {return "[ " + this.requestMatcher + ", " + this.filters + "]";}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
