Spring Boot引入第三方工具EasyCaptcha生成图形验证码(包含中文验证码和算数验证码)

目录

1.简介

2.maven方式引入 

3.后端代码

4.前端使用ajax获取验证码:

6.验证码效果


1.简介

 EasyCaptcha,Java图形验证码,支持gif、中文、算术等类型,可用于Java Web、JavaSE等项目。

2.maven方式引入 

 <!-- 验证码 -->
<dependencies>
   <dependency>
      <groupId>com.github.whvcse</groupId>
      <artifactId>easy-captcha</artifactId>
      <version>1.6.2</version>
   </dependency>
</dependencies>

3.后端代码

前后端分离项目建议不要存储在session中,存储在redis中,redis存储需要一个key,key一同返回给前端用于验证输入

@Controller
public class CaptchaController {
    @Autowired
    private RedisUtil redisUtil;
    
    @ResponseBody
    @RequestMapping("/captcha")
    public JsonResult captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
         //动态验证码
        GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
        //静态验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);
        //中文验证码
        ChineseCaptcha chineseCaptchaAbstract = new ChineseCaptcha(130,28,4);
        //算术验证码
        ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(130 , 28 , 4);

        String verCode = specCaptcha.text().toLowerCase();
        String key = UUID.randomUUID().toString();
        // 存入redis并设置过期时间为30分钟
        redisUtil.setEx(key, verCode, 30, TimeUnit.MINUTES);
        // 将key和base64返回给前端
        return JsonResult.ok().put("key", key).put("image", specCaptcha.toBase64());
    }
    
    @ResponseBody
    @PostMapping("/login")
    public JsonResult login(String username,String password,String verCode,String verKey){
        // 获取redis中的验证码
        String redisCode = redisUtil.get(verKey);
        // 判断验证码
        if (verCode==null || !redisCode.equals(verCode.trim().toLowerCase())) {
            return JsonResult.error("验证码不正确");
        }
    }  
}

4.前端使用ajax获取验证码:

<img id="verImg" width="130px" height="48px"/>
 
<script>
    var verKey;
    // 获取验证码
    $.get('/captcha', function(res) {
        verKey = res.key;
        $('#verImg').attr('src', res.image);
    },'json');
    
    // 登录
    $.post('/login', {
        verKey: verKey,
        verCode: '8u6h',
        username: 'admin',
        password: 'admin'
    }, function(res) {
        console.log(res);
    }, 'json');
</script>

6.验证码效果

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Spring Boot生成验证码,可以使用 Google 的开源项目 Kaptcha。以下是使用 Kaptcha 生成验证码的步骤: 1. 在 pom.xml 中添加以下依赖: ```xml <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 2. 创建一个 Controller 类,添加一个生成验证码的接口,代码如下: ```java @RestController public class CaptchaController { @RequestMapping("/captcha") public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception { // 创建 Kaptcha 对象 DefaultKaptcha kaptcha = new DefaultKaptcha(); // 设置配置 Properties properties = new Properties(); // 图片边框,合法值:yes、no properties.setProperty("kaptcha.border", "yes"); // 边框颜色,合法值:r,g,b (and optional alpha) 或者 white、black、blue properties.setProperty("kaptcha.border.color", "105,179,90"); // 字体颜色,合法值:r,g,b 或者 white、black、blue properties.setProperty("kaptcha.textproducer.font.color", "blue"); // 图片宽 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); kaptcha.setConfig(config); // 生成验证码 String text = kaptcha.createText(); // 将验证码存入 session request.getSession().setAttribute("code", text); // 生成图片 BufferedImage image = kaptcha.createImage(text); // 将图片输出给客户端 ImageIO.write(image, "jpg", response.getOutputStream()); } } ``` 3. 在前端页面中,使用 Uniapp 的 `image` 组件来显示验证码图片,代码如下: ```html <template> <view> <image :src="captchaUrl" @click="refreshCaptcha" mode="widthFix"></image> <input type="text" v-model="code" placeholder="请输入验证码"> </view> </template> <script> export default { data() { return { captchaUrl: '/captcha', code: '' }; }, methods: { refreshCaptcha() { this.captchaUrl = '/captcha?t=' + new Date().getTime(); } } }; </script> ``` 在这个例子中,我们使用了 `t` 参数来强制刷新验证码图片,以确保每次刷新都会生成新的验证码。当用户输入验证码后,我们可以将其与后端生成验证码进行比较,以进行验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

superboy@.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值