java Spring Security 总结一 4

LogoutFilter的构造函数需要两个参数,第一个是退出系统后系统跳转到的URL,第二个是一个LogoutHandler类型的数组,这个数组里的对象都实现了LogoutHandler接口,并实现了它的logout方法,用户在发送退出请求后,会一次执行LogoutHandler数组的对象并调用它们的 logout方法进行一些后续的清理操作,主要是从SecurityContextHolder对象中清楚所有用户的认证信息(Authentication对象),将用户的会话对象设为无效,这些都时由SecurityContextLogoutHandler来完成。 LogoutFilter还会清除Cookie记录,它由另外一个Bean来完成(RememberMeServices)。

    <ref bean="rememberMeServices"/>标记指向了我们另外配置的一个Bean:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1  < bean  id ="rememberMeServices"     
        class
="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices"
2      p:key ="springsecurity"
3     p:userDetailsService-ref ="userDetailsService" />

    TokenBasedRememberMeServices继承自系统的AbstractRememberMeServices抽象类(实现了 RememberMeServices和 LogoutHandler两个接口), RememberMeServices接口的loginSuccess方法负责在用户成功登录之后将用户的认证信息存入Cookie中,这个类在后续的过滤器执行过程中也会被用到。

    另一个userDetailsService属性也是指向了我们配置的Bean, 它负责从数据库中读取用户的信息,这个类的详细配置将在后面的部分详细介绍,这里只是简单的认识一下。

    过滤器链的下个配置的过滤器是authenticationProcessingFilter(认证过程过滤器),我们使用它来处理表单认证,当接受到与filterProcessesUrl所定义相同的请求时它开始工作:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  < bean  id ="authenticationProcessingFilter"
 2 
 3      class ="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"
 4 
 5      p:authenticationManager-ref ="authenticationManager"
 6      p:authenticationFailureUrl ="/login.jsp?login_error=1"
 7     p:defaultTargetUrl ="/default.jsp"
 8      p:filterProcessesUrl ="/j_spring_security_check"
 9     p:rememberMeServices-ref ="rememberMeServices" />

    下面列出了认证过程过滤器配置中各个属性的功能:

    1.authenticationManager     认证管理器

    2.authenticationFailureUrl 定义登录失败时转向的页面

    3.defaultTargetUrl         定义登录成功时转向的页面

    4.filterProcessesUrl        定义登录请求的地址(在web.xml中配置过)

    5.rememberMeServices        在验证成功后添加cookie信息

    这里也用到了rememberMeServices,如果用户认证成功,将调用RememberMeServices的loginSuccess方法将用户认证信息写入Cookie中,这里也可以看到使用IoC的好处。

    决定用户是否有权限访问受保护资源的第一步就是要确定用户的身份,最常用的方式就是用户提供一个用户名和密码以确认用户的身份是否合法,这一步就是由认证过程过滤器调用authenticationManager(认证管理器)来完成的。 org.springframework.security.AuthenticationManager接口定义了一个authenticate方法,它使用Authentication作为入口参数(只包含用户名和密码),并在验证成功后返回一个完整的Authentication对象(包含用户的权限信息GrantedAuthority数组对象),authenticationProcessingFilter(认证过程过滤器)会将这个完整的 Authentication对象存入SecurityContext中,如果认证失败会抛出一个AuthenticationException并跳转到authenticationFailureUrl 定义的URL.认证管理其配置如下:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  < bean  id ="authenticationManager"
 2 
 3  class ="org.springframework.security.providers.ProviderManager"
 4   p:sessionController-ref ="concurrentSessionController" >
 5       < property  name ="providers" >
 6          < list >
 7              < ref  bean ="daoAuthenticationProvider" />
 8              < bean
 9 
10  class ="org.springframework.security.providers.anonymous.AnonymousAuthenticationProvider"
11                p:key ="springsecurity" />
12              < bean
13 
14  class ="org.springframework.security.providers.rememberme.RememberMeAuthenticationProvider"
15                p:key ="springsecurity" />
16          </ list >
17       </ property >   
18  </ bean >

    正如在配置中看到的一样,系统使用org.springframework.security.providers.ProviderManager(提供者管理器)类作为认证管理器的一个实现,事实上这个类是继承自实现了AuthenticationManager接口的 AbstractAuthenticationManager类。需要注意的是ProviderManager(提供者管理器)自己并不实现身份验证,而是把这项工作交给了多个认证提供者(提供者集合)或者说的多个认证来源。

 

提示:
Spring Security为我们提供的所有认证提供者实现都是org.springframework.security.providers .AuthenticationProvider
接口的实现类,它们都实现了此接口的authenticate方法,如果你正在看源代码,会发现这个authenticate方法事实上和Authe
nticationManager(认证管理器)接口的authenticate方法完全一样。
 

    providers属性定义了提供者管理器的集合,ProviderManager(提供者管理器)逐一遍历这个认证提供者的集合并调用提供者的 authenticate方法,如果一个提供者认证失败会尝试另外一个提供者直到某一个认证提供者能够成功的验证该用户的身份,以保证获取不同来源的身份认证。下面表格列出了系统提供的一些认证提供者:

 

     

            

DaoAuthenticationProvider

从数据库中读取用户信息验证身份

AnonymousAuthenticationProvider

匿名用户身份认证

RememberMeAuthenticationProvider

已存cookie中的用户信息身份认证

AuthByAdapterProvider

使用容器的适配器验证身份

CasAuthenticationProvider

根据Yale中心认证服务验证身份, 用于实现单点登陆

JaasAuthenticationProvider

从JASS登陆配置中获取用户信息验证身份

RemoteAuthenticationProvider

根据远程服务验证用户身份

RunAsImplAuthenticationProvider

对身份已被管理器替换的用户进行验证

X509AuthenticationProvider

从X509认证中获取用户信息验证身份

TestingAuthenticationProvider

单元测试时使用

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值