spring security3.x学习(3)_初探过滤器机制和auto-config用法

来源:互联网 发布:网络数据传输方式 编辑:程序博客网 时间:2024/06/05 18:19

我们了解一下验证的过程 、首先用户发起一个请求、 这时候,认证管理器进行拦截,验证用户发起请求时的一些凭证信息,未通过验证信息审核的那么返回给用户,通过审核的,那么继续进行请求访问,访问页面之前,会被访问决策管理拦截,访问决策管理器验证用户是否有访问页面的权限,如果有,那么继续到访问页面。


其实spring security这样的权限框架就是根据一系列的依赖代理(delegates)和servlet过滤器来实现的。看看如下这个图:


首先先通过过滤器拦截用户的请求,拦截后通过servlet来进行处理,如果处理成功那么进行正常访问,在返回给用户一个请求。


所以了解过滤器链的优先级是非常重要的。通过我查看spring security的帮助说明文档,我们可以看到过滤器链的顺序,如下:



1.ChannelProcessingFilter, because it might need to redirect to a different protocol
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)
3.ConcurrentSessionFilter, because it uses the SecurityContextHolder functionality and needs to update the SessionRegistry to reflect ongoing requests from the principal
4.Authentication  processing  mechanisms  -  UsernamePasswordAuthenticationFilter, CasAuthenticationFilter,  BasicAuthenticationFilter  etc  -  so  that  the SecurityContextHolder can be modified to contain a valid Authentication request token
5.The SecurityContextHolderAwareRequestFilter,  if  you  are  using  it  to  install  a  Spring Security aware HttpServletRequestWrapper into your servlet container
6.The  JaasApiIntegrationFilter,  if  a  JaasAuthenticationToken  is  in  the SecurityContextHolder  this  will  process  the  FilterChain  as  the  Subject  in  the 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
8.AnonymousAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, an anonymous Authentication object will be put there
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
10.FilterSecurityInterceptor, to protect web URIs and raise exceptions when access is denied


文档中大概给出了10个过滤器的顺序,我们试着可以翻译一下:

1.ChannelProcessingFilter,使用它因为我们可能会指向不同的协议(如:Http,Https)
2.SecurityContextPersistenceFilter,负责从SecurityContextRepository 获取或存储 SecurityContext。SecurityContext 代表了用户安全和认证过的session
3.ConcurrentSessionFilter,使用SecurityContextHolder的功能,更新来自“安全对象”不间断的请求,进而更新SessionRegistry
4.认证进行机制,UsernamePasswordAuthenticationFilter,CasAuthenticationFilter,BasicAuthenticationFilter等等--SecurityContextHolder可能会修改含有Authentication这样认证信息的token值
5.SecurityContextHolderAwareRequestFilter,如果你想用它的话,需要初始化spring security中的HttpServletRequestWrapper到你的servlet容器中。
6.JaasApiIntegrationFilter,如果JaasAuthenticationToken在SecurityContextHolder的上下文中,在过滤器链中JaasAuthenticationToken将作为一个对象。
7.RememberMeAuthenticationFilter,如果还没有新的认证程序机制更新SecurityContextHolder,并且请求已经被一个“记住我”的服务替代,那么将会有一个Authentication对象将存放到这(就是 已经作为cookie请求的内容)。
8.AnonymousAuthenticationFilter,如果没有任何认证程序机制更新SecurityContextHolder,一个匿名的对象将存放到这。
9.ExceptionTranslationFilter,为了捕获spring security的错误,所以一个http响应将返回一个Exception或是触发AuthenticationEntryPoint。
10.FilterSecurityInterceptor,当连接被拒绝时,保护web URLS并且抛出异常。


通过介绍完这么多的过滤器,相信我们都想到了,,,我应该可以建一个自己的过滤器,, 应该是的。 呵呵。 我们先这么猜测的。 然后继续看书和文档。


还记得spring security的xml吗?、 里边的配置文件、

[html] view plaincopy
  1. <http auto-config="true" >  
  2.         <intercept-url pattern"/*" access="ROLE_USER" />  
  3. </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更希望我们能配置一些有针对性的服务。

阅读全文
0 0
原创粉丝点击