자바 웹 입문 인증 코드 그림 생 성

18301 단어 자바인증번호
자바 로 인증 코드 그림 을 만 듭 니 다.코드 는 다음 과 같 습 니 다.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class IdentifyingImage {
    static private int width = 80, height = 27;
    static  private Random random = new Random();
    static private Font font = new Font("Comic Sans", Font.BOLD|Font.ITALIC, 24);

    static private Color getRandomColor()
    {
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return new Color(r, g, b);
    }

    static public void getItem()
    {
        //         
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //        
        Graphics graphics = image.getGraphics();
        //     
        graphics.setColor(getRandomColor());
        graphics.fillRect(0, 0, width, height);
        //      
        graphics.setColor(getRandomColor());
        graphics.drawRect(0, 0, width-1, height-1);
        //     
        for (int i=0; i<10; ++i)
        {
            int x1 = random.nextInt(width-2);
            int y1 = random.nextInt(height-2);
            int x2 = random.nextInt(width-2);
            int y2 = random.nextInt(height-2);
            graphics.setColor(getRandomColor());
            graphics.drawLine(x1+1, y1+1, x2+1, y2+1);
        }
        //    
        StringBuffer strbuf = new StringBuffer("");
        graphics.setFont(font);
        for (int i=0; i<4; ++i)
        {
            graphics.setColor(getRandomColor());
            int n = random.nextInt(10);
            strbuf.append(n);
            graphics.drawString(""+n, 1+20*i, height-4);
        }

        //    
        File file = new File("D:\\123.gif");
        try
        {
            ImageIO.write(image, "GIF", file);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        //    
        graphics.dispose();
    }


    public static void main(String[] args)
    {
        IdentifyingImage.getItem();
    }
}

일반적으로 웹 에 사용 되 는 것 은 인증 코드 가 있 는 간단 한 로그 인 인터페이스 입 니 다.먼저 위의 종 류 를 자바 빈 으로 봉 합 니 다.
package advance;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;

public class IdentifyingImage {
    static private int width = 78, height = 27;
    static  private Random random = new Random();
    static private Font font = new Font("Comic Sans", Font.BOLD|Font.ITALIC, 24);

    static private Color getRandomColor()
    {
        int r = random.nextInt(255);
        int g = random.nextInt(255);
        int b = random.nextInt(255);
        return new Color(r, g, b);
    }

    static public BufferedImage getItem(String s)
    {
        //         
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //        
        Graphics graphics = image.getGraphics();
        //     
        graphics.setColor(getRandomColor());
        graphics.fillRect(0, 0, width, height);
        //      
        graphics.setColor(getRandomColor());
        graphics.drawRect(0, 0, width-1, height-1);
        //     
        for (int i=0; i<10; ++i)
        {
            int x1 = random.nextInt(width-2);
            int y1 = random.nextInt(height-2);
            int x2 = random.nextInt(width-2);
            int y2 = random.nextInt(height-2);
            graphics.setColor(getRandomColor());
            graphics.drawLine(x1+1, y1+1, x2+1, y2+1);
        }
        //    
        graphics.setFont(font);
        for (int i=0; i<4; ++i)
        {
            graphics.setColor(getRandomColor());
            int n = random.nextInt(10);
            graphics.drawString(""+s.charAt(i), 2+20*i, height-4);
        }

        //    
        graphics.dispose();
        return image;
    }
}

그리고 인증 코드 를 클 라 이언 트 에 전송 하 는 Servlet 를 작성 하여 인증 코드 정 보 를 session 에 넣 어 인증 을 준비 합 니 다.
package advance;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import advance.IdentifyingImage;

public class GenImage extends HttpServlet {
    public GenImage()
    {
        super();
    }
    public void destroy()
    {
        super.destroy();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        String code="";
        Random rm = new Random();
        for(int i=0; i<4; ++i) code += rm.nextInt(10);
        HttpSession session = request.getSession();
        session.setAttribute("piccode",code);
        //    
        response.setHeader("Prama","no-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/gif");
        ImageIO.write(IdentifyingImage.getItem(code), "gif", response.getOutputStream());
        response.getOutputStream().close();
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet(request, response);
    }
    public void init() throws ServletException {}
}

웹.xml 설정
<servlet>
  <servlet-name>image</servlet-name>
  <servlet-class>advance.GenImage</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>image</servlet-name>
  <url-pattern>/image</url-pattern>
</servlet-mapping>

로그 인 인터페이스 login.html.
<html>
    <meta http-equiv=Content-Type content="text/html;charset=utf-8">
    <script type="text/javascript"> function reloadImage(t) { t.src="image?flag="+Math.random(); } </script>
<head>
    <title>login</title>
</head>
<body>
    <form action="check.jsp" method="post">
    <input type="text" name="checkcode">
    <img src="image" onclick="reloadImage(this)">
    <br><br>
    <input type="submit" value="  ">
    </form>
</body>
</html>

페이지 check.jsp 를 검증 하고 session 에서 정확 한 인증 코드 를 얻 으 며 request 매개 변수 에서 입력 한 인증 코드 를 얻 습 니 다.
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" %>
<html>
    <meta http-equiv=Content-Type content="text/html;charset=utf-8">
<head>
    <title>check</title>
</head>
<body>
    <%
    String checkcode=request.getParameter("checkcode");
    String piccode=new String();
    Object o = session.getAttribute("piccode");
    if(o != null) piccode=o.toString();
    else piccode="    ";
    %>
            : <%=checkcode%><br>
           : <%=piccode%>
</body>
</html>

좋은 웹페이지 즐겨찾기