后台生成随机验证码验证登录

文章详细描述了如何使用Java在SpringMVC框架中通过WebGET请求生成并验证动态验证码,包括创建验证码图像、设置干扰线以及登录验证过程中的验证码校验。
摘要由CSDN通过智能技术生成

 web get请求获取图片

<div class="p2">
    <img id="imgId" src="/get/code">
    <a href="#">看不清,换一张</a>
</div>

后台代码:

/*获取动态验证码*/
@ResponseBody
@RequestMapping(value = "/get/code", method = {RequestMethod.POST, RequestMethod.GET})
public void getCode(HttpServletResponse response) {
	creatImg(response);
}
private void creatImg(HttpServletResponse response) {
        int width = 120;
        int height = 30;
        // 在内存中生成图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 先获取画笔对象
        Graphics2D g = (Graphics2D) image.getGraphics();
        // 设置灰色
        g.setColor(Color.GRAY);
        // 画填充的矩形
        g.fillRect(0, 0, width, height);
        // 设置颜色
        g.setColor(Color.BLUE);
        // 画边框
        g.drawRect(0, 0, width - 1, height - 1);
        // 准备数据,随机获取4个字符
        String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        // 设置颜色
        g.setColor(Color.white);
        // 设置字体
        g.setFont(new Font("隶书", Font.BOLD, 20));
        String code = "";
        //构造存储字符的数组
        char[] a = {};
        //构造存储字符串的集合
        List<String> list = new ArrayList<String>();
        Random random = new Random();
        int x = 20;
        int y = 20;
        for (int i = 0; i < 4; i++) {
            // void rotate(double theta, double x, double y)
            // theta 弧度
            // hudu = jiaodu * Math.PI / 180;
            // 获取正负30之间的角度
            int jiaodu = random.nextInt(60) - 30;
            double hudu = jiaodu * Math.PI / 180;
            g.rotate(hudu, x, y);
            // 获取下标
            int index = random.nextInt(words.length());
            // 返回指定下标位置的字符,随机获取下标
            char ch = words.charAt(index);
            //将字符存入字符数组中
            char[] chc = {ch};
            //使用字符数组构造字符串
            String string = new String(chc);
            //将构造好的字符串添加进list集合中
            list.add(string);
            // 写字符串
            g.drawString("" + ch, x, y);
            g.rotate(-hudu, x, y);
            x += 20;
        }
        for (int i = 0; i < list.size(); i++) {
            code += list.get(i);
        }
        //将验证码存入上下文中
        servletContext.setAttribute("code", code);
        // 设置颜色
        g.setColor(Color.GREEN);
        int x1, x2, y1, y2;
        // 画干扰线
        for (int i = 0; i < 4; i++) {
            x1 = random.nextInt(width);
            y1 = random.nextInt(height);
            x2 = random.nextInt(width);
            y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
        // 输出到客户端
        try {
            ImageIO.write(image, "jpg", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

登录验证:

@RequestMapping(value = "/user/login", method = {RequestMethod.POST, RequestMethod.GET})
    public void Login(@RequestBody UserBean userBean, HttpSession session, HttpServletResponse response) throws Exception {
        String code = userBean.getCode();
        //从上下文获取存储的验证码
        String code1 = (String) servletContext.getAttribute("code");
        //账号密码为admin.且验证码(忽略大小写)输入正确,则跳转到登陆成功页面
        if ("".equals(code) || code == null || !code.equalsIgnoreCase(code1)) {
            HttpServletResponseUtil.back(response, 202, "验证码错误!", null);
            return;
        }
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userBean.getUsername());
        queryWrapper.eq("password", userBean.getPassword());
        List<UserBean> beans = mapper.selectList(queryWrapper);
        if (beans != null && beans.size() > 0) {
            log.info("登陆成功,用户名:" + userBean.getUsername());
            log.info("登陆成功,密码:" + userBean.getPassword());
            session.setAttribute("loginUser", userBean);
            HttpServletResponseUtil.back(response, 200, "登录成功!", null);
        } else {
            HttpServletResponseUtil.back(response, -1, "账号密码错误", null);
        }
 
    }

 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值