springboot通过kaptcha生成验证码

1.pom.xml文件配置

    <dependency>
        <groupId>com.github.penggle</groupId>
        <artifactId>kaptcha</artifactId>
        <version>2.3.2</version>
    </dependency>

2.新建config

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;
@Componentpublic class KaptchaConfig {
    @Bean    public DefaultKaptcha getDDefaultKaptcha() {        DefaultKaptcha dk = 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", "110");
        // 图片高        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.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        dk.setConfig(config);
        return dk;
    }}

3.创建关于kaptcha验证码插件controller层

import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
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.ByteArrayOutputStream;

@Controller
@RequestMapping("/admin/kaptcha")
public class KaptchaController {
    @Autowired
    @Resource
    private DefaultKaptcha captchaProducer;
    /**
     * 获取验证码
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws Exception
     */
    @GetMapping("/getKaptcha")
    public void getKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // 接受验证码图片输出流
        ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
        try {
            // 生产验证码字符串并保存到session中
            String code = captchaProducer.createText();
            System.out.println(code);
            // 该key值必须和 kaptcha.session.key 配置的相同
            httpServletRequest.getSession().setAttribute("code", code);
            BufferedImage challenge = captchaProducer.createImage(code);
            ImageIO.write(challenge, "jpg", imgOutputStream);
        } catch (Exception e) {
            // 如果出错,返回404
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // 设置返回值
        httpServletResponse.setHeader("Cache-Control", "no-store");
        httpServletResponse.setDateHeader("Expires", 0);
        httpServletResponse.setContentType("image/jpeg");
        // 将图片刷进response
        ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
        responseOutputStream.write(imgOutputStream.toByteArray());
        responseOutputStream.flush();
        responseOutputStream.close();
    }
}

4.前端样式
用的两个style,一个设置左浮动,一个右浮动

 <div class="hr15" style="margin: auto;">
                <input name="yanCode" style="width:60%;float:left;" lay-verify="required" placeholder="验证码"  type="text" class="layui-input">
                <img style="width:35%;float:right;" title="看不清,换一张" class="pointer" th:src="@{/admin/kaptcha/getKaptcha}"
                     onclick="this.src='/admin/kaptcha/getKaptcha?d='+Math.floor(Math.random()*100)">
            </div>

5.关于获取验证码传值
String scode = (String)request.getSession().getAttribute(“code”);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩成笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值