springboot实现图片验证码之kaptcha框架
springboot实现图片验证码之kaptcha框架
1.添加依赖
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2.编写配置
新建KaptchaConfig.java文件,进行相关参数配置
package com.example.demo.config;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfig {
/**
* 验证码配置
* @return
*/
@Bean
public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
Properties properties=new Properties();
//是否有边框
properties.setProperty(Constants.KAPTCHA_BORDER,"yes");
//验证码文本颜色
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR,"red");
//验证码图片宽度
properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH,"160");
//验证码图片高度
properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT,"60");
//文本字符大小
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE,"38");
//验证码session的值
properties.setProperty(Constants.KAPTCHA_SESSION_CONFIG_KEY,"kaptchaCode");
//验证码文本长度
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH,"4");
//字体
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋体,楷体,微软雅黑");
Config config=new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3.实现接口
package com.example.demo.controller;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
@RestController
@RequestMapping("/api")
public class KaptchaController {
@Autowired
DefaultKaptcha defaultKaptcha;
@GetMapping("/getCode")
public void getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String text = defaultKaptcha.createText();
//保存验证码
request.getSession().setAttribute("vrifyCode",text);
BufferedImage image=defaultKaptcha.createImage(text);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image,"jpg",out);
out.flush();
out.close();
}
@GetMapping("/vrifyCode")
public String vrifyCode(HttpServletRequest request, String code){
String vrifyCode= (String) request.getSession().getAttribute("vrifyCode");
if (vrifyCode==null){
return null;
}
if (!vrifyCode.equals(code)){
return "错误的验证码";
}else{
return "验证成功";
}
}
}
4.接口验证
使用postman工具进行验证。
- 获取验证码
http://localhost:8080/api/getCode
- 校验验证码
http://localhost:8080/api/vrifyCode
通过Postman获取验证码
通过Postman验证验证码-成功
通过Postman验证验证码-失败