java实例 验证码机制

(1)登陆页面前台html部分代码


     
     


(2)web.xml配置sevlet调用入口


	
      
      
		
       
       
        
        ValidateCodeServlet
       
       
		
       
       
        
        com.saving.ecm.servlet.ValidateCodeServlet
       
       
	
      
      

	
      
      
		
       
       
        
        ValidateCodeServlet
       
       
		
       
       
        
        /getImg
       
       
	
      
      

(3)sevlet生成验证码,存入session,返回前台

public class ValidateCodeServlet extends HttpServlet {

	
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String[] validates = {
				"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","0","1","2","3","4","5",
				"6","7","8","9",
		        };
		
		Random r = new Random();
		String validateCode = "";
		for(int i = 0; i < 4; i++){
			validateCode += validates[r.nextInt(validates.length)];
		}
		
		BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
		
		Graphics g = image.getGraphics();
		
		g.setColor(new Color(0xDCDCDC));
	    g.fillRect(0, 0, 80, 20);
	     
		g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
		g.setColor(Color.RED);
		String str = validateCode.substring(0,1);
		g.drawString(str, 8, 15);
		str = validateCode.substring(1,2);
		g.drawString(str, 28, 15);
		str = validateCode.substring(2, 3);
		g.drawString(str, 46, 15);
		str = validateCode.substring(3, 4);
		g.drawString(str, 64, 15);
		
		g.dispose();
		
		HttpSession session = request.getSession(true);
		session.removeAttribute("validate");
		
		System.out.println("生成的验证码="+validateCode);
		session.setAttribute("validate", validateCode);
		
		ImageIO.write(image, "JPEG", response.getOutputStream());
	
	}

}


(4)后台校验验证码
@POST
	@Path("/login")
	@Produces("application/json;charset=utf-8")
	public Response login(@Context HttpServletRequest request, User user)
			throws Throwable {
		String message = null;
		String foreValidateCode = user.getValidate();
		String backValidateCode =  request.getSession(true).getAttribute("validate").toString();
		if(!foreValidateCode.equalsIgnoreCase(backValidateCode)){
			message = "验证码错误";
			
		}

(5)如果验证码错误,或者看不清楚,局部刷新验证码
//改变验证码图片  
function changeCode(){  
    var request = new XMLHttpRequest();  
    request.onreadystatechange = function(){  
        if(request.readyState == 4){  
            document.getElementById("code").src = "/ecm/getImg";
        }  
    }  
    request.open("GET","/ecm/getImg",true);  
    request.send(null);  
}  




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十五楼亮哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值