效果如上
1.添加依赖
<!-- 谷歌验证码依赖-->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2.在登录接收参数列表中添加code
@TableField(exist = false)
private String code;//验证码
3.验证码样式配置
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;
/**
* 谷歌图形验证码配置类
* <p>
* Created by HMF on 2021/07/24 21:44
*/
@Configuration // 配置了的@Configuration注解不要忘记了
public class CaptchaConfig {
/**
* 配置直接copy文档的
* DefaultKaptcha 验证码生成器
*
* @return
*/
@Bean // @Bean注解,返回的对象交给IOC处理
public DefaultKaptcha defaultKaptcha() {
// 验证码生成器
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
// 配置
Properties properties = new Properties();
//是否有边框
properties.setProperty("kaptcha.border", "yes");
// 设置边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 边框粗细度,默认为1
// properties.setProperty("kaptcha.border.thickness","1");
// 验证码
properties.setProperty("kaptcha.session.key", "code");
// 验证码文本字符颜色 默认为黑色
properties.setProperty("kaptcha.textproducer.font.color", "blue");
// 设置字体样式
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
// 字体大小,默认40
properties.setProperty("kaptcha.textproducer.font.size", "30");
// 验证码文本字符内容范围 默认为abced2345678gfynmnpwx
// properties.setProperty("kaptcha.textproducer.char.string", "");
// 字符长度,默认为5
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字符间距 默认为2
properties.setProperty("kaptcha.textproducer.char.space", "4");
//验证码图片宽度 默认为200
properties.setProperty("kaptcha.image.width", "100");
// 验证码图片高度 默认为40
properties.setProperty("kaptcha.image.height", "40");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
上面的配置可以根据自己的个人喜好更改
4.验证码请求接口
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 谷歌验证码接口Controller
* <p>
* Created by HMF on 2021/07/24 21:51
*/
@RestController
public class CaptchaController {
@Autowired
private DefaultKaptcha defaultKaptcha;
@GetMapping(value = "/captcha", produces = "image/jpeg")
public void captcha(HttpServletRequest request, HttpServletResponse response) {
// 返回的是一个图形验证码,通过流的方式直接传一个正常的图片过去
// 那么响应头需要做一些处理,这里处理基本上来说固定的
// 定义response输出类型为image/jpeg类型
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
//-------------------生成验证码 begin --------------------------
// 获取验证码文本内容
String text = defaultKaptcha.createText();
System.out.println("验证码内容:" + text);
// 将验证码放入session中
request.getSession().setAttribute("captcha", text);
// 根据文本内容创建图形验证码
BufferedImage image = defaultKaptcha.createImage(text);
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
// 输出流输出图片,格式jpg
ImageIO.write(image, "jpg", outputStream);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//-------------------生成验证码 end ----------------------------
}
}
5.前端请求
data
captchaUrl: '/captcha?time=' + new Date(),
这里做了一个点击事件
// 点击更换验证码
updateCaptcha() {
this.captchaUrl = '/captcha?time=' + new Date();
},
需要注意的是,如果你的项目中设置了登录限制,我们放行的路径需要放上
"/captcha",