目录贴:跟我学Shiro目录贴

在做用户登录功能时,很多时候都需要验证码支持,验证码的目的是为了防止机器人模拟真实用户登录而恶意访问,如暴力破解用户密码/恶意评论等。目前也有一些验证码比较简单,通过一些OCR工具就可以解析出来;另外还有一些验证码比较复杂(一般通过如扭曲、加线条/噪点等干扰)防止OCR工具识别;但是在中国就是人多,机器干不了的可以交给人来完成,所以在中国就有很多打码平台,人工识别验证码;因此即使比较复杂的如填字、算数等类型的验证码还是能识别的。所以验证码也不是绝对可靠的,目前比较可靠还是手机验证码,但是对于用户来说相对于验证码还是比较麻烦的。

对于验证码图片的生成,可以自己通过如Java提供的图像API自己去生成,也可以借助如JCaptcha这种开源Java类库生成验证码图片;JCaptcha提供了常见的如扭曲、加噪点等干扰支持。本章代码基于《第十六章 综合实例》。

一、添加JCaptcha依赖

  1. <dependency>
  2. <groupId>com.octo.captcha</groupId>
  3. <artifactId>jcaptcha</artifactId>
  4. <version>2.0-alpha-1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.octo.captcha</groupId>
  8. <artifactId>jcaptcha-integration-simple-servlet</artifactId>
  9. <version>2.0-alpha-1</version>
  10. <exclusions>
  11. <exclusion>
  12. <artifactId>servlet-api</artifactId>
  13. <groupId>javax.servlet</groupId>
  14. </exclusion>
  15. </exclusions>
  16. </dependency>

com.octo.captcha . jcaptcha 提供了jcaptcha 核心;而jcaptcha-integration-simple-servlet提供了与Servlet集成。

二、GMailEngine

来自https://code.google.com/p/musicvalley/source/browse/trunk/musicvalley/doc/springSecurity/springSecurityIII/src/main/java/com/spring/security/jcaptcha/GMailEngine.java?spec=svn447&r=447(目前无法访问了),仿照JCaptcha2.0编写类似GMail验证码的样式;具体请参考com.github.zhangkaitao.shiro.chapter22.jcaptcha.GMailEngine。

三、MyManageableImageCaptchaService

提供了判断仓库中是否有相应的验证码存在。

  1. public class MyManageableImageCaptchaService extends
  2. DefaultManageableImageCaptchaService {
  3. public MyManageableImageCaptchaService(
  4. com.octo.captcha.service.captchastore.CaptchaStore captchaStore,
  5. com.octo.captcha.engine.CaptchaEngine captchaEngine,
  6. int minGuarantedStorageDelayInSeconds,
  7. int maxCaptchaStoreSize,
  8. int captchaStoreLoadBeforeGarbageCollection) {
  9. super(captchaStore, captchaEngine, minGuarantedStorageDelayInSeconds,
  10. maxCaptchaStoreSize, captchaStoreLoadBeforeGarbageCollection);
  11. }
  12. public boolean hasCapcha(String id, String userCaptchaResponse) {
  13. return store.getCaptcha(id).validateResponse(userCaptchaResponse);
  14. }
  15. }

四、JCaptcha工具类

提供相应的API来验证当前请求输入的验证码是否正确。

  1. public class JCaptcha {
  2. public static final MyManageableImageCaptchaService captchaService
  3. = new MyManageableImageCaptchaService(new FastHashMapCaptchaStore(),
  4. new GMailEngine(), 180, 100000, 75000);
  5. public static boolean validateResponse(
  6. HttpServletRequest request, String userCaptchaResponse) {
  7. if (request.getSession(false) == null) return false;
  8. boolean validated = false;
  9. try {
  10. String id = request.getSession().getId();
  11. validated =
  12. captchaService.validateResponseForID(id, userCaptchaResponse)
  13. .booleanValue();
  14. } catch (CaptchaServiceException e) {
  15. e.printStackTrace();
  16. }
  17. return validated;
  18. }
  19. public static boolean hasCaptcha(
  20. HttpServletRequest request, String userCaptchaResponse) {
  21. if (request.getSession(false) == null) return false;
  22. boolean validated = false;
  23. try {
  24. String id = request.getSession().getId();
  25. validated = captchaService.hasCapcha(id, userCaptchaResponse);
  26. } catch (CaptchaServiceException e) {
  27. e.printStackTrace();
  28. }
  29. return validated;
  30. }
  31. }

validateResponse():验证当前请求输入的验证码否正确;并从CaptchaService中删除已经生成的验证码;

hasCaptcha():验证当前请求输入的验证码是否正确;但不从CaptchaService中删除已经生成的验证码(比如Ajax验证时可以使用,防止多次生成验证码);

五、JCaptchaFilter

用于生成验证码图片的过滤器。

  1. public class JCaptchaFilter extends OncePerRequestFilter {
  2. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  3. response.setDateHeader("Expires", 0L);
  4. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  5. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  6. response.setHeader("Pragma", "no-cache");
  7. response.setContentType("image/jpeg");
  8. String id = request.getRequestedSessionId();
  9. BufferedImage bi = JCaptcha.captchaService.getImageChallengeForID(id);
  10. ServletOutputStream out = response.getOutputStream();
  11. ImageIO.write(bi, "jpg", out);
  12. try {
  13. out.flush();
  14. } finally {
  15. out.close();
  16. }
  17. }
  18. }

CaptchaService使用当前会话ID当作key获取相应的验证码图片;另外需要设置响应内容不进行浏览器端缓存。

  1. <!-- 验证码过滤器需要放到Shiro之后 因为Shiro将包装HttpSession 如果不,可能造成两次的sesison id 不一样 -->
  2. <filter>
  3. <filter-name>JCaptchaFilter</filter-name>
  4. <filter-class>
  5. com.github.zhangkaitao.shiro.chapter22.jcaptcha.JCaptchaFilter
  6. </filter-class>
  7. </filter>
  8. <filter-mapping>
  9. <filter-name>JCaptchaFilter</filter-name>
  10. <url-pattern>/jcaptcha.jpg</url-pattern>
  11. </filter-mapping>

这样就可以在页面使用/jcaptcha.jpg地址显示验证码图片。

六、JCaptchaValidateFilter

用于验证码验证的Shiro过滤器。

  1. public class JCaptchaValidateFilter extends AccessControlFilter {
  2. private boolean jcaptchaEbabled = true;//是否开启验证码支持
  3. private String jcaptchaParam = "jcaptchaCode";//前台提交的验证码参数名
  4. private String failureKeyAttribute = "shiroLoginFailure"; //验证失败后存储到的属性名
  5. public void setJcaptchaEbabled(boolean jcaptchaEbabled) {
  6. this.jcaptchaEbabled = jcaptchaEbabled;
  7. }
  8. public void setJcaptchaParam(String jcaptchaParam) {
  9. this.jcaptchaParam = jcaptchaParam;
  10. }
  11. public void setFailureKeyAttribute(String failureKeyAttribute) {
  12. this.failureKeyAttribute = failureKeyAttribute;
  13. }
  14. protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
  15. //1、设置验证码是否开启属性,页面可以根据该属性来决定是否显示验证码
  16. request.setAttribute("jcaptchaEbabled", jcaptchaEbabled);
  17. HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
  18. //2、判断验证码是否禁用 或不是表单提交(允许访问)
  19. if (jcaptchaEbabled == false || !"post".equalsIgnoreCase(httpServletRequest.getMethod())) {
  20. return true;
  21. }
  22. //3、此时是表单提交,验证验证码是否正确
  23. return JCaptcha.validateResponse(httpServletRequest, httpServletRequest.getParameter(jcaptchaParam));
  24. }
  25. protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
  26. //如果验证码失败了,存储失败key属性
  27. request.setAttribute(failureKeyAttribute, "jCaptcha.error");
  28. return true;
  29. }
  30. }

七、MyFormAuthenticationFilter

用于验证码验证的Shiro拦截器在用于身份认证的拦截器之前运行;但是如果验证码验证拦截器失败了,就不需要进行身份认证拦截器流程了;所以需要修改下如FormAuthenticationFilter身份认证拦截器,当验证码验证失败时不再走身份认证拦截器。

  1. public class MyFormAuthenticationFilter extends FormAuthenticationFilter {
  2. protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
  3. if(request.getAttribute(getFailureKeyAttribute()) != null) {
  4. return true;
  5. }
  6. return super.onAccessDenied(request, response, mappedValue);
  7. }
  8. }

即如果之前已经错了,那直接跳过即可。

八、spring-config-shiro.xml

  1. <!-- 基于Form表单的身份验证过滤器 -->
  2. <bean id="authcFilter"
  3. class="com.github.zhangkaitao.shiro.chapter22.jcaptcha.MyFormAuthenticationFilter">
  4. <property name="usernameParam" value="username"/>
  5. <property name="passwordParam" value="password"/>
  6. <property name="rememberMeParam" value="rememberMe"/>
  7. <property name="failureKeyAttribute" value="shiroLoginFailure"/>
  8. </bean>
  9. <bean id="jCaptchaValidateFilter"
  10. class="com.github.zhangkaitao.shiro.chapter22.jcaptcha.JCaptchaValidateFilter">
  11. <property name="jcaptchaEbabled" value="true"/>
  12. <property name="jcaptchaParam" value="jcaptchaCode"/>
  13. <property name="failureKeyAttribute" value="shiroLoginFailure"/>
  14. </bean>
  15. <!-- Shiro的Web过滤器 -->
  16. <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  17. <property name="securityManager" ref="securityManager"/>
  18. <property name="loginUrl" value="/login"/>
  19. <property name="filters">
  20. <util:map>
  21. <entry key="authc" value-ref="authcFilter"/>
  22. <entry key="sysUser" value-ref="sysUserFilter"/>
  23. <entry key="jCaptchaValidate" value-ref="jCaptchaValidateFilter"/>
  24. </util:map>
  25. </property>
  26. <property name="filterChainDefinitions">
  27. <value>
  28. /static/** = anon
  29. /jcaptcha* = anon
  30. /login = jCaptchaValidate,authc
  31. /logout = logout
  32. /authenticated = authc
  33. /** = user,sysUser
  34. </value>
  35. </property>
  36. </bean>

九、login.jsp登录页面

  1. <c:if test="${jcaptchaEbabled}">
  2. 验证码:
  3. <input type="text" name="jcaptchaCode">
  4. <img class="jcaptcha-btn jcaptcha-img"
  5. src="${pageContext.request.contextPath}/jcaptcha.jpg" title="点击更换验证码">
  6. <a class="jcaptcha-btn" href="javascript:;">换一张</a>
  7. <br/>
  8. </c:if>

根据jcaptchaEbabled来显示验证码图片。

十、测试

输入http://localhost:8080/chapter22将重定向到登录页面;输入正确的用户名/密码/验证码即可成功登录,如果输入错误的验证码,将显示验证码错误页面:

示例源代码:https://github.com/zhangkaitao/shiro-example;可加群 231889722 探讨Spring/Shiro技术。

第二十二章 集成验证码——《跟我学Shiro》的更多相关文章

  1. Gradle 1.12用户指南翻译——第二十二章. 标准的 Gradle 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  2. 《Linux命令行与shell脚本编程大全》 第二十二章 学习笔记

    第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系后代,ash shell是Unix系统上原来地Bourne shell的简化版本 ...

  3. 第二十二章 Django会话与表单验证

    第二十二章 Django会话与表单验证 第一课 模板回顾 1.基本操作 def func(req): return render(req,'index.html',{'val':[1,2,3...]} ...

  4. “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. “全栈2019”Java异常第二十二章:try-with-resources语句详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  6. “全栈2019”Java第二十二章:控制流程语句中的决策语句if-else

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. python 教程 第二十二章、 其它应用

    第二十二章. 其它应用 1)    Web服务 ##代码 s 000063.SZ ##开盘 o 26.60 ##最高 h 27.05 ##最低 g 26.52 ##最新 l1 26.66 ##涨跌 c ...

  8. 第二十二章 跳出循环-shift参数左移-函数的使用 随堂笔记

    第二十二章 跳出循环-shift参数左移-函数的使用 本节所讲内容: 22.1 跳出循环 22.2 Shift参数左移指令 22.3 函数的使用 22.4 实战-自动备份mysql数据库和nginx服 ...

  9. 20190925 On Java8 第二十二章 枚举

    第二十二章 枚举 基本 enum 特性 创建 enum 时,编译器会为你生成一个相关的类,这个类继承自 Java.lang.Enum. valueOf() 是在 Enum 中定义的 static 方法 ...

随机推荐

  1. Codeforces 380E Sereja and Dividing

    题面 洛谷传送门 题解 博客 有精度要求所以只用求几十次就差不多了 CODE #include <bits/stdc++.h> using namespace std; typedef l ...

  2. 动态 DP 总结

    目录 例题1:模拟赛题 代码: 例题2 例题3:带修改树上最大独立集. 代码: 注:部分参考 https://www.luogu.org/blog/gkxx-is-here/what-the-hell ...

  3. 2019ICPC南京自我反省

    第一场ICPC,跟第一场CCPC一样,可惜真的可惜. 打完比赛就感觉难受,难受不在于又抱了块铜牌,而是那种能出的题没出的可惜感非常浓重. 开场还是可以的,通过一阵讨论,就大胆猜测了A的规律,然后一发过 ...

  4. linux系列(十四):head命令

    1.命令格式: head [参数] [文件] 2.命令功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行. 3.命令参数: -q 隐藏文件名 -v 显示文件名 ...

  5. Angular2日期格式化

    一:组件日期格式化: ts中调用: import {DatePipe} from "@angular/common"; @Component({     providers: [D ...

  6. 配置interfaces

    demo1 # This file describes the network interfaces available on your system # and how to activate th ...

  7. 如何利用awk累加第一列的值?

    以下是一个五行文件的例子: 1.[root@master yjt]# seq 5 |awk 'BEGIN{sum=0;print "总和:"}{if(NR<=4)printf ...

  8. 已安装gcc编译器,但./configure还是提示找不到编译器(分析)

    1.编译nginx前, ./configure检查提示找不到C编译器 [root@test nginx-]# ./configure checking for OS + Linux -.el7.x86 ...

  9. Mac之Sublime Text使用Go

    安装Golang build 包 点击 Preferences > Package control 菜单(MAC快捷键 shift + command + p) 在弹出的输入框输入 instal ...

  10. Git git rm和git rm --cached

    git rm 和 git rm --cached 的区别 git rm file git commit -m "xxx" git push origin master 删除本地及仓 ...