• 验证码


    • VerifyUtil
    package com.govcloud.msbusinessserviceprovider.util;
    
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    
    public class VerifyUtil {
        // 验证码字符集
    //    private static final char[] chars = {
    //            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    //            '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',
    //            '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'};
        private static final char[] chars = {
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        // 字符数量
        private static final int SIZE = 4;
        // 干扰线数量
        private static final int LINES = 2;
        // 宽度
        private static final int WIDTH = 80;
        // 高度
        private static final int HEIGHT = 33;
        // 字体大小
        private static final int FONT_SIZE = 28;
    
        /**
         * 生成随机验证码及图片
         * Object[0]:验证码字符串;
         * Object[1]:验证码图片。
         */
        public static Object[] createImage() {
            StringBuffer sb = new StringBuffer();
            // 1.创建空白图片
            BufferedImage image = new BufferedImage(
                    WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            // 2.获取图片画笔
            Graphics graphic = image.getGraphics();
            // 3.设置画笔颜色
            graphic.setColor(Color.WHITE);
            // 4.绘制矩形背景
            graphic.fillRect(0, 0, WIDTH, HEIGHT);
            // 5.画随机字符
            Random ran = new Random();
            for (int i = 0; i <SIZE; i++) {
                // 取随机字符索引
                int n = ran.nextInt(chars.length);
                // 设置随机颜色
                graphic.setColor(getRandomColor());
                // 设置字体大小
                graphic.setFont(new Font(
                        null, Font.BOLD + Font.ITALIC, FONT_SIZE));
                // 画字符
                graphic.drawString(
                        chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3);
                // 记录字符
                sb.append(chars[n]);
            }
            // 6.画干扰线
            for (int i = 0; i < LINES; i++) {
                // 设置随机颜色
                graphic.setColor(getRandomColor());
                // 随机画线
                graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
                        ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
            }
            // 7.返回验证码和图片
            return new Object[]{sb.toString(), image};
        }
    
        /**
         * 生成随机数字
         * @return
         */
        public static String CreateRadomNumber(){
            StringBuilder sb = new StringBuilder();
            Random ran = new Random();
            for (int i = 0; i <SIZE; i++) {
                // 取随机字符索引
                int n = ran.nextInt(chars.length);
    
                // 记录字符
                sb.append(chars[n]);
            }
            return sb.toString();
        }
    
        /**
         * 随机取色
         */
        public static Color getRandomColor() {
            Random ran = new Random();
            Color color = new Color(ran.nextInt(256),
                    ran.nextInt(256), ran.nextInt(256));
            return color;
        }
    }
    
    • CloudODAFInfoController
      package com.govcloud.msbusinessserviceprovider.controller.odaf;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.govcloud.msbusinessserviceprovider.base.Constants;
    import com.govcloud.msbusinessserviceprovider.base.ResponseModel;
    import com.govcloud.msbusinessserviceprovider.base.ResponseStatus;
    import com.govcloud.msbusinessserviceprovider.base.SessionEnum;
    import com.govcloud.msbusinessserviceprovider.model.business.BusiSystemPersonrelation;
    import com.govcloud.msbusinessserviceprovider.model.odaf.ParamModel.AccountModel;
    import com.govcloud.msbusinessserviceprovider.model.odaf.;
    import com.govcloud.msbusinessserviceprovider.model.system.odSysoperatelog;
    import com.govcloud.msbusinessserviceprovider.service.business.IBusiSystemPersonrelationService;
    import com.govcloud.msbusinessserviceprovider.service.business.ISMSInterfaceService;
    import com.govcloud.msbusinessserviceprovider.service.dingding.IAuthorityService;
    import com.govcloud.msbusinessserviceprovider.service.odaf.IRoleInfoService;
    import com.govcloud.msbusinessserviceprovider.service.odaf.IUserNewService;
    import com.govcloud.msbusinessserviceprovider.service.odaf.IUserRightInfoService;
    import com.govcloud.msbusinessserviceprovider.service.system.ISysOperatelogService;
    import com.govcloud.msbusinessserviceprovider.util.
    ;
    import dm.jdbc.util.StringUtil;
    import lombok.extern.log4j.Log4j2;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.*;
    import java.util.concurrent.TimeUnit;

    /**

    • 用户操作控制器定义
      */
      @Log4j2
      @RestController
      @RequestMapping("/CloudODAFInfo")
      public class CloudODAFInfoController {
      @Autowired
      IUserRightInfoService iUserRightInfoService;
      @Autowired
      IRoleInfoService iRoleInfoService;
      @Autowired
      IUserNewService iUserNewService;

      @Autowired
      private ISysOperatelogService iSysOperatelogService;

      @Autowired
      IAuthorityService iAuthorityService;

      @Autowired
      ISMSInterfaceService iSMSInterfaceService;

      @Autowired
      IBusiSystemPersonrelationService iBusiSystemPersonrelationService;

      @RequestMapping(value = "/Getcode", method = RequestMethod.GET)
      public void GetCode(HttpServletResponse response, HttpServletRequest request) throws Exception {

       HttpSession session = request.getSession();
      
       Object oldCode = session.getAttribute(SessionEnum.SESSIONIMAGECODE.getSessionname());
       if (oldCode != null) { //将前一个code值删除
           String codeKey = oldCode.toString(); //RedisUtil.getInstance().del();
           RedisUtil.getInstance().del(codeKey);
       }
      
       //利用图片工具生成图片
       //第一个参数是生成的验证码,第二个参数是生成的图片
       Object[] objs = VerifyUtil.createImage();
       //将验证码存入Session
       session.setAttribute(SessionEnum.SESSIONIMAGECODE.getSessionname(), objs[0]);
       RedisUtil.getInstance().set(objs[0].toString(), "true", 10, TimeUnit.MINUTES); //将验证码加到redis中
      
       //将图片输出给浏览器
       BufferedImage image = (BufferedImage) objs[1];
       response.setContentType("image/png");
       OutputStream os = response.getOutputStream();
       ImageIO.write(image, "png", os);
      

      }

    // @GetMapping(value = "/sendmsg/{phonenumber}")
    public ResponseModel SendMSMSData(@PathVariable String phonenumber) {
    ResponseModel datainfo = new ResponseModel();
    List phoneNumbers = Arrays.asList(phonenumber.split(","));

        iSMSInterfaceService.SendMessage(phoneNumbers, "test");
        return datainfo;
    }
    

    }

  • 相关阅读:
    php Windows系统 wamp集成环境下redis的使用
    IO流文件拷贝
    IO流框架
    Map集合
    泛型
    Deque(队列)
    List接口
    Iterator接口(迭代器)
    Java中的异常详解
    Java中的正则表达式
  • 原文地址:https://www.cnblogs.com/kutoutuo/p/14181479.html
Copyright © 2020-2023  润新知