@[TOC]
软件简介
kaptcha 是一个扩展自 simplecaptcha 的验证码库,默认情况下,Kaptcha非常易于设置和使用,并且默认输出会产生一个很难验证的验证码。默认情况下,它生成的验证码看起来与上面的非常相似。如果您想更改输出的外观,则有几个配置选项,并且该框架是模块化的,因此您可以编写自己的变形代码
1.添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>kaptcha-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
2.代码示例
- 配置类
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* @auther:Wangxl
* @Emile:18335844494@163.com
* @Time:2021/8/2 10:47
*/
@Configuration
public class CaptchaConfig {
/**
* 验证码配置
* Kaptcha配置类名
*
* @return
*/
@Bean
@Qualifier("captchaProducer")
public DefaultKaptcha kaptcha() {
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//验证码个数
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
//字体间隔
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE,"8");
//干扰线颜色
properties.setProperty(Constants.KAPTCHA_NOISE_COLOR,"red");
//干扰实现类
properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.DefaultNoise");
// properties.setProperty(Constants.KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
//图片样式
properties.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.WaterRipple");
//文字来源
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
- service
package wxl.top.service;
import wxl.top.util.JsonData;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @auther:Wangxl
* @Emile:18335844494@163.com
* @Time:2021/8/2 11:44
*/
public interface CaptchaService {
void getCaptcha(HttpServletRequest request, HttpServletResponse response);
}
- impl
@Service
public class CaptchaServiceImpl implements CaptchaService {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
private Producer captchaProducer;
@Override
public void getCaptcha(HttpServletRequest request, HttpServletResponse response) {
String captchaText = captchaProducer.createText();
String key = getCaptchaKey(request);
//五分钟过期
redisTemplate.opsForValue().set(key,captchaText,5, TimeUnit.MINUTES);
BufferedImage bufferedImage = captchaProducer.createImage(captchaText);
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
ImageIO.write(bufferedImage,"jpg",outputStream);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String getCaptchaKey(HttpServletRequest httpServletRequest){
String ipAddr = CommonUtil.getIpAddr(httpServletRequest);
String userAgent = httpServletRequest.getHeader("User-Agent");
String key = "user-service:captcha:"+CommonUtil.MD5(ipAddr+userAgent);
return key;
}
- controller
@RestController
@RequestMapping("captcha")
public class CaptchaController {
@Autowired
private CaptchaService captchaService;
@GetMapping(value = "get_captcha")
public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
captchaService.getCaptcha(request,response);
}
}