patchca实现验证码验证
程序员文章站
2022-05-19 19:17:40
...
Patchca是Piotr Piastucki写的一个java验证码,打包成jar文件发布,patchca简单但功能强大。
pom.xml注入依赖
<!-- https://mvnrepository.com/artifact/com.github.bingoohuang/patchca -->
<dependency>
<groupId>com.github.bingoohuang</groupId>
<artifactId>patchca</artifactId>
<version>0.0.1</version>
</dependency>
CaptchaWordFactory.java
public class CaptchaWordFactory extends RandomWordFactory {
private String defaultValue = "";
private boolean isDefaultState = false; // 默认为非固定值状态
public CaptchaWordFactory() {
// 文本范围和长度
characters = "abcdefskmnwx23456789";
minLength = 4;
maxLength = 4;
}
public CaptchaWordFactory(String defaultValue) {
this.defaultValue = defaultValue;
this.isDefaultState = true; //开启绑定值状态
}
public WordBean getNextWord() {
if(isDefaultState) {
return new WordBean("","","");
} else {
return super.getNextWord();
}
}
}
CaptchaService.java
public class CaptchaService extends AbstractCaptchaService {
private static final Random random = new Random();
/**
* 不可定制的验证码服务(默认)
*/
public CaptchaService() {
// 文本内容
wordFactory = new CaptchaWordFactory();
// 字体
fontFactory = new RandomFontFactory();
// 效果
textRenderer = new BestFitTextRenderer();
// 背景
backgroundFactory = new SingleColorBackgroundFactory();
// 字体颜色
colorFactory = new SingleColorFactory(new Color(25, 60, 170));
// 样式(曲线波纹加干扰线)
//0到4 之间生产随机数
int i = random.nextInt(5) ;
switch (i) {
case 0:
filterFactory = new CurvesRippleFilterFactory(colorFactory);
break;
case 1:
filterFactory = new DoubleRippleFilterFactory();
break;
case 2:
filterFactory = new WobbleRippleFilterFactory();
break;
case 3:
filterFactory = new DiffuseRippleFilterFactory();
break;
case 4:
filterFactory = new MarbleRippleFilterFactory();
break;
}
// 图片宽高
width = 150;
height = 50;
}
/**
* 可以定制的验证码服务(宽度和高度)
* @param width
* @param height
*/
public CaptchaService(int width, int height) {
// 文本内容
wordFactory = new CaptchaWordFactory();
// 字体
fontFactory = new RandomFontFactory();
// 效果
textRenderer = new BestFitTextRenderer();
// 背景
backgroundFactory = new SingleColorBackgroundFactory();
// 字体颜色
colorFactory = new SingleColorFactory(new Color(25, 60, 170));
// 样式(曲线波纹加干扰线)
filterFactory = new CurvesRippleFilterFactory(colorFactory);
// 图片宽高
this.width = width;
this.height = height;
}
/**
* 可以定制的验证码服务(默认值,宽度和高度)
* @param defaultValue
* @param width
* @param height
*/
public CaptchaService(String defaultValue, int width, int height) {
// 文本内容
wordFactory = new CaptchaWordFactory(defaultValue);
// 字体
fontFactory = new RandomFontFactory();
// 效果
textRenderer = new BestFitTextRenderer();
// 背景
backgroundFactory = new SingleColorBackgroundFactory();
// 字体颜色
colorFactory = new SingleColorFactory(new Color(25, 60, 170));
// 样式(曲线波纹加干扰线)
filterFactory = new CurvesRippleFilterFactory(colorFactory);
// 图片宽高
this.width = width;
this.height = height;
}
}
利用Controller接收前段Http请求并且以图片资源作响应
CaptchaController.java
@Controller
public class CaptchaController implements Serializable {
@RequestMapping("/generateCaptcha")
public void getCaptchaController(HttpServletRequest req, HttpServletResponse res) throws IOException
{
// 设置两个响应头
//返回值类型时 png格式的图片
res.setContentType("image/png");
//验证码不要进行缓存
res.setHeader("cache", "no-cache");
OutputStream os = res.getOutputStream();
// 值随机,宽度110px,高度40px
//可以根据需要修改 参数设置定制化的化的验证码
CaptchaService captchaService = new CaptchaService(110, 40);
//生产验证码图片 通过outputStream的方式进行发送
String captcha = EncoderHelper.getChallangeAndWriteImage(captchaService, "png", os);
os.flush();
if(os != null) {
os.close();
os = null;
}
//将图像的验证码存贮在session 在账号验证时对其进行验证
req.getSession().setAttribute("captcha", captcha);
}
}
jsp部分加入图片标签:
<tr height="70">
<td>验证码</td>
<td>
<input id="captcha" class="easyui-textbox" name="captcha" class="l_user" />
<img id="img" src="generateCaptcha" title="点击刷新" width="60px" height="50px" style="vertical-align:middle;cursor:pointer;" onclick="refreshCode()"/>
<span id="errorMsg" style="color:red;margin-left:10px;"></span>
</td>
</tr>
js设置函数用于刷新验证码:
function refreshCode()
{
var t = (new Date()).getTime();
var code = "generateCaptcha?k=" + t;
$("#img").prop("src", code);
}
最终效果: