• java servlet 返回图片验证码


    java servlet 返回图片验证码 ,用于注册、登录场景,使用图片验证码防止爆破攻击
    实际使用要注意在java后台每次取到验证码比较验证码是否正确时都需要把图片验证码清空掉,
    否则拿到一个验证码依然可以一直爆破攻击

    CheckCodeServlet.java

    package com.gaom.servlet;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;
    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;
    
    public class CheckCodeServlet extends HttpServlet {
    	
    	private static  int width=94;
    	private static int height=38;
    	private static int codeCount=4;
    	private static int lineCount=30;
    	private int code;
    	
    	
    	
    	private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J','K',   
    			'M', 'N',  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y',   
    			   '2', '3', '4', '5', '6', '7', '8', '9' }; 
    
    	public void destroy() {
    		super.destroy(); // Just puts "destroy" string in log
    		// Put your code here
    	}
    
    	
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		
    		OutputStream os=null;
    		try{
    			
    			os=response.getOutputStream();
    			Map map=new HashMap();
    		BufferedImage bufferedImage=createCode(map);
    		String randomCode=map.get("randomCode")+"";
    		HttpSession httpSession=request.getSession();
    					httpSession.removeAttribute("randomCode");
    					httpSession.setAttribute("randomCode", randomCode);
    					httpSession.setMaxInactiveInterval(3600);
    		System.out.println("随机生成的验证码为:"+ randomCode);
    		
    		
    		// 设置响应的类型格式为图片格式  
            response.setContentType("image/jpeg");  
            //禁止图像缓存。  
            response.setHeader("Pragma", "no-cache");  
            response.setHeader("Cache-Control", "no-cache");  
            response.setDateHeader("Expires", 0); 
    		
    		ImageIO.write(bufferedImage, "png",os);
    		
    		
    		
    		
    		  
    		}catch(Exception e){
    			e.printStackTrace();
    		}finally{
    			if(os!=null){
    				os.close();
    			}
    		}
    	}
    
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		doGet(request,response);
    	}
    
    	public void init() throws ServletException {
    		
    	}
    	
    	
    	private BufferedImage createCode(Map map){
    		int x=17,fontHeight=25,codeY=30,red=0,green=0,blue=0;
    		BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    		 Graphics2D 	graphics2d=		bufferedImage.createGraphics();
    		 Random random=new Random();
    		 	 graphics2d.setColor(Color.white);
    		 	 graphics2d.fillRect(0,0, width, height);
    		 	 Font font = new Font("Algerian", Font.ITALIC, fontHeight);  
    		 	 graphics2d.setFont(font);
    		 	 for(int i=0;i<lineCount;i++){
    		 		 
    		 		int xs = random.nextInt(width);  
    		 		int ys = random.nextInt(height);  
    		 		int xe = xs+random.nextInt(width/8);  
    		 		int ye = ys+random.nextInt(height/8);
    		 		 
    		 		 red=random.nextInt(255);
    		 		 green=random.nextInt(255);
    		 		 blue=random.nextInt(255);
    		 		 graphics2d.setColor(new Color(red,green,blue));
    		 		 graphics2d.drawLine(xs, ys, xe, ye);
    		 		 
    		 	 }
    		 	StringBuffer randomCode = new StringBuffer(); 
    		 	for(int i=0;i<codeCount;i++){
    		 		String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
    		 		red = random.nextInt(255);  
    		 		green = random.nextInt(255);  
    		 		blue = random.nextInt(255);  
    		 		graphics2d.setColor(new Color(red, green, blue));  
    		 		graphics2d.drawString(strRand, (i + 1) * x, codeY);  
    		 		randomCode.append(strRand);  
    		 	}
    		 	map.put("randomCode", randomCode.toString());
    		 	
    		 	 bufferedImage.flush();
    		 	
    		 	 return bufferedImage;
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    web.xml 增加servlet-mapping

    web3.0以后也可以直接在CheckCodeServlet.java上使用servlet注解形式

     
            <servlet-mapping>
            	<servlet-name>CheckCodeservlet-name>
            	<url-pattern>/checkCodeurl-pattern>
            servlet-mapping>
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    本地存储(LocalStorage)、会话存储(Session)和 Cookie 三者之间的区别
    java第二讲:运算符与流程控制
    中国石油大学《计算机应用基础》第三次在线作业
    Flask框架学习:模板渲染与Get,Post请求
    两个月雅思口语速成
    springboot基于安卓的移动数字图书馆的设计与实现毕业设计源码030911
    23062C++&QTday7
    vue2学习之插槽
    高可用(keepalived)部署方案
    软件杯 深度学习YOLO安检管制物品识别与检测 - python opencv
  • 原文地址:https://blog.csdn.net/qq445829096/article/details/134436470