springsecurityrememberme功能_第1页
springsecurityrememberme功能_第2页
springsecurityrememberme功能_第3页
springsecurityrememberme功能_第4页
springsecurityrememberme功能_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、spring security remember me 功能 1、建立 login.jsp 页面 .内容如下 : Html 代码 form action=%=path %/j_spring_security_check method=post USERNAME:input type=text name=j_username value=$sessionScopeSPRING_SECURITY_LAST_USE RNAME /br/ PASSWORD:input type=password name=j_password value= /br/ input type=checkbox name=

2、_spring_security_remember_me / 两周之内不 必登陆 br/ input type=submit /form form action=%=path %/j_spring_security_check method=post USERNAME:input type=text name=j_username value=$sessionScopeSPRING_SECURITY_LAST_USE RNAME /br/ PASSWORD:input type=password name=j_password value= /br/ input type=checkbox n

3、ame=_spring_security_remember_me /两周之内不 必登陆 br/ input type=submit /formj_spring_security_check : 为 security 验证 中心 (不知道怎么说合适 .暂时这么理解吧 .). j_username: 验证用户名 ; j_password: 验证密码 ; $sessionScopeSPRING_SECURITY_LAST_USERNAM E: 使用最后一次登录用户名 . _spring_security_remember_me: 记住我 . ( 2)Spring Security 提供了 Remem

4、ber-me 机制用来实现记录用户的 登录状态。方便用户下 次自动登录。 Spring Security 对此操作提供了必要的钩子, remember-me 有两个固定的实现 一个是使用把用户登录信息加密以 cookie 的方式保存到客 户端。一是用户可以把用户信息 持久化到服务端的数据库中。 注意:这两种实现都必须有一个 UserDetailsService 实 现。如果你使用一个没有使用 UserDetailsService 的验证提供者, Remember-me 将不会 工作,除非你在应用的上下文中 有一个 UserDetailsService 对象。 一、基本的 Hash 保存到客户端

5、的 Cookie 中。 直接在命名空间 http 中加入 remember-me / 元素,就可以使用本方法。但要注意的 是在你的。此时系统会自动为你生成 UserDetailsService 对 象(如果你在配置文件中定义了此 对象必须在 remember-me / 使用 user-service-ref 指 定 )。本策略使用的为 java view plaincopyprint? org.springframework.security.web.authentication.remembe rme.TokenBasedRememberMeServices org.springframew

6、ork.security.web.authentication.remembe rme.TokenBasedRememberMeServices 此策略会在客户端 生成一个名称为 SPRING_SECURITY_REMEMBER_ME_COOKIE ,值进 行加密后的 Cookie 。 二、持久化到服务器端的服务器中 如果想使用本策略,你需要在命名空间的配置中提供 一个 datasource 引用: html view plaincopyprint? http remember-me data-source-ref=someDataSource / /http http remember-me

7、 data-source-ref=someDataSource / /http 数据库中还必须包含一个名为 persistent_logins 的表,表结构使用一下 SQL 语句创建 (或 者是等价的 ): sql view plaincopyprint? create table persistent_logins ( username varchar(64) not null, series varchar(64) primary key, token varchar(64) not null, last_used timestamp not null)create table persi

8、stent_logins ( username varchar(64) not null, series varchar(64) primary key, token varchar(64) not null, last_used timestamp not null)由于以上两种策略对应 的实现类都的父类 (org.springframework.security.web.authentication.rememb erme.AbstractRememberMeServices) 实现了 LogoutHandler 接口。 在 AbstractRememberMeServices 类中的 l

9、ogout(.) 方 法中清楚了 Cookie 。如下所示: java view plaincopyprint? public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) if (logger.isDebugEnabled() logger.debug( Logout of user + (authentication = null ? Unknown : authentication.getName(); cancelCookie(re

10、quest, response); public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) if (logger.isDebugEnabled() logger.debug( Logout of user + (authentication = null ? Unknown : authentication.getName(); cancelCookie(request, response); .java view plaincopyp

11、rint? * Sets a cancel cookie (with maxAge = 0) on the response to disable persistent logins. * * param request * param response */ protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) logger.debug(Cancelling cookie); Cookie cookie = new Cookie(cookieName, null); cook

12、ie.setMaxAge(0); cookie.setPath(getCookiePath(request); response.addCookie(cookie); /* Sets a cancel cookie (with maxAge = 0) on the response to disable persistent logins. * param request * param response */ protected void cancelCookie(HttpServletRequest request, HttpServletResponse response) logger

13、.debug(Cancelling cookie); Cookie cookie = new Cookie(cookieName, null); cookie.setMaxAge(0); cookie.setPath(getCookiePath(request); response.addCookie(cookie); 如果想在用户安全退出后不删除 Cookie , 可以自己实现类, 实现类可以继承 org.springframework.security.web.authentication.remembe rme.PersistentTokenBasedRememberMeServices 或 org.springframework.securi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论