Java EE login interface generates random numbers to prevent malicious registration or login

Source: Internet
Author: User

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 (); // set the background color setBackGround (g); // set the border setBorder (g); // draw the interference line drawRandomLine (g ); // write the random number 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 );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.