<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>cn.apiclub.tool</groupId>
<artifactId>simplecaptcha</artifactId>
<version>1.2.2</version>
</dependency>
spring.redis.database=4 spring.redis.host=hostname spring.redis.password=password spring.redis.port=6379 spring.redis.timeout=2000 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1
package com.xiaofangtech.sun.config;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory();
}
@Bean RedisTemplate<String, String>redisTemplate(RedisConnectionFactory factory)
{
RedisTemplate<String, String> template = new RedisTemplate<String, String>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}
@Autowired
private RedisTemplate<String, String> redisTemplate;
//将验证码以<key,value>形式缓存到redis
redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS);
package com.xiaofangtech.sunt.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer;
@RestController
@RequestMapping("captcha")
public class CaptchaModule {
@Autowired
private RedisTemplate<String, String> redisTemplate;
private static int captchaExpires = 3*60; //超时时间3min
private static int captchaW = 200;
private static int captchaH = 60;
@RequestMapping(value = "getcaptcha", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public @ResponseBody byte[] getCaptcha(HttpServletResponse response)
{
//生成验证码
String uuid = UUID.randomUUID().toString();
Captcha captcha = new Captcha.Builder(captchaW, captchaH)
.addText().addBackground(new GradiatedBackgroundProducer())
.gimp(new FishEyeGimpyRenderer())
.build();
//将验证码以<key,value>形式缓存到redis
redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS);
//将验证码key,及验证码的图片返回
Cookie cookie = new Cookie("CaptchaCode",uuid);
response.addCookie(cookie);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
try {
ImageIO.write(captcha.getImage(), "png", bao);
return bao.toByteArray();
} catch (IOException e) {
return null;
}
}
}
@Autowired
private RedisTemplate<String, String> redisTemplate;
//验证码校验在后面章节添加
String captchaCode = loginPara.getCaptchaCode();
try {
if (captchaCode == null)
{
throw new Exception();
}
String captchaValue = redisTemplate.opsForValue().get(captchaCode);
if (captchaValue == null)
{
throw new Exception();
}
redisTemplate.delete(captchaCode);
if (captchaValue.compareTo(loginPara.getCaptchaValue()) != 0)
{
throw new Exception();
}
} catch (Exception e) {
resultMsg = new ResultMsg(ResultStatusCode.INVALID_CAPTCHA.getErrcode(),
ResultStatusCode.INVALID_CAPTCHA.getErrmsg(), null);
return resultMsg;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有