1.pom.xml中添加Cage依赖。

    <dependency>
<groupId>com.github.cage</groupId>
<artifactId>cage</artifactId>
<version>1.0</version>
</dependency>

项目相关资料:https://akiraly.github.io/cage/quickstart.html 。

2.Controller:@RestController


@RestController
@RequestMapping("captcha")
public class CaptchaController { @Autowired
private CaptchaService captchaService; @RequestMapping("get")
public void get(HttpServletResponse response,String key) throws IOException {
response.setContentType("image/jpeg");//设置响应的媒体类型,这样浏览器会识别出响应的是图片
response.getOutputStream().write(captchaService.getCaptcha(key));
response.flushBuffer();
}
}

3.Service:

@Service("captchaService")
public class CaptchaService { private static final Logger log = LoggerFactory.getLogger(CaptchaService.class); @Autowired
RedisDao redisDao;
Cage cage = new GCage(); public byte[] getCaptcha(String id) {
if (StringUtils.isBlank(id)) {
return null;
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
String token = cage.getTokenGenerator().next().substring(0, 4);
try {
cage.draw(token, os);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
String key = "captcha-" + id;
redisDao.getJredis().opsForValue().set(key, token);
redisDao.getJredis().expire(key, 1, TimeUnit.HOURS);
return os.toByteArray();
} public boolean matchCaptcha(String id, String captcha) {
if (StringUtils.isBlank(id) || StringUtils.isBlank(captcha)) {
return false;
}
String key = "captcha-" + id;
String redisCaptcha = String.valueOf(redisDao.getJredis().opsForValue().get(key));
if (StringUtils.isBlank(redisCaptcha)) {
return false;
}
log.info(id + ", " + captcha + ", " + redisCaptcha);
return StringUtils.equalsIgnoreCase(captcha, redisCaptcha);
}
}

4.前端页面:

$('#yzmimg').attr('src','https://localhost:8082/captcha/get?key'+timestamp);

总结:设置

response.setContentType("image/jpeg"),这样返回时将会以图片形式,此处为坑。

Spring中整合Cage,实现验证码功能的更多相关文章

  1. spring中整合memcached,以及创建memcache的put和get方法

    spring中整合memcached,以及创建memcache的put和get方法: 1:在项目中导入memcache相关的jar包 2:memcache在spring.xml的配置: 代码: < ...

  2. Spring中整合Titles

    在<Spriing实战(第三版)>这本书中,有一个使用titles的例子,但是这是一个不完整的例子.那么要参照起来就比较难了,于是找到了下面这篇博客. 在Spring中使用tiles2 ( ...

  3. 利用cglib包实现Spring中aop的<aop:advisor>功能

    一:前言 还有<aop:before>/<aop:after>/<aop:around>的没有实现,不过根<aop:advisor>是差不多的,就是要额 ...

  4. S2SH框架中的无刷新验证码功能实现

    暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下: 1 package com.sem.action; 2 3 import j ...

  5. 玩转Spring MVC(五)----在spring中整合log4j

    在前边的基础上,本文主要总结一下如何在spring 中配置log4j,在本文末尾会给出完整项目的链接. 首先是web.xml中要新添加的代码: <!-- 6. 配置log4j --> &l ...

  6. SpringBoot学习:整合shiro(验证码功能和登录次数限制功能)

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 (一)验证码 首先login.jsp里增加了获取验证码图片的标签: <body s ...

  7. spring中整合ssm框架注解版

    和xml版差不多,只不过创建对象的方式是由spring自动扫描包名,然后命名空间多一行context代码在application.xml中,然后将每个对象通过注解创建和注入: 直接上代码: 1.use ...

  8. SpringMVC整合kaptcha(验证码功能)

    一.依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha& ...

  9. SpringMVC 整合 kaptcha(验证码功能)

    一.添加依赖 <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptch ...

随机推荐

  1. Apache虚拟主机实战

    [root@localhost ~]# wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.2.32.tar.bz2  [root@localho ...

  2. Python内置函数(64)——classmethod

    英文文档: classmethod(function) Return a class method for function. A class method receives the class as ...

  3. Python-基础学习-Day1

    1 Python介绍 1.1 Python 是一门什么样的语言? python是一门动态解释性的强类型定义语言. 编译型的特点:可一致性差,运行速度快. 解释型的特点:边执行边解释,速度慢 1.2 P ...

  4. Linux实战案例(5)关闭Centos的防火墙

    1.检查防火墙的状态 [root@LxfN1 ~]# service iptables status表格:filterChain INPUT (policy ACCEPT)num target pro ...

  5. python与mongodb的交互 增删改差

    首先引入包: pip install pymongo需要用到如下对象: MongoClient对象:用于与MongoDB服务器建立连接 client=MongoClient('主机ip',端口) Da ...

  6. Python之格式化输出,初始编码以及运算符

    一.题型 1.使用while循环输入 1 2 3 4 5 6  8 9 10 count = 0 while count < 10: count += 1   #count = count + ...

  7. 基于JWT标准的用户认证接口实现

    前面的话 实现用户登录认证的方式常见的有两种:一种是基于 cookie 的认证,另外一种是基于 token 的认证 .本文以基于cookie的认证为参照,详细介绍JWT标准,并实现基于该标签的用户认证 ...

  8. Hadoop:读取hdfs上zip压缩包并解压到hdfs的实现代码

    背景: 目前工作中遇到一大批的数据,如果不压缩直接上传到ftp上就会遇到ftp空间资源不足问题,没办法只能压缩后上传,上穿完成后在linux上下载.但是linux客户端的资源只有20G左右一个压缩包解 ...

  9. CSS Box Model 盒子模型

    1. 介绍 1.1 什么是 Box Model 在HTML中的每个element(元素)都可以看作一个矩形的盒子,矩形从内到外依次由元素的内容(content).内边距(padding).边框(bor ...

  10. 一张图片快速明白Python概述