图形验证码
分类: springboot 专栏: 在线教育项目实战 标签: 图形验证码
2023-05-19 19:10:00 571浏览
图形验证码
依赖
<!-- google kaptcha依赖 --> <dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency>
配置类
@Configuration public class CaptchaConfig { @Bean 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; } }
生成验证码controller
@RestController public class CaptchaController { @Resource private DefaultKaptcha defaultKaptcha; @ApiOperation(value = "验证码") @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 -------------------------- } }
校验验证码
String captcha = (String) request.getSession().getAttribute("captcha"); if (StringUtils.isEmpty(captcha)) { return ResultDto.error("系统生成验证码失败"); } if (StringUtils.isEmpty(userVo.getCode())||!captcha.equalsIgnoreCase(userVo.getCode())){ return ResultDto.error("验证码输入错误,请重新输入!"); }
前端页面
<el-form-item prop="code"> <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="点击图片更换验证码" style="width: 250px;margin-right: 5px"></el-input> <img :src="captchaUrl" @click="updateCaptcha"> </el-form-item> //data数据模型 captchaUrl: process.env.BASE_API+'captcha?time=' + new Date(), // 函数 updateCaptcha() { this.captchaUrl = process.env.BASE_API+'captcha?time=' + new Date() },
前后分离项目的话,建议图形验证码存redis里。
md5加密工具类
public class MD5Util { private static final String SALT = "jf3q"; public static String encode(String password) { password = password + SALT; MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { throw new RuntimeException(e); } char[] charArray = password.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } }
好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
他的专栏
他感兴趣的技术