SpringBoot下实现前端验证码图片的生成和校验

1、SpringBoot下实现前端验证码图片的生成和校验
https://blog.csdn.net/Colton_Null/article/details/78744240

2、SpringBoot——登录验证码实现
https://blog.csdn.net/qq_37651267/article/details/99305573

3、SpringBoot下实现前端验证码图片的生成和校验
https://www.cnblogs.com/qiantao/p/12670517.html

4、SpringBoot Spring Security5 实现验证码登录
https://liuyanzhao.com/8115.html

5、SpringBoot实现简单的登录验证码
https://www.it610.com/article/1283015419799552000.htm

6、JAVA生成验证码代码
https://www.cnblogs.com/lidedong/p/9575830.html

7、Java学习 作者:menglanyingfei/Java
https://github.com/menglanyingfei/Java/blob/master/JavaWebTrain/day_1_12/src/com/wtu/servlet/LoginServlet.java

https://github.com/menglanyingfei/Java

8、案例2——验证码登录与注销
https://blog.csdn.net/qq_36371449/article/details/80313665?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2allsobaiduend~default-2-80313665.nonecase&utm_term=javaweb%E9%AA%8C%E8%AF%81%E7%99%BB%E5%BD%95

9、JavaWeb实现登录验证码及生成图片
https://blog.csdn.net/menglanyingfei/article/details/79101580

https://blog.csdn.net/qq_36371449/article/details/80313665

https://blog.csdn.net/qq_26641781/article/details/78180138

https://www.cnblogs.com/lidedong/p/9575830.html

https://blog.csdn.net/github_36086968/article/details/78592775

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
验证码登录通常包含以下步骤: 1. 在前端页面上展示验证码图片,并在提交表单时将验证码值一并提交到后端。 2. 后端接收到验证码值后,通过比较用户提交的验证码值和生成验证码值是否一致来判断验证码是否正确。 3. 如果验证码正确,则继续进行登录操作;否则返回错误提示信息。 下面是一个使用Vue和Spring Boot实现验证码登录的示例代码: 前端页面(使用Element UI库): ```html <template> <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="100px"> <el-form-item label="用户名" prop="username"> <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码"></el-input> </el-form-item> <el-form-item label="验证码" prop="verifyCode"> <el-input v-model="loginForm.verifyCode" placeholder="请输入验证码"></el-input> <img :src="verifyCodeUrl" @click="refreshVerifyCode" style="cursor: pointer;"> </el-form-item> <el-form-item> <el-button type="primary" @click="login">登录</el-button> </el-form-item> </el-form> </template> <script> import { login } from '@/api/user' export default { data() { return { loginForm: { username: '', password: '', verifyCode: '' }, rules: { username: [{ required: true, message: '请输入用户名', trigger: 'blur' }], password: [{ required: true, message: '请输入密码', trigger: 'blur' }], verifyCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }] }, verifyCodeUrl: '/api/verifyCode?timestamp=' + new Date().getTime() } }, methods: { refreshVerifyCode() { this.verifyCodeUrl = '/api/verifyCode?timestamp=' + new Date().getTime() }, login() { this.$refs.loginForm.validate(valid => { if (valid) { login(this.loginForm).then(res => { // 登录成功后的操作 }).catch(error => { // 登录失败后的操作 }) } }) } } } </script> ``` 后端代码: ```java @RestController public class LoginController { @Autowired private RedisTemplate<String, String> redisTemplate; @GetMapping("/api/verifyCode") public void verifyCode(HttpServletRequest request, HttpServletResponse response) throws Exception { // 生成验证码 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); // 将验证码存入Redis中,有效期为5分钟 String key = "verifyCode_" + request.getSession().getId(); redisTemplate.opsForValue().set(key, verifyCode, 5, TimeUnit.MINUTES); // 将验证码图片写入响应流中 response.setContentType("image/jpeg"); ServletOutputStream out = response.getOutputStream(); VerifyCodeUtils.outputImage(100, 40, out, verifyCode); } @PostMapping("/api/login") public Result login(@RequestBody LoginForm form, HttpServletRequest request) { // 校验验证码 String key = "verifyCode_" + request.getSession().getId(); String verifyCode = redisTemplate.opsForValue().get(key); if (StringUtils.isBlank(verifyCode)) { return Result.error("验证码已过期,请重新获取"); } if (!verifyCode.equalsIgnoreCase(form.getVerifyCode())) { return Result.error("验证码不正确"); } // 校验用户名和密码 // ... return Result.ok(); } } ``` 其中,`VerifyCodeUtils`是一个工具类,用于生成验证码图片。`LoginForm`是一个表单类,用于接收前端传递的用户名、密码和验证码值。`Result`是一个自定义的返回结果类,用于返回登录结果和错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值