Java 登录输入的验证码

简介: Java 登录输入的验证码

首先通过controller层进行业务数据访问如下:  

@RequestMapping(value = "/generVerifyCode",method = RequestMethod.GET)
    void imageVerifyCode(HttpServletResponse response)throws Exception {
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType(MediaType.IMAGE_JPEG_VALUE);
        String code = ImageCodeUtils.generateVerifyCode(4);
        int w = 200,h = 80;
        ImageCodeUtils.outputImage(w, h, response.getOutputStream(), code);
    }


具体的业务生成代码如下:

public class ImageCodeUtils {
    //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
    static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    static Random random = new Random();
    /**
     * 使用默认字符源码生成验证码
     * @param verifySize
     * @return
     */
     public static String generateVerifyCode(int verifySize){
       return generateVerifyCode(verifySize,VERIFY_CODES);
     }
    /**
     * 使用指定源生成验证码
     * @param verifySize
     * @param sources
     * @return
     */
     public static String generateVerifyCode(int verifySize,String sources){
         if(sources == null || sources.length() == 0){
             sources = VERIFY_CODES;
         }
         int codesLen = sources.length();
         Random rand = new Random(System.currentTimeMillis());
         StringBuilder verifyCode = new StringBuilder(verifySize);
         for(Integer i=0;i<verifySize;i++){
             verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
         }
         return verifyCode.toString();
     }
    /**
     * 输出置顶验证码图片流
     * @param w
     * @param h
     * @param os
     * @param code
     * @throws Exception
     */
     public static void outputImage(int w, int h, OutputStream os,String code)throws Exception{        
         int verifySize = code.length();
         BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
         Random rand = new Random();
         Graphics2D gd = image.createGraphics();
         gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
         Color[] colors = new Color[5];
         Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
                 Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
                 Color.PINK, Color.YELLOW };
         float[] fractions = new float[colors.length];
         for(int i = 0; i < colors.length; i++){
             colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
             fractions[i] = rand.nextFloat();
         }
         Arrays.sort(fractions);
         gd.setColor(new Color(250, 252, 253));// 设置边框色
         gd.fillRect(0, 0, w, h);
         gd.setColor(Color.WHITE);// 设置背景色
         gd.fillRect(0, 0, w, h);
         // 添加噪点
         float yawpRate = 0.1f;// 噪声率
         int area = (int) (yawpRate * w * h);
         for (int i = 0; i < area; i++) {
             int x = random.nextInt(w);
             int y = random.nextInt(h);
             int rgb = getRandomIntColor();
             image.setRGB(x, y, rgb);
         }
         //绘制干扰线
         Random random = new Random();
         gd.setColor(Color.gray);// 设置线条的颜色
         for (int i = 0; i < 16; i++) {
             int x = random.nextInt(w);
             int y = random.nextInt(h);
             int xl = random.nextInt(12);
             int yl = random.nextInt(12);
             gd.drawLine(x, y, x + xl, y + yl);
         }
         //shear(gd, w, h, Color.WHITE);// 使图片扭曲
         gd.setColor(getRandColor(100, 160));
         int fontSize = h-35;
         Font font = new Font("Fixedsys", Font.PLAIN,fontSize);
         gd.setFont(font);
         char[] chars = code.toCharArray();
         for(Integer i = 0; i < verifySize; i++){
             AffineTransform affine = new AffineTransform();
             affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
             gd.setTransform(affine);
             gd.setColor(new Color(20 + random.nextInt(110),20 + random.nextInt(110), 20 + random.nextInt(110)));
             gd.drawChars(chars, i, 1, (((w-10) / verifySize) * i + 5), h/2 + fontSize/2 - 10);
         }
         gd.dispose();
         ImageIO.write(image, "jpg", os);
     }
    private static int getRandomIntColor() {
        int[] rgb = getRandomRgb();
        int color = 0;
        for (int c : rgb) {
            color = color << 8;
            color = color | c;
        }
        return color;
    }
    private static int[] getRandomRgb() {
        int[] rgb = new int[3];
        for (int i = 0; i < 3; i++) {
            rgb[i] = random.nextInt(255);
        }
        return rgb;
    }
    public static  Color getRandColor(int fc,int bc){
         Random random = new Random();
         if(fc>255){
             fc=255;
         }
         if(bc>255){
             bc=255;
         }
         int r = fc + random.nextInt(bc-fc);
         int g = fc + random.nextInt(bc-fc);
         int b = fc + random.nextInt(bc-fc);
         return new Color(r,g,b);
     }


相关实践学习
【AI破次元壁合照】少年白马醉春风,函数计算一键部署AI绘画平台
本次实验基于阿里云函数计算产品能力开发AI绘画平台,可让您实现“破次元壁”与角色合照,为角色换背景效果,用AI绘图技术绘出属于自己的少年江湖。
从 0 入门函数计算
在函数计算的架构中,开发者只需要编写业务代码,并监控业务运行情况就可以了。这将开发者从繁重的运维工作中解放出来,将精力投入到更有意义的开发任务上。
目录
相关文章
|
11月前
|
存储 缓存 Java
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
1427 3
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
|
11月前
|
缓存 Java 应用服务中间件
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
2184 5
|
11月前
|
存储 小程序 前端开发
微信小程序与Java后端实现微信授权登录功能
微信小程序极大地简化了登录注册流程。对于用户而言,仅仅需要点击授权按钮,便能够完成登录操作,无需经历繁琐的注册步骤以及输入账号密码等一系列复杂操作,这种便捷的登录方式极大地提升了用户的使用体验
3330 12
|
小程序 前端开发 算法
|
C#
C# 图形验证码实现登录校验代码
C# 图形验证码实现登录校验代码
349 2
|
存储 JSON 前端开发
node使用token来实现前端验证码和登录功能详细流程[供参考]=‘很值得‘
本文介绍了在Node.js中使用token实现前端验证码和登录功能的详细流程,包括生成验证码、账号密码验证以及token验证和过期处理。
496 0
node使用token来实现前端验证码和登录功能详细流程[供参考]=‘很值得‘
|
资源调度 JavaScript API
nest.js + sms 实现短信验证码登录
本文介绍了在Nest.js框架中集成短信验证码登录的实现方案,详细阐述了使用阿里云短信服务的配置流程、资质申请、短信模板设置,并提供了API调用示例和工程代码的运行步骤。
nest.js + sms 实现短信验证码登录
【Azure 环境】中国区Azure B2C 是否支持手机验证码登录呢?
【Azure 环境】中国区Azure B2C 是否支持手机验证码登录呢?
141 0