springboot关于登录时动态随机验证码的操作

第一步,我们定义一个常量类,声明图片名和存放的路径

public class Constants {
    public static final String USER_LOGIN_CODE = "user_login_code" ;
    public static final  String IMAGE_PATH = "D:\\DownLoad\\" ;
}

第二步,引用CodeUtils类,无需我们编写,复制粘贴即可

/**
 * 用来生成验证码的工具
 */
public class CodeUtils {

    private static int width = 90;// 定义图片的width
    private static int height = 20;// 定义图片的height
    private static int fontHeight = 18; // 定义的字体大小
    private static int codeCount = 4;// 定义图片上显示验证码的个数

    private static int codeX = 15;
    private static  int codeY = 16;

    private static String [] codeArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
            "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };


    /**
     * 生成验证码,并通过响应流将验证码数据发送出去

     * @return  生成的验证码
     * @throws IOException
     */
    public static String  generateCodeImage() throws IOException {
//		 用程序(内存)在硬盘上创建图片
//		在内存中如何表示一个图片对象?
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics gd = buffImg.getGraphics();
//		把画板里面的颜色调成白色,背景用颜料填充
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);

        // 画边框。
        gd.setColor(Color.BLACK);
        gd.drawRect(0, 0, width - 1, height - 1);

        // 创建字体,字体的大小应该根据图片的高度来定。
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        // 设置字体。
        gd.setFont(font);
        // 创建一个随机数生成器类
        Random random = new Random();
        String code = "" ;
        for(int i=0;i<codeCount;i++){
//		       随机产生颜色     0 - 255
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            gd.setColor(new Color(red, green, blue));
//		        随机产生字符
            String singeCode = codeArray[random.nextInt(36)];
//		       产生的字符拼接到一起
            code+= singeCode;
//		        画到画板上面
            gd.drawString(singeCode, (i + 1) * codeX, codeY);
        }

        // 随机产生10条干扰线,使图象中的认证码不易被其它程序探测到。
        gd.setColor(Color.BLACK);
        for (int i = 0; i < 10; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            gd.drawLine(x, y, x + xl+1, y + yl+1);
        }

        File codeImage = new File(Constants.IMAGE_PATH+"code_img.png");
        FileOutputStream fos = new FileOutputStream(codeImage);
        ImageIO.write(buffImg, "jpg", fos);


//		设置响应类型
//        response.setContentType("image/jpg");
//		获取到客户端的输出流
//        ServletOutputStream out = response.getOutputStream();
//		将图片数据直接通过输出流写出去
//        ImageIO.write(buffImg, "jpg", out);

        return code ;

    }

}

第三步,通过前端页面提供的getCode方法返回到后台,这时我们对后台处理显示验证码的代码

   @RequestMapping("getCode")
    public void getCode(HttpServletRequest req, HttpServletResponse res) throws Exception {
        String code = CodeUtils.generateCodeImage();
        req.getSession().setAttribute(Constants.USER_LOGIN_CODE, code);
        showImg("code_img.png",req,res);
    }

    public void showImg(String fileName, HttpServletRequest req, HttpServletResponse res) throws Exception {
        File file = new File(Constants.IMAGE_PATH + fileName);
        FileInputStream fis = new FileInputStream(file);
        ServletOutputStream out = res.getOutputStream();
        byte[] bs = new byte[1024 * 1024];
        int i = -1;
        while ((i = fis.read(bs)) != -1) {
            out.write(bs);
            out.flush();
        }
        out.close();
        fis.close();
    }

这里通过调用封装好的showImg方法就可以完成前端验证码的显示

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值