本文为转载学习
原文链接:http://blog.csdn.net/dsundsun/article/details/11750189
我们了解一下验证的过程:首先用户发起一个请求、 这时候,认证管理器进行拦截,验证用户发起请求时的一些凭证信息,未通过验证信息审核的那么返回给用户,通过审核的,那么继续进行请求访问,访问页面之前,会被访问决策管理拦截,访问决策管理器验证用户是否有访问页面的权限,如果有,那么继续到访问页面。
其实spring security这样的权限框架就是根据一系列的依赖代理(delegates)和servlet过滤器来实现的。看看如下这个图:
首先先通过过滤器拦截用户的请求,拦截后通过servlet来进行处理,如果处理成功那么进行正常访问,在返回给用户一个请求。
所以了解过滤器链的优先级是非常重要的。通过我查看spring security的帮助说明文档,我们可以看到过滤器链的顺序,如下:
1.ChannelProcessingFilter, because it might need to redirect to a different protocol
【ChannelProcessingFilter,使用它因为我们可能会指向不同的协议(如:Http,Https)】
2.SecurityContextPersistenceFilter, so a SecurityContext can be set up in the SecurityContextHolder at the beginning of a web request, and any changes to the SecurityContext can be copied to the HttpSession when the web request ends (ready for use with the next web request)
【SecurityContextPersistenceFilter,负责从SecurityContextRepository 获取或存储 SecurityContext。SecurityContext 代表了用户安全和认证过的session】
3.ConcurrentSessionFilter, because it uses the SecurityContextHolder functionality and needs to update the SessionRegistry to reflect ongoing requests from the principal
【ConcurrentSessionFilter,使用SecurityContextHolder的功能,更新来自“安全对象”不间断的请求,进而更新SessionRegistry】
4.Authentication processing mechanisms - UsernamePasswordAuthenticationFilter, CasAuthenticationFilter, BasicAuthenticationFilter etc - so that the SecurityContextHolder can be modified to contain a valid Authentication request token
【认证进行机制,UsernamePasswordAuthenticationFilter,CasAuthenticationFilter,BasicAuthenticationFilter等等--SecurityContextHolder可能会修改含有Authentication这样认证信息的token值】
5.The SecurityContextHolderAwareRequestFilter, if you are using it to install a Spring Security aware HttpServletRequestWrapper into your servlet container
【SecurityContextHolderAwareRequestFilter,如果你想用它的话,需要初始化spring security中的HttpServletRequestWrapper到你的servlet容器中】
6.The JaasApiIntegrationFilter, if a JaasAuthenticationToken is in the SecurityContextHolder this will process the FilterChain as the Subject in the JaasAuthenticationToken
【JaasApiIntegrationFilter,如果JaasAuthenticationToken在SecurityContextHolder的上下文中,在过滤器链中JaasAuthenticationToken将作为一个对象。】
7.RememberMeAuthenticationFilter, so that if no earlier authentication processing mechanismupdated the SecurityContextHolder, and the request presents a cookie that enables remember-me services to take place, a suitable remembered Authentication object will be put there
【RememberMeAuthenticationFilter,如果还没有新的认证程序机制更新SecurityContextHolder,并且请求已经被一个“记住我”的服务替代,那么将会有一个Authentication对象将存放到这(就是 已经作为cookie请求的内容)。】
8.AnonymousAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, an anonymous Authentication object will be put there
【AnonymousAuthenticationFilter,如果没有任何认证程序机制更新SecurityContextHolder,一个匿名的对象将存放到这。】
9.ExceptionTranslationFilter, to catch any Spring Security exceptions so that either an HTTP error response can be returned or an appropriate AuthenticationEntryPoint can be launched
【ExceptionTranslationFilter,为了捕获spring security的错误,所以一个http响应将返回一个Exception或是触发AuthenticationEntryPoint。】
10.FilterSecurityInterceptor, to protect web URIs and raise exceptions when access is denied
【FilterSecurityInterceptor,当连接被拒绝时,保护web URLS并且抛出异常。】
还记得spring security的xml吗?里边的配置文件:
<http auto-config="true" > <intercept-url pattern= "/*" access="ROLE_USER" /> </http >
有一个属性叫做auto-config ,这个是一个自动配置过滤器(Filter)的属性 我们进入eclipse 然后光标指向它 点一下F2
我们可以看到这个属性的介绍,大概意思是:这是一个预留的属性,他可以自动配置login form,BSIC 认证和logout URL 和logout services,如果没有特殊表明,这个的默认值是false。我们推荐你避免使用这个属性,相反的,配置你需要的一些服务。。,,(spring security本来不希望我们使用这个属性,这个事我们都先记着)
那好,今天就到这里,我们了解了两个事情:
第一个:整个spring security是有过滤器链和sevlet组成的,并且是按一定顺序执行的,而且查看相关spring security提供的说明,我们可以清楚的了解到这些过滤器大致都是做什么用的、
第二个:上次我们跑起来的应用中,配置了auto-config="true"这个属性,它帮我们自动添加了一些过滤器,使项目跑起来很快,但是由于不太有针对性(这是我自己的猜测),spring security不建议我们使用它,spring security更希望我们能配置一些有针对性的服务。