자바 랜 덤 인증 코드

5985 단어
1. 인증 코드 생 성 도구
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class SecurityHelper {

    final static String[] key = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "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"};
    static Random random = new Random();

    private int width = 0;
    private int height = 70;

    public void setHeight(int height) {
        this.height = height;
    }

    BufferedImage image = null;

    public static String genCodeStr(int length) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(key[random.nextInt(key.length)]);
        }
        return buffer.toString();
    }

    public BufferedImage drawImg(String codeStr) {
        this.width = 38 * codeStr.length();
        image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        Graphics2D g = image.createGraphics();
        //     
        g.setColor(getRandomColor(200, 250));
        //     
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
        g.fillRect(0, 0, width, height);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        //   
        drawRandomLines(g, 20);
        //    
        //drawRandomText(g);
        //   
        drawCenterLine(g);
        StringBuffer strbuf = new StringBuffer();
        //       
        Font myFont = new Font("Consolas", Font.BOLD, 38);
        boolean b = random.nextBoolean();
        for (int i = 0; i < codeStr.length(); i++) {
            String temp = String.valueOf(codeStr.charAt(i));
            Color color = new Color(40 + random.nextInt(80), 40 + random.nextInt(80), 40 + random.nextInt(80));
            g.setColor(color);
            //       
            AffineTransform trans = new AffineTransform();
            int rad = random.nextInt(30);
            if (b = !b) {
                trans.rotate(Math.toRadians(rad), 50 * (i) + 8, 30);
            } else {
                trans.rotate(Math.toRadians(-rad), 50 * (i) + 8, 40);
            }
            //     
            float scaleSize = random.nextFloat() + 0.8f;
            if (scaleSize > 1f) {
                scaleSize = 1f;
            }
            trans.scale(1f, 1f);
            g.setTransform(trans);
            //   ,    
            g.setFont(myFont);
            g.drawString(temp, 35 * i + (i == 0 ? 10 : 15), 40);
            strbuf.append(temp);
        }
        g.dispose();
        return image;
    }

    public void genImgFile(String fileFullName) throws FileNotFoundException, IOException {
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(new File(fileFullName));
            ImageIO.write(image, "PNG", os);
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //    
    private Color getRandomColor(int fc, int bc) {
        if (fc > 255)
            fc = 200;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    //     
    private void drawRandomLines(Graphics2D g, int nums) {
        int x1 = 0, y1 = 0;
        for (int i = 0; i < nums; i++) {
            g.setColor(this.getRandomColor(190, 230));
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
            x1 = x2;
            y1 = y2;
        }
    }

    //     
    private void drawRandomText(Graphics2D g) {
        Font myFont = new Font("Consolas", Font.HANGING_BASELINE, 30);
        g.setFont(myFont);
        int max = 8 + random.nextInt(4);
        for (int i = 0; i < max; i++) {
            g.setColor(this.getRandomColor(170, 220));
            String temp = key[random.nextInt(key.length)];
            g.drawString(temp, i * 24, random.nextInt(height));
        }

    }

    //     
    private void drawCenterLine(Graphics2D g) {
        Random random = new Random();
        int py = 3 + random.nextInt(6);
        int x1 = 0, y1 = height / 2, y2 = height / 2 - 1;
        boolean minus = true;
        for (int i = 0; i < width; i++) {
            g.setColor(this.getRandomColor(60, 80));
            g.drawLine(x1, y1, x1, y2);
            x1++;
            if (minus) {
                y1--;
                y2--;
            } else {
                y1++;
                y2++;
            }
            if (Math.abs(y1 - height / 2) > py) {
                minus = !minus;
                py = 3 + random.nextInt(6);
            }
        }
    }
}

2. 사용 방식
        int index = (int) ((Math.random() * 9 + 1) * 1000);
        String code = String.valueOf(index);
        SecurityHelper helper = new SecurityHelper();
        BufferedImage image = helper.drawImg(code);

        HttpSession session = request.getSession();
        session.setAttribute("code", code);

좋은 웹페이지 즐겨찾기