zoukankan      html  css  js  c++  java
  • 登录验证码。。

    html页面:

    <form action="login" method="post">
    用户名:<input type="text" name="username" id="username"/><br/>
    密码:&nbsp;&nbsp;&nbsp;<input type="password" name="password" id="password"/><br/>
    验证码:<input type="text" name="identifyCode" id="identifyCode"/>
    <img src="identifyCode" id="identifyCodeImg" onclick="getCode();" title="换一张"/><br/>
    <input type="button" value="登录" onclick="test();"/>
    <span id="resultMsg" style="color: red;"></span>
    </form>

    js:

    function test(){
    $.ajax({
    url:"login",
    data:{
    username:$("#username").val(),
    password:$("#password").val(),
    identifyCode:$("#identifyCode").val()
    },
    type:"POST",
    dataType:"json",
    success:function(data){
    if(data.success){
    window.location.href="user/operation";
    }
    else{
    $("#resultMsg").html(data.message);
    }

    },
    erro:function(){
    alert("爆炸了!");
    }

    });
    }
    function getCode(){
    $("#identifyCodeImg").attr("src","identifyCode?flag="+Math.random());
    }

    controller:

    private Random random = new Random();
    private String[] allCodes = {
    "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"
    };

    @RequestMapping(value="identifyCode")
    public void getCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "No-cache");
    response.setDateHeader("Expires", 0);//禁止客户端缓存页面
    response.setContentType("image/jpg");//设置响应正文类型为图片
    int height = 20;
    int width = 60;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();//获取处理图片的画笔
    g.setColor(getRandomColor(200, 250));//设置背景色
    g.fillRect(0, 0, width, height);//画一个矩形
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//设置字体
    g.setColor(getRandomColor(160, 200));//设置干扰线的颜色
    for (int i = 0; i < 100; i++){//画一百条干扰线
    int x = random.nextInt(width);
    int y = random.nextInt(height);
    int x1 = random.nextInt(10);
    int y1 = random.nextInt(10);
    g.drawLine(x, y, x1, y1);//干扰线的两个顶点坐标
    }

    String strCode = "";
    for (int i = 0; i < 4; i++){//生成四个随机字符
    String strNumber = allCodes[random.nextInt(36)];
    strCode += strNumber;
    g.setColor(getRandomColor(20, 120));
    g.drawString(strNumber , 13 * i + 3, 16);
    }
    request.getSession(true).setAttribute("code", strCode);//将生成的验证码存入session
    g.dispose();//释放图像的上下文资源
    ImageIO.write(image, "JPEG", response.getOutputStream());//输出JPEG格式图像
    response.getOutputStream().flush();//刷新输出流
    response.getOutputStream().close();//关闭输出流
    }

    private Color getRandomColor(int min, int max){
    int r = min + random.nextInt(max - min);
    int b = min + random.nextInt(max - min);
    int g = min + random.nextInt(max - min);
    return new Color(r, b, g);
    }

    依照前人的来做的。。前几天写的。。忘了来源了。。。0.0.0

  • 相关阅读:
    USACO 玛丽卡(最短路+枚举)
    POJ 1161 Walls(最短路+枚举)
    Windows 上配置Docker Desktop 的k8s
    菜鸡学算法--70. 爬楼梯
    CLR 异步函数
    【.NET Core开发实战学习笔记】依赖注入框架:管理服务的依赖与生命周期
    【.NET Core 开发实战学习笔记】StartUp:理解程序的启动过程
    .ef core 多对对关系的关联方法
    HttpGet请求传递数组(集合)
    使用wkhtmltopdf工具生成pdf
  • 原文地址:https://www.cnblogs.com/yuezeyuan/p/7655286.html
Copyright © 2011-2022 走看看