Generate a verification code on the login page at the front desk

    @RequestMapping(value = "/system/keyword")
    public void keyword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader( "Expires", 0 );
         // Create an image in memory 
        int width = 60, height = 20 ;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
         // Get graphics context 
        Graphics g = image.getGraphics();
         // Generate random class 
        Random random = new Random();
         // Set background color
         // g.setColor(getRandColor(200,250)); 
        g.setColor( new Color(250, 250, 250 ));
        g.fillRect(0, 0, width, height);

        // Set the font 
        g.setFont( new Font("Times New Roman", Font.PLAIN, 18 ));
         // Draw a border
         // g.setColor(new Color());
         // g.drawRect(0, 0,width-1,height-1);
         // Randomly generate 155 interference lines, so that the authentication code in the image is not easy to be detected by other programs
         // g.setColor(getRandColor(160,200)); g.setColor 
        ( new Color(250, 250, 250 ));
         for ( int i = 0; i < 155; i++ ) {
             int x = random.nextInt(width);
             int y = random.nextInt(height);
             int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }
        // Take the randomly generated authentication code (4 digits) 
        String sRand = "" ;
         for ( int i = 0; i < 4; i++ ) {
            String rand = KeywordImg.getRandomString(); // Annotated by Han Jianfeng on 2008-01-26-- 
            sRand += rand;
             // Display the authentication code in the image
             // g.setColor(new
             // Color(20+random .nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); 
            g.setColor( new Color(0, 0, 0 ));
             // The color from the calling function is the same, Probably because the seeds are too close, so only 
            g.drawString(rand, 13 * i + 6, 16 );
        }
        // Store the authentication code in SESSION 
        request.getSession().setAttribute("keyword" , sRand);
         // The image is valid 
        g.dispose();
         // Output the image to the page 
        ImageIO.write(image, "JPEG" , response.getOutputStream());
    }

 

 

/**
 * Refresh quick login verification code
 */
function refreshCode() {
    $("#ymimg").attr("src",
        appPath + "/system/keyword.html?" + Math.random() + 100);
}
/**
 * wangchuanfu
 *
 * User presses enter to log in
 */
function keyLogin() {
    if (event.keyCode == 13)   //回车键的键值为13
        document.getElementById("ptLogin").click();  //调用登录按钮的登录事件
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324856293&siteId=291194637