当前位置: 代码迷 >> J2EE >> Java EE登陆界眼生成随机数防止恶意注册或者登录
  详细解决方案

Java EE登陆界眼生成随机数防止恶意注册或者登录

热度:42   发布时间:2016-04-21 22:21:48.0
Java EE登陆界面生成随机数防止恶意注册或者登录
package cn.com;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;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;public class ResponseDemoRandom extends HttpServlet {	private static final long serialVersionUID = 1L;	public static final int WIDTH=120;	public static final int HEIGHT=25;	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);		Graphics g=image.getGraphics();		//设置背景色		setBackGround(g);				//设置边框		setBorder(g);				//画干扰线		drawRandomLine(g);				//写随机数		drawRandomNum((Graphics2D)g);				response.setContentType("image/jpeg");		response.setDateHeader("expries", -1);		response.setHeader("Cache-Control", "no-control");		response.setHeader("prama", "no-control");		ImageIO.write(image, "jpg", response.getOutputStream());	}	private void drawRandomNum(Graphics2D g) {		g.setColor(Color.PINK);		g.setFont(new Font("宋体",Font.BOLD,20));		int x=10;		for(int i=0;i<4;i++)		{			int degree=new Random().nextInt()%30;			int num=new Random().nextInt(9);			String str=num+"";			g.rotate(degree*Math.PI/180, x, 20);			g.drawString(str, x, 20);			g.rotate(-degree*Math.PI/180, x, 20);			x+=30;		}			}	private void drawRandomLine(Graphics g) {		g.setColor(Color.blue);		for(int i=0;i<5;i++)		{			int x1=new Random().nextInt(WIDTH-4)+2;			int y1=new Random().nextInt(HEIGHT-4)+2;						int x2=new Random().nextInt(WIDTH-2);			int y2=new Random().nextInt(HEIGHT-2);						g.drawLine(x1, y1, x2, y2);					}			}	private void setBorder(Graphics g) {		g.setColor(Color.GREEN);		g.drawRect(1, 1, WIDTH-2, HEIGHT-2);	}	private void setBackGround(Graphics g) {		g.setColor(Color.gray);		g.fillRect(0, 0, WIDTH, HEIGHT);	}	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		doGet(request,response);	}}

  相关解决方案