Springboot整合kaptcha验证码

Springboot整合kaptcha验证码

01、通过配置类来配置kaptcha

01-01、添加kaptcha的依赖:

<!-- kaptcha验证码 -->
<dependency>
    <groupId>com.github.penggle</groupId>
    <artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

02-01、新建KaptchaConfig配置类

package com.czr.config.code;

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 getDefaultKaptcha(){
        DefaultKaptcha captchaProducer = 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", "blue");
        properties.setProperty("kaptcha.image.width", "110");
        properties.setProperty("kaptcha.image.height", "40");
        properties.setProperty("kaptcha.textproducer.font.size", "30");
        properties.setProperty("kaptcha.session.key", "code");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        captchaProducer.setConfig(config);
        return captchaProducer;

    }
}

验证码 KAPTCHA 参数详解

Constant描述默认值
kaptcha.border图片边框,合法值:yes , noyes
kaptcha.border.color边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.black
kaptcha.border.thickness边框厚度,合法值:>01
kaptcha.image.width图片宽200
kaptcha.image.height图片高50
kaptcha.producer.impl图片实现类com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl文本实现类com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string文本集合,验证码值从此集合中获取abcde2345678gfynmnpwx
kaptcha.textproducer.char.length验证码长度5
kaptcha.textproducer.font.names字体Arial, Courier
kaptcha.textproducer.font.size字体大小40px
kaptcha.textproducer.font.color字体颜色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.textproducer.char.space文字间隔2
kaptcha.noise.impl干扰实现类com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color干扰颜色,合法值: r,g,b 或者 white,black,blue.black
kaptcha.obscurificator.impl图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpycom.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl背景实现类com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from背景颜色渐变,开始颜色light grey
kaptcha.background.clear.to背景颜色渐变,结束颜色white
kaptcha.word.impl文字渲染器com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keysession keyKAPTCHA_SESSION_KEY
kaptcha.session.datesession dateKAPTCHA_SESSION_DATE

03-01、编写controller用于生成验证码:

package com.czr.controller.code;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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;

@Controller
public class CodeController {
    @Autowired
    private Producer captchaProducer ;
    
    @RequestMapping("/kaptcha")
    public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        //生成验证码
        String capText = captchaProducer.createText();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
        //向客户端写出
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}

04-01、访问验证地址查看验证码

http://localhost:8888/kaptcha

关于验证码的业务对接和注意事项

01、验证码的作用是什么?

  • 解决一些程序和肉机攻击的接口的一种防护机制。增加攻击的难度
  • 增加一点安全性。
  • 短信验证码,语音验证码,图片融合验证码等等

02、原理

通过java的awt的图像技术,然后用随机数(字母和数字的混合)然后生成一张图片,用io流输出到浏览器过程。

03、如何实现防护呢?

  • 在生成这个随机数的时候,会在服务器端把生成的随机数放入到session会话中,然后当用户输入验证码的时候,会从session获取比对。

04、登录如何调用验证码的呢?

在这里插入图片描述

04-01、在login.html页面增加验证码
<div data-v-7f5e281c=""  class="container" >
         <!---->
         <input data-v-7f5e281c="" placeholder="验证码" id="code" type="text" maxlength="4" v-model="user.code"  class="input_init container_error mas-form-input-large" />
         <img src="/kaptcha" alt="">
         <div data-v-7f5e281c="" class="slot_content"></div>
         <div data-v-7f5e281c="" class="error_label" style="color: rgb(224, 95, 66);">
          <div data-v-7f5e281c="" class="icon">
           <svg data-v-7f5e281c="" width="1em" height="1em" viewbox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" style="font-size: 12px; transform: rotate(0deg);">
            <path fill-rule="evenodd" clip-rule="evenodd" d="M12.5 7C12.5 10.0376 10.0376 12.5 7 12.5C3.96243 12.5 1.5 10.0376 1.5 7C1.5 3.96243 3.96243 1.5 7 1.5C10.0376 1.5 12.5 3.96243 12.5 7ZM13.5 7C13.5 10.5899 10.5899 13.5 7 13.5C3.41015 13.5 0.5 10.5899 0.5 7C0.5 3.41015 3.41015 0.5 7 0.5C10.5899 0.5 13.5 3.41015 13.5 7ZM6.0995 3.99504C6.04623 3.46229 6.46459 3 7 3C7.53541 3 7.95377 3.46229 7.9005 3.99504L7.54975 7.50248C7.52151 7.78492 7.28384 8 7 8C6.71616 8 6.47849 7.78492 6.45025 7.50249L6.0995 3.99504ZM7 10.9985C6.44706 10.9985 6 10.5554 6 9.99854C6 9.45308 6.44706 8.99854 7 8.99854C7.55294 8.99854 8 9.44172 8 9.99854C8 10.5554 7.55294 10.9985 7 10.9985Z" fill="currentcolor"></path>
           </svg>
          </div>
          <div data-v-7f5e281c="" class="error_message h5">
           请输入验证码
          </div>
         </div>
        </div>
04-02、在login.html页面增加验证码
<img src="/kaptcha" title="看不清,点我改变!!!" onclick="this.src='/kaptcha?d='+new Date().getTime()" alt="">

为什么点击的时候:一定要增加一个随机数或者时间戳呢?

因为:浏览器对图片会进行缓存,因为验证码是一个图片,浏览器默认是根据图片的地址进行缓存的。如果你不加时间戳或者随机数,你点击直接根据地址去缓存中取,所以会拿到缓存图片,就不发生改变。

04-03、增加样式
.ksd-pr{position: relative}
.ksdcode{position: absolute;right:9px;top:6px;border-radius: 12px;}
<div data-v-7f5e281c=""  class="container ksdpr" >
    <input data-v-7f5e281c="" placeholder="验证码" id="code" type="text" maxlength="4" v-model="user.code"  class="input_init container_error mas-form-input-large" />
    <img class="ksdcode" src="/kaptcha" title="看不清,点我改变!!!" onclick="this.src='/kaptcha?d='+new Date().getTime()" alt="">
</div>
04-04、业务对接

login.js修改:

var vue = new Vue({
    el:"#app",
    data:{
        title:"MoMo登录",
        user:{
            account:"10000",
            password:"123456",
            code:""
        }
    },
    methods:{
        toLogin:function(){
            var that = this;
            var user = that.user;

            if(!user.account){
                alert("请输入账号")
                document.getElementById("account").focus();
                return;
            }

            if(!user.password){
                alert("请输入密码")
                document.getElementById("pwd").focus();
                return;
            }

            if(!user.code){
                alert("请输入验证码")
                document.getElementById("code").focus();
                return;
            }

            axios.post("/logined",user).then(function(res){
                 if(res.data == "success"){
                     window.location.href = "/";
                 }else if(res.data=="failcode"){
                     alert("你的验证码输入有误");
                     document.getElementById("code").value = "";
                     document.getElementById("code").focus();
                     document.getElementById("ksdimgcode").src = "/kaptcha?d="+new Date().getTime();
                 }else{
                     alert("输入账号密码有误");
                     document.getElementById("pwd").value = "";
                     document.getElementById("pwd").focus();
                 }
            })
        }
    }
});



后台代码部分:

 /**
     * 处理登录逻辑
     *
     * @param userVo
     * @param session
     * @return
     */
    @ResponseBody
    @PostMapping("/logined")
    public String logined(@RequestBody UserVo userVo, HttpSession session) {
        //1: 根据账号去查询用户信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getTelephone, userVo.getAccount());
        User user = userService.getOne(lambdaQueryWrapper);
        // 2: 如果用户存在直接抛异常
        if (user == null) {
            return "fail";
        }

        // 3: 比较验证码?
        String userinputcode = userVo.getCode();
        String sessioncode = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);

        // 4:采用equalsIgnoreCase忽略大小写比较,不论你输入大写还小写还是全大写还全小写都可以,只要值相同都相同
        boolean codeFlag = userinputcode.equalsIgnoreCase(sessioncode);
        if (!codeFlag) {
            // 如果不相同,直接返回
            return "failcode";
        }

        // 5:把用户输入的密码进行加密
        String inputpwd = MD5Util.md5slat(userVo.getPassword());
        // 6:然后把用户输入的密码进行加密和数据库已经加密的密码进行比较,如果一致,就说明是正确的。
        if (user.getPassword().equals(inputpwd)) {
            // 7: 正确的话,就把登录的用户信息放到session中。
            session.setAttribute(KConstants.SESSION_USER, user);
            return "success";
        }

        // 8:不一致用户密码不存在
        return "fail";
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中整合Kaptcha,你可以按照以下步骤进行操作: 步骤1:添加Kaptcha依赖 在你的Spring Boot项目的pom.xml文件中,添加Kaptcha的依赖: ```xml <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 步骤2:配置Kaptcha 在application.properties(或application.yml)文件中,添加以下配置: ```properties # Kaptcha Configurations kaptcha.border = no kaptcha.border.color = black kaptcha.textproducer.font.color = black kaptcha.image.width = 150 kaptcha.image.height = 50 kaptcha.textproducer.char.string = ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 kaptcha.textproducer.char.length = 4 kaptcha.noise.impl = com.google.code.kaptcha.impl.NoNoise kaptcha.background.clear.from = white kaptcha.background.clear.to = white kaptcha.textproducer.font.size = 40 ``` 步骤3:创建验证码接口 在你的控制器中创建一个用于生成验证码图片的接口,例如: ```java @RestController public class CaptchaController { @GetMapping("/captcha") public void getCaptcha(HttpServletRequest request, HttpServletResponse response) { // 创建DefaultKaptcha对象并配置参数 DefaultKaptcha kaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", "no"); properties.setProperty("kaptcha.textproducer.font.color", "black"); kaptcha.setConfig(new Config(properties)); // 生成验证码文本 String text = kaptcha.createText(); // 将验证码文本保存到session中 request.getSession().setAttribute("captcha", text); // 创建验证码图片并输出到response中 BufferedImage image = kaptcha.createImage(text); try { OutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); out.flush(); } catch (IOException e) { e.printStackTrace();

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值