首先看一下效果
1.添加依赖(pom.xml)
<!--图片验证-->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>EasyCaptcha</artifactId>
<version>1.1.0</version>
</dependency>
2.继承架包的工具类,我这边引入的工具类有问题所以需要重写一下方法
首先我们看一下代码问题
这里传进来了自定义的key,但是代码中并没有使用key,这将导致调用此方法生成的验证码不管怎么校验都显示验证码错误
解决方案:1.继承CaptchaUtil 类,同时增加了短信验证码过期。过期时间默认60秒,可自行修改测试,另外大家可以根据自己的需求进行修改。
import com.wf.captcha.Captcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class ImgUtil extends CaptchaUtil {
private int expiredMillis=60000;//验证码有效时长,单位毫秒
public void out(HttpServletRequest rq, HttpServletResponse rp) throws IOException {
this.setHeader(rp);
Captcha captcha = new GifCaptcha(super.getWidth(), super.getHeight(), super.getLen());
rq.getSession().setAttribute(super.getCodeName(), captcha.text().toLowerCase());
HttpSession session=rq.getSession();
String CodeName=super.getCodeName();
if(expiredMillis>0){
new Thread(new Runnable(){//验证码失效
@Override
public void run() {
try {
Thread.sleep(expiredMillis);
session.removeAttribute(CodeName);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
captcha.out(rp.getOutputStream());
}
public boolean ver(String key, String code, HttpServletRequest rq){
if(key==null){
key="";
}
return super.ver( key, code, rq);
}
public void out(String key, HttpServletRequest rq, HttpServletResponse rp) throws IOException {
setHeader(rp);
if(key==null){
key="";
}
Captcha captcha = new GifCaptcha(super.getWidth(), super.getHeight(), super.getLen());
ServletContext sc = rq.getServletContext();
sc.setAttribute(super.getCodeName()+ "-" + key, captcha.text().toLowerCase());
String CodeName=super.getCodeName();
String finalKey = key;
if(expiredMillis>0){
new Thread(new Runnable(){//验证码失效
@Override
public void run() {
try {
Thread.sleep(expiredMillis);
sc.removeAttribute(CodeName +"-" + finalKey);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
captcha.out(rp.getOutputStream());
}
private void setHeader(HttpServletResponse rp) {
rp.setContentType("image/gif");
rp.setHeader("Pragma", "No-cache");
rp.setHeader("Cache-Control", "no-cache");
rp.setDateHeader("Expires", 0L);
}
public int getExpiredMillis() {
return expiredMillis;
}
public void setExpiredMillis(int expiredMillis) {
this.expiredMillis = expiredMillis;
}
public ImgUtil() {
super();
}
public ImgUtil(int width, int height, int len) {
super(width, height, len);
}
}
调用演示,在调用的时候,传key和不传key的区别在与,不传是将验证码保存在session中,传了是将验证码保存ServletContext中。后者比较适合app等一些客户端调用。下方代码一个是生成图片验证码的演示,一个是校验验证码的演示。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("img")
public class ImgController {
@RequestMapping("/images/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response,String key) {
ImgUtil imgUtil=new ImgUtil();
try {
//imgUtil.out(request,response);
imgUtil.out(key,request,response);
} catch (IOException e) {
e.printStackTrace();
}
}
@ResponseBody
@RequestMapping("/images/captcha1")
public String captcha1(HttpServletRequest request, String code,String key) {
ImgUtil imgUtil=new ImgUtil();
//Boolean t=imgUtil.ver(code,request);
Boolean t=imgUtil.ver(key,code,request);
if(t){
return "验证码正确";
}else{
return "验证码不正确";
}
}
}
在代码中有一些不规范的地方还希望各位能够指出,本次教程到此结束!