栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java验证码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java验证码

Java验证码工具类 :

public class PhoneCode {

    private BufferedImage codeImg;
    private String codeStr;
    private static char code[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    public final String SESSION_CODE_NAME="code";

    private PhoneCode(){
        init();
    }

    public static PhoneCode codeInstance(){
        return new PhoneCode();
    }

    public BufferedImage getCodeImg(){
        return this.codeImg;
    }

    public String getCodeStr(){
        return codeStr;
    }

    private Color getRandColor(int x,int y){
        Random random = new Random();
        if (x>255){
            x=255;
        }
        if (y>255){
            y=255;
        }
        int r = x+random.nextInt(y-x);
        int g = x+random.nextInt(y-x);
        int b = x+random.nextInt(y-x);

        return new Color(r,g,b);
    }

    private void init(){
        int width = 85;
        int height = 20;
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        Graphics graphics = bufferedImage.getGraphics();

        Random random = new Random();
        graphics.setColor(getRandColor(200,250));
        graphics.fillRect(0,0,width,height);
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        graphics.setColor(getRandColor(160,200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(12);
            int y1 = random.nextInt(12);
            graphics.drawLine(x,y,x+x1,y+y1);
        }

        String phoneCode = "";
        for (int i = 0; i < 6; i++) {
            String onCode = String.valueOf(code[random.nextInt(code.length-1)]);
            phoneCode+=onCode;

            graphics.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
            graphics.drawString(onCode,13*i+6,16);
        }

        codeStr=phoneCode;
        graphics.dispose();
        codeImg = bufferedImage;
    }
}

 

controller:

@Controller
public class PhoneCodeController {


    @RequestMapping(value = "/getCode")
    @ResponseBody
    public void getPhoneCode(HttpServletResponse response,HttpServletRequest request){

        response.setHeader("Expires","-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "-1");
        PhoneCode phoneCode = PhoneCode.codeInstance();

         //连接redis,将验证码放入redis中
        long seconds = 60;
        String codeStr = phoneCode.getCodeStr();
        JedisCommon jedisCommon = new JedisCommon();
        Jedis jedis = jedisCommon.jedisUtil();
        jedis.setex(request.getParameter("phoneNum"),seconds,codeStr);
        jedis.close();


        try {
            //最主要的就是这个
            ImageIO.write(phoneCode.getCodeImg(),"jpg",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

html: 




手机号:

其中src是到对应controller获取验证码的请求地址。

验证的话就是直接从redis中获取key对应的值。

key是手机号。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/292550.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号