springboot中如何使用图片验证码

1.先在pom中引入kaptcha依赖

<!-- 引入图片验证依赖https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

2.配置资源文件(yml),这些样式都是可以调节的!

#配置图片验证码的样式
kaptcha:
  border: 'no'
  textproducer:
    font:
      color: red
      size: 43
      names: Arial
    char:
      string: abcdef123
      length: 4
  image:
    width: 135
    height: 50
  noise:
    color: black

3.在config包下面创建kaptcha配置类

package com.kaptcha.config;

import com.google.code.kaptcha.servlet.KaptchaServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletException;

/**
 *图片验证码配置类
 */
@Configuration //加入配置注解,springboot会自动扫描,作为配置文件
public class KaptchaConfig {
    @Value("${kaptcha.border}")
    private String border;

    @Value("${kaptcha.textproducer.font.color}")
    private String fcolor;

    @Value("${kaptcha.image.width}")
    private String width;

    @Value("${kaptcha.textproducer.char.string}")
    private String cString;

    @Value("${kaptcha.image.height}")
    private String height;

    @Value("${kaptcha.textproducer.font.size}")
    private String fsize;

    @Value("${kaptcha.noise.color}")
    private String nColor;

    @Value("${kaptcha.textproducer.char.length}")
    private String clength;

    @Value("${kaptcha.textproducer.font.names}")
    private String fnames;

    @Bean //表明向容器中注册servletbean,这个bean的泛型就是KaptchaServlet
    public ServletRegistrationBean<KaptchaServlet> servletRegistrationBean() throws ServletException {
        //这个注册的servletbena需要一个处理请求的类就是KaptchaServlet,并且需要一个url映射拦截所有的/kaptcha请求,这样就可以处理前端页面的请求
        ServletRegistrationBean<KaptchaServlet> servlet = new ServletRegistrationBean<KaptchaServlet>(new KaptchaServlet(), "/kaptcha");
        //为这个注册servletbean,添加初始化的参数
        servlet.addInitParameter("kaptcha.border", border);// 无边框
        servlet.addInitParameter("kaptcha.textproducer.font.color", fcolor); // 字体颜色
        servlet.addInitParameter("kaptcha.image.width", width);// 图片宽度
        servlet.addInitParameter("kaptcha.textproducer.char.string", cString);// 使用哪些字符生成验证码
        servlet.addInitParameter("kaptcha.image.height", height);// 图片高度
        servlet.addInitParameter("kaptcha.textproducer.font.size", fsize);// 字体大小
        servlet.addInitParameter("kaptcha.noise.color", nColor);// 干扰线的颜色
        servlet.addInitParameter("kaptcha.textproducer.char.length", clength);// 字符个数
        servlet.addInitParameter("kaptcha.textproducer.font.names", fnames);// 字体
        return servlet;
    }
}

4.这样就配置好了,只要再前端页面引入这个img标签,并绑定点击事件,就可以动态刷新验证码了,是不是很神奇赶紧试试吧!

<img th:src="@{/kaptcha}" alt="" @click="imgkaptchachange()"/>
imgkaptchachange(){
    //重新设置img标签的src属性
    //event.target表示获取当前标签
    event.target.src = "/kaptcha?time="+new Date();
}

5.验证码的验证只需要从session中取出,再和请求框里面输入的进行比较就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值