Spring Boot 中 Web开发登录页验证码的简单实现

RT,简单在于不用自己渲染。

环境:Spring Boot 2.x 、Maven 3.6.x

三方jar :Hutool


引用Hutool > 验证码生成,对比其ref

 

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.hutool.captcha.LineCaptcha;

public class VerifyCodeGen {

    public static ICaptcha codeGen(int width,int height,int codeCount,int lineCount){
        // 验证码字符个数由${project.codeCount}指定
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(width, height, codeCount, lineCount);
        return captcha;
    }

}

Controller 

@GetMapping(path = "/verifyCode")
    public void loadVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 生成验证码对象, 字符个数由配置文件控制
        ICaptcha captcha = VerifyCodeGen.codeGen(100, 50, this.codeCount, 100);

        // 获取验证码设置session
        String code = captcha.getCode();
        HttpSession session = request.getSession();
        session.setAttribute("verifyCode", code);
        session.setMaxInactiveInterval(3 * 60);
        // 写出验证码到输出流,前端即可得到该验证码图
        response.setContentType(MimeTypeUtils.IMAGE_PNG_VALUE);
        ServletOutputStream os = response.getOutputStream();
        captcha.write(os);
        os.flush();
        os.close();
    }

测试: 不建议swagger,全是乱码。

Thymeleaf 模板:

<img id="imgVerifyCode" th:src="@{/meeting/verifyCode}" width="100" height="50">

效果:

小结:只是利用三方实现,其中奥妙可按需探究。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用Spring Boot和Vue实现验证码,可以按照以下步骤进行: 1. 首先在Spring Boot添加依赖项,包括spring-boot-starter-webspring-boot-starter-validation。 2. 创建一个验证码生成器类,该类可以使用Java的Graphics2D类在内存生成图片。 3. 创建一个Spring Boot控制器,该控制器将生成的验证码图片返回给客户端。 4. 在Vue创建一个组件,该组件将显示验证码图片,并允许用户输入验证码。 5. 在Vue实现验证逻辑,将用户输入的验证码与后端生成的验证码进行比较。 以下是示例代码: Spring Boot控制器: ```java @RestController public class CaptchaController { @Autowired private CaptchaGenerator captchaGenerator; @GetMapping("/captcha") public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception { // 获取验证码图片 BufferedImage image = captchaGenerator.generateCaptchaImage(); // 将图片写入响应流 response.setContentType("image/png"); ImageIO.write(image, "png", response.getOutputStream()); // 将验证码存储在session HttpSession session = request.getSession(); session.setAttribute("captcha", captchaGenerator.getCaptchaString()); } } ``` Vue组件: ```vue <template> <div> <img :src="captchaUrl" @click="refreshCaptcha"> <input v-model="captchaInput" @keyup.enter="submitCaptcha"> <button @click="submitCaptcha">Submit</button> </div> </template> <script> export default { data() { return { captchaUrl: '/captcha', captchaInput: '' } }, methods: { refreshCaptcha() { this.captchaUrl = '/captcha?' + Math.random() }, submitCaptcha() { axios.post('/validate-captcha', { captcha: this.captchaInput }).then(response => { if (response.data.success) { alert('验证码正确') } else { alert('验证码错误') } }) } } } </script> ``` 在上面的Vue组件,我们使用axios发送POST请求到后端验证用户输入的验证码。在后端,我们可以将存储在session验证码与用户输入的验证码进行比较。 希望这可以帮助到你!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值