`
xyz20003
  • 浏览: 290284 次
  • 性别: Icon_minigender_1
  • 来自: 唐山
社区版块
存档分类
最新评论

(翻译)Spring Security-2.0.x参考文档“表单认证机制”

阅读更多
表单认证机制
11.1. 概述

HTTP表单认证,使用AuthenticationProcessingFilter来处理一个登录表单。 这是系统认证最终用户的最常见的一种方法。 基于表单认证与DAO和JAAS认证提供器是完全兼容的。
11.2. 配置

登录表单包含j_username 和 j_password 输入框,然后将数据发送到过滤器监听的一个URL(默认是/j_spring_security_check)。 你应该把AuthenticationProcessingFilter添加到你的application context中:

<bean id="authenticationProcessingFilter"
    class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
  <property name="authenticationManager" ref="authenticationManager"/>
  <property name="authenticationFailureUrl" value="/login.jsp?login_error=1"/>
  <property name="defaultTargetUrl" value="/"/>
  <property name="filterProcessesUrl" value="/j_spring_security_check"/>
</bean>
           

配置好的AuthenticationManager会处理每个认证请求。 如果认证失败,浏览器会重定向到authenticationFailureUrl。 AuthenticationException会被放到HttpSession中,属性名是AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY,在错误页里为用户提供一个出错原因。

如果认证成功了,得到的Authentication对象会被放到SecurityContextHolder中。

一旦SecurityContextHolder更新了, 浏览器需要重定向到目标URL,这个URL通常保存在HttpSession里,属性名是AbstractProcessingFilter.SPRING_SECURITY_TARGET_URL_KEY。 这个属性自动由ExceptionTranslationFilter在抛出AuthenticationException异常的时候设置,这样在登录完成之后,用户可以跳转到最初尝试访问的位置。 如果因为一些原因,HttpSession没办法分析目标URL,浏览器将重定向到defaultTargetUrl属性的值。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics