springboot整合kaptcha生成验证码功能

网友投稿 719 2023-03-22

springboot整合kaptcha生成验证码功能

springboot整合kaptcha生成验证码功能

介绍:kaptcha 是谷歌开源的非常实用的验证码生成工具

一、导入jar包

com.github.penggle

kaptcha

2.3.2

二、编写kaptcha配置

package com.zym.config;

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 {

@Bean

public DefaultKaptcha defaultKaptcha(){

DefaultKaptcha kaptcha = new DefaultKaptcha();

Properties properties = new Properties();

//边框

properties.setProperty("kaptcha.border", "no");

//字体颜色

properties.setProperty("kaptcha.textproducer.font.color", "green");

//图片宽度

properties.setProperty("kaptcha.image.width", "120");

//图片高度

properties.setProperty("kaptcha.image.height", "30");

//字体大小

properties.setProperty("kaptcha.textproducer.font.size", "20");

//session key

properties.setProperty("kaptcha.session.key", "kaptcha");

//验证码长度

properties.setProperty("kaptcha.textproducer.char.length", "4");

//字体

properties.setProperty("kaptcha.textproducer.font.names", "宋体");

//文字间隔

properties.setProperty("kaptcha.textproducer.char.space", "10");

//噪点实现类

properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");

//图片样式-阴影

properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");

Config config = new Config(properties);

kaptcha.setConfig(config);

return kaptcha;

}

}

三、编写接口

package com.zym.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import org.apache.tomcat.util.http.fileupload.IOUtils;

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 javax.servlet.http.HttpSession;

import java.awt.image.BufferedImage;

impoHHboWlvrt java.io.IOException;

@RestController

public class KaptchaController {

@Autowired

private DefaultKaptcha defaultKaptcha;

@GetMapping("/kaptcha")

public void getKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {

//设置响应头

response.setHeader("Cache-Control", "no-cache");

response.setHeader("Pragma", "no-cache");

response.setContentType("image/jpeg");

String text = defaultKaptcha.createText();

HttpSession session = requHHboWlvest.getSession();

//将验证码存入session

session.setAttribute("code", text);

//创建验证码图片

BufferedImage image = defaultKaptcha.createImage(text);

ServletOutputStream os = response.getOutputStream();

ImageIO.write(image, "jpg", os);

IOUtils.closeQuietly(os);

}

}

四、测试接口

使用PostMan:

输入url:localhost:8080/kaptcha

成功得到验证码图片,大功告成!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:app 小程序 插件(小程序插件是什么)
下一篇:vue ui框架(vuejs ui框架)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~