验证码 使用

本文介绍如何在网页中使用Patchca库生成验证码,并提供了一个刷新验证码的JavaScript函数。文章详细阐述了如何配置和使用Patchca服务来生成包含特定字符集的验证码,以及如何在用户界面上显示和刷新验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 下载区 有jar 包

 

	<dependency>
			<groupId>com.github.bingoohuang</groupId>
			<artifactId>patchca</artifactId>
			<version>0.0.1</version>
	</dependency>

 

 

 

验证码 使用

 

<div class="login_code_pic">
	 <a href="javascript:void(0)">
		 <img src="images/code_demo.png"	id="loginCode" οnclick="refreshCode('login');" />
	 </a>
	 </div>
	 <div class="login_code_text">
		 <a href="javascript:void(0)" οnclick="refreshCode('login');" >看不清,换一张?</a>
	 </div>
 </div>

 

 

 

function refreshCode(imgName) {
	var date = new Date();
	try {
		if (imgName == "login") {
			$("#loginCode").attr(
					"src",
					"userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		} else {
			$("#patchaImage").attr(
					"src",
					"userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		}
	} catch (e) {
		alert("验证码刷新超时,请重新刷新界面!");
		window.location.reload();
	}
}

  

 

private static final String  DEFAULT_CHARACTERS="34578AaCcdEeFGHhJjKkMmNnPpQRsTtUuVvWwXxYy";

private static final String PATCHCACODE = "PATCHCA"; 



public void refreshPatchcaImage() throws Exception {
		HttpServletResponse response = null;
		HttpServletRequest request = null;
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
		response.setContentType("image/png");//设置相应类型,告诉浏览器输出的内容为图片        
		response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容        
		response.setHeader("Cache-Control", "no-cache");        
		response.setDateHeader("Expire", 0);
		OutputStream os=null;
		try{
			//创建图片对象服务
			ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
			//修改图片文字颜色
			cs.setColorFactory(new SingleColorFactory(new Color(0, 0, 0)));
			//创建图片文字对象
			RandomWordFactory randomWordFactory = new RandomWordFactory();
			//设置文字对象范围
			randomWordFactory.setCharacters(DEFAULT_CHARACTERS);
			//设置图片文字最大长度
			randomWordFactory.setMaxLength(4);
			//设置图片文字最小长度
			randomWordFactory.setMinLength(4);
			//设置图片文字属性
			cs.setWordFactory(randomWordFactory);
			//设置图片的干扰效果的样式..一共有五种
			cs.setFilterFactory(new WobbleRippleFilterFactory ());
			Captcha captcha = cs.getCaptcha();
			//将四位数字的验证码保存到Session中。
			request.getSession().setAttribute(PATCHCACODE, captcha.getChallenge());
			//将图像输出到Servlet输出流中
			os=response.getOutputStream();
			ImageIO.write(captcha.getImage(), "png", os);
			os.flush();
			//关闭输出流
		}catch (Exception e) {
			e.printStackTrace();
			/*PrintWriter writer = response.getWriter();
			writer.print(false);
			writer.flush();
			writer.close();*/
			//log.error("生成验证码错误!......." + e);
		}finally{
			if(null!=os){
				os.close();
			}
		}
		
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值