• 图片验证码生成与登陆验证(SpringBoot Mybatis-Plus)


    gradle

    compile group: 'com.github.penggle', name: 'kaptcha', version: '2.3.2'
    

    demo

    import com.google.code.kaptcha.Constants;
    import com.google.code.kaptcha.Producer;
    
    import javax.annotation.Resource;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import java.util.concurrent.TimeUnit;
    
    @RestController
    @CrossOrigin(origins = "*", maxAge = 3600, allowCredentials="true")
    public class 图片验证码 {
    
        @Resource
        private Producer producer;
        @Resource
        private RedisUtils redisUtils;
        /**
         * @updateAuthor 李雪雯
         * @param uuid 唯一key
         * @throws Exception
         */
        @GetMapping("captcha.jpg")
        public void captcha(HttpServletResponse response, HttpServletRequest request,@RequestParam("uuid") String uuid) throws Exception {
    
            response.setHeader("Cache-Control", "no-store, no-cache");
            response.setContentType("image/jpeg");
    
            // 生成文字验证码
            String text = "";
            text=String.valueOf((new Random().nextInt(89999) + 10000));
            System.out.println("验证码:" + text);
            redisUtils.set(uuid,text,1l, TimeUnit.MINUTES);
    
            // 生成图片验证码
            BufferedImage image = producer.createImage(text);
            // 保存到验证码到 session
            request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);
            System.out.println(uuid);
    
    //      ServletOutputStream 类提供了将二进制数据写入响应的流。这是一个抽象类。
    //      ServletResponse 接口的 getOutputStream() 方法返回 ServletOutputStream 类的实例
    
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(image, "jpg", out);
            IOUtils.closeQuietly(out);
        }
    
        /**
         * 登录接口
         */
        @PostMapping(value = "/login")
        public HttpResult login(@RequestBody LoginUserVO loginUser, HttpServletRequest request) {
            System.out.println("登录接口Login");
            String username = loginUser.getUsername();
            String password = loginUser.getPassword();
            String captcha = loginUser.getCaptcha();
            String uuid = loginUser.getUuid();
    
            if (username.isEmpty()) {
                return HttpResult.error("账号不能为空");
            }
            if (password.isEmpty()) {
                return HttpResult.error("密码不能为空");
            }
            if (null == captcha || captcha.isEmpty()) {
                return HttpResult.error("验证码不能为空");
            }
    
             //从session中获取之前保存的验证码跟前台传来的验证码进行匹配
            String captchaTmp = String.valueOf(redisUtils.get(uuid));
            System.out.println("uuid"+uuid);
            System.out.println(captchaTmp);
    
            //从 redis 中获取之前保存的验证码跟前台传来的验证码进行匹配
            if(null == captchaTmp || "null".equals(captchaTmp) || "".equals(captchaTmp)){
                return HttpResult.error("验证码已失效");
            }
            if(!captcha.equals(captchaTmp)){
                return HttpResult.error("验证码不正确");
            }
    
            // 根据用户名获取用户信息
            SysUser user = sysUserService.getUserByField("username", username);
            // 账号不存在、密码错误
            if (user == null) {
                return HttpResult.error("账号不存在");
            }
            if (!PasswordUtil.matches(user.getSalt(), password, user.getPassword())) {
                return HttpResult.error("密码不正确");
            }
            // 账号锁定
            if (user.getStatus() == 0) {
                return HttpResult.error("账号已被锁定,请联系管理员");
            }
            // 系统登录认证
            JwtAuthenticatioToken token = SecurityUtil.login(request, username, password, authenticationManager);
            return HttpResult.ok(token);
        }
    }
    
  • 相关阅读:
    6-5 函数
    6-2 触发器
    4、MongoDB学习之备份还原
    3、MongoDB学习之固定集合
    2、MongoDB学习之索引的管理
    1、MongoDB学习之基本操作
    JS-01
    | 和 ||,& 和 && 的区别
    正则
    设计测试用例的经验总结
  • 原文地址:https://www.cnblogs.com/ideaAI/p/14757786.html
Copyright © 2020-2023  润新知