java 验证码 maven_java验证码接口

1.maven依赖

com.github.penggle

kaptcha

2.3.2

2.配置

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

import com.google.code.kaptcha.util.Config;

import org.springframework.context.annotation.Bean;

import org.springframework.stereotype.Component;

import java.util.Properties;

@Component

public class KaptchaConfig {

@Bean

public DefaultKaptcha getDefaultKaptcha() {

DefaultKaptcha defaultKaptcha = new DefaultKaptcha();

Properties properties = new Properties();

// 图片边框

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

// 边框颜色

properties.setProperty("kaptcha.border.color", "105,179,90");

// 字体颜色

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

// 图片宽

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

// 图片高

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

// 字体大小

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

// session key

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

// 验证码长度

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

// 文字间距

properties.put("kaptcha.textproducer.char.space", "7");

// 字体

properties.put("kaptcha.textproducer.font.names", "Arial,cmr10");

// 采用那些文字

properties.put("kaptcha.textproducer.char.string", "ABCDEFGHJKLMNPQRSTUVWXYZ13456789abcdeghjknsuvwxyz");

Config config = new Config(properties);

defaultKaptcha.setConfig(config);

return defaultKaptcha;

}

}

3.接口

@Controller

@RequestMapping("kaptcha")

@Slf4j

@Api(tags = {"验证码生成接口"})

public class KaptchaController {

/**

* 1、验证码工具

*/

@Autowired

DefaultKaptcha defaultKaptcha;

/**

* 生成验证码

* @param request

* @param response

* @throws Exception

*/

@ApiOperation(value = "生成验证码")

@GetMapping("generateKaptcha")

public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response)

throws Exception {

byte[] captchaChallengeAsJpeg = null;

ByteArrayOutputStream jpegOutputStream = null;

ServletOutputStream responseOutputStream = null;

try {

jpegOutputStream = new ByteArrayOutputStream();

// 生产验证码字符串并保存到session中

String createText = defaultKaptcha.createText();

log.info("sessionId={},验证码={}", request.getSession().getId(), createText);

request.getSession().setAttribute(Constant.VERIFY_CODE, createText);

// 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中

BufferedImage challenge = defaultKaptcha.createImage(createText);

ImageIO.write(challenge, "jpg", jpegOutputStream);

// 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组

captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

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

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

response.setDateHeader("Expires", 0);

response.setContentType("image/jpeg");

responseOutputStream = response.getOutputStream();

responseOutputStream.write(captchaChallengeAsJpeg);

} catch (IllegalArgumentException e) {

response.sendError(HttpServletResponse.SC_NOT_FOUND);

return;

} finally {

IOUtils.closeQuietly(jpegOutputStream);

IOUtils.closeQuietly(responseOutputStream);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值