009-shiro与spring web项目整合【三】验证码、记住我
一、验证码
1、自定义FormAuthenticationFilter
需要在验证账号和名称之前校验验证码
/**
*
* <p>Title: CustomFormAuthenticationFilter</p>
* <p>Description:自定义FormAuthenticationFilter,认证之前实现 验证码校验 </p>
* @version 1.0
*/
public class CustomFormAuthenticationFilter extends FormAuthenticationFilter { //原FormAuthenticationFilter的认证方法
@Override
protected boolean onAccessDenied(ServletRequest request,
ServletResponse response) throws Exception {
//在这里进行验证码的校验
//从session获取正确验证码
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpSession session =httpServletRequest.getSession();
//取出session的验证码(正确的验证码)
String validateCode = (String) session.getAttribute("validateCode"); //取出页面的验证码
//输入的验证和session中的验证进行对比
String randomcode = httpServletRequest.getParameter("randomcode");
if(randomcode!=null && validateCode!=null && !randomcode.equals(validateCode)){
//如果校验失败,将验证码错误失败信息,通过shiroLoginFailure设置到request中
httpServletRequest.setAttribute("shiroLoginFailure", "randomCodeError");
//拒绝访问,不再校验账号和密码
return true;
}
return super.onAccessDenied(request, response);
}
}
2、FormAuthenticationFilter配置
修改applicationContext-shiro.xml中对FormAuthenticationFilter的配置
a、在shiroFilter中添加filters:
<!-- 自定义filter配置 -->
<property name="filters">
<map>
<!-- 将自定义 的FormAuthenticationFilter注入shiroFilter中 -->
<entry key="authc" value-ref="formAuthenticationFilter"/>
</map>
</property>
b、formAuthenticationFilter定义
<!-- 自定义form认证过虑器 -->
<!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 -->
<bean id="formAuthenticationFilter" class="com.lhx.ssm.shiro.CustomFormAuthenticationFilter">
<!-- 表单中账号的input名称 -->
<property name="usernameParam" value="username"/>
<!-- 表单中密码的input名称 -->
<property name="passwordParam" value="password"/>
</bean>
3、登录页面
添加验证码:
<TR>
<TD>验证码:</TD>
<TD><input id="randomcode" name="randomcode" size="8" /> <img
id="randomcode_img" src="${baseurl}validatecode.jsp" alt=""
width="56" height="20" align='absMiddle' /> <a
href=javascript:randomcode_refresh()>刷新</a></TD>
</TR>
4、配置validatecode.jsp匿名访问
修改applicationContext-shiro.xml:
<!-- 验证码,可匿名访问 -->
/validatecode.jsp = anon
二、记住我
用户登陆选择“自动登陆”本次登陆成功会向cookie写身份信息,下次登陆从cookie中取出身份信息实现自动登陆。
1、用户身份实现java.io.Serializable接口
向cookie记录身份信息需要用户身份信息对象实现序列化接口,如下:
/**
* 用户身份信息,存入session 由于tomcat将session会序列化在本地硬盘上,所以使用Serializable接口
*/
public class ActiveUser implements java.io.Serializable {
2、配置rememberMeManager
修改applicationContext-shiro.xml:
安全管理器
<!-- securityManager安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="customRealm"/>
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager"/>
<!-- 记住我 -->
<property name="rememberMeManager" ref="rememberMeManager"/>
</bean>
rememberMeManager管理器和cookie
<!-- rememberMeManager管理器,写cookie,取出cookie生成用户信息 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie" ref="rememberMeCookie"/>
</bean>
<!-- 记住我cookie -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<!-- rememberMe是cookie的名字 -->
<constructor-arg value="rememberMe"/>
<!-- 记住我cookie生效时间30天 -->
<property name="maxAge" value="2592000"/>
</bean>
3、FormAuthenticationFilter配置
修改formAuthenticationFitler添加页面中“记住我checkbox”的input名称:
<!-- 自定义form认证过虑器 -->
<!-- 基于Form表单的身份验证过滤器,不配置将也会注册此过虑器,表单中的用户账号、密码及loginurl将采用默认值,建议配置 -->
<bean id="formAuthenticationFilter" class="com.lhx.ssm.shiro.CustomFormAuthenticationFilter">
<!-- 表单中账号的input名称 -->
<property name="usernameParam" value="username"/>
<!-- 表单中密码的input名称 -->
<property name="passwordParam" value="password"/>
<!-- 记住我input的名称 -->
<property name="rememberMeParam" value="rememberMe"/>
</bean>
4、登录页面
在login.jsp中添加“记住我”checkbox
<TR>
<TD></TD>
<TD><input type="checkbox" name="rememberMe" />自动登陆</TD>
</TR>
009-shiro与spring web项目整合【三】验证码、记住我的更多相关文章
- 007-shiro与spring web项目整合【一】基础搭建
一.需求 将原来基于url的工程改成使用shiro实现 二.代码 https://github.com/bjlhx15/shiro.git 中的permission_shiro 三.去除原项目拦截器 ...
- 008-shiro与spring web项目整合【二】认证、授权、session管理
一.认证 1.添加凭证匹配器 添加凭证匹配器实现md5加密校验. 修改applicationContext-shiro.xml: <!-- realm --> <bean id=&q ...
- 010-shiro与spring web项目整合【四】缓存Ehcache
一.Ehcache shiro每次授权都会通过realm获取权限信息,为了提高访问速度需要添加缓存,第一次从realm中读取权限数据,之后不再读取,这里Shiro和Ehcache整合. 1.添加Ehc ...
- (转)shiro权限框架详解06-shiro与web项目整合(下)
http://blog.csdn.net/facekbook/article/details/54962975 shiro和web项目整合,实现类似真实项目的应用 web项目中认证 web项目中授权 ...
- (转) shiro权限框架详解06-shiro与web项目整合(上)
http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...
- spring web项目中整合netty, akka
spring web项目中整合netty, akka 本身的web项目仍然使用tomcat/jetty8080端口, 在org.springframework.beans.factory.Initia ...
- Spring与Web项目整合的原理
引言: 在刚开始我们接触IOC时,我们加载并启用SpringIOC是通过如下代码手动加载 applicationContext.xml 文件,new出context对象,完成Bean的创建和属性的注入 ...
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
- 实战突击: Java Web项目整合开发(PDF)
实战突击: Java Web项目整合开发(PDF)
随机推荐
- Selenium操作之滚动条
在用Selenium做UI自动化时,经常会遇到有些元素找不到之类的问题,但是自己的代码并没有错,元素就是找不到,这是为什么呢?原因很简单,由于页面内容较多,有些内容需要下拉滚动条才会显示,这里介绍一种 ...
- Python3.0+Selenium3进行Web自动化遇到的坑
1.搭建环境时,已经把chromedriver的路径加入到PATH,但是还是报错说需要加入PATH.此时重新启动下Pycharm即可
- 跟着百度学PHP[14]-PDO-优化驱动
使用方法设置预定义变量 PDO的方法/属性 PDO::beginTransaction — Initiates a transaction PDO::commit — Commits a transa ...
- CentOS6.5下Apache防止目录遍历
原先以为CentOS下的Apache应该是默认关闭目录遍历的... 然后拿自己网站试了一下发现想太多...汗 就去改下Apache的配置 首先Apache的配置文件在 /etc/httpd/conf/ ...
- cocos2dx位图字体bitmapfont使用
参考此文,http://www.cocos2dres.com/post/87.html 在cocosbuilder里使用时有几个注意事项 1.中文保存时选择unicode 2.导出时选择 text 3 ...
- 信号signal编号及意义及一般处理
SIGQUIT:停止 SIGILL:illegal instruction SIGABRT:Abort SIGFPE:Float point exception SIGPIPE:Broken pipe ...
- 11 jsp脚本调用java代码
大多数情况下, jsp 文档的大部分由静态文本(html)构成, 为处理该页面而创建的 servlet 只是将它们原封不动的传递给客户端, 原封不动的传送给客户端有两个小例外: 1. 如果想传送 &l ...
- Window对应的类为java.awt.Windows, 它可独立于其他Container而存在
Window对应的类为java.awt.Windows, 它可独立于其他Container而存在,它有两个子类, Frame和Dialog, Frame是具有标题(title)和可伸缩的角(resiz ...
- php源码,php网站源码,php源码下载
网址:http://www.aspku.com/php/ 有时间,可以研究研究.
- Machine Learning Yearning - Andrew NG
链接(1~12章): https://gallery.mailchimp.com/dc3a7ef4d750c0abfc19202a3/files/Machine_Learning_Yearning_V ...