Java验证码 patchca
Java验证码 patchca
- maven依赖
- 工具类
- 访问效果
maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<springboot.version>2.1.3.RELEASE</springboot.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.bingoohuang</groupId>
<artifactId>patchca</artifactId>
<version>0.0.1</version>
</dependency>
工具类
/** * 验证码工具类 * * @author zhengzc * @date 2019-08-01 */
public class CaptchaUtil {
public static ConfigurableCaptchaService getConfigurableCaptchaService() {
ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
RandomWordFactory wf = new RandomWordFactory();
wf.setCharacters("23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ");
wf.setMaxLength(4);
wf.setMinLength(4);
cs.setWordFactory(wf);
return cs;
}
/** * 选择验证码生成工厂 * * @param cs * @param index */
public static void setFactory(ConfigurableCaptchaService cs, int index) {
System.err.println("工厂序号:" + index);
if (index == 0) {
// 弧线波纹
cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
} else if (index == 1) {
// 大理石波纹
cs.setFilterFactory(new MarbleRippleFilterFactory());
} else if (index == 2) {
// 大理石波纹
cs.setFilterFactory(new DoubleRippleFilterFactory());
} else if (index == 3) {
// 摇摆波纹
cs.setFilterFactory(new WobbleRippleFilterFactory());
} else if (index == 4) {
// 扩散的波纹
cs.setFilterFactory(new DiffuseRippleFilterFactory());
}
}
/** * 选择验证码生成工厂 * * @param cs * @param index */
public static void setColorFactory(ConfigurableCaptchaService cs, int index) {
System.err.println("颜色序号:" + index);
if (index == 0) {
// 自定义颜色
cs.setColorFactory(getColorFactory());
} else if (index == 1) {
// 梯度变化颜色
cs.setColorFactory(new GradientColorFactory());
} else if (index == 2) {
// 随机颜色
cs.setColorFactory(new RandomColorFactory());
} else if (index == 3) {
// 单一颜色
cs.setColorFactory(new SingleColorFactory());
}
}
/** * 自定义颜色工厂 * * @return */
private static ColorFactory getColorFactory() {
return new ColorFactory() {
@Override
public Color getColor(int x) {
Random random = new Random();
int[] c = { 25, 60, 170};
int i = random.nextInt(c.length);
for (int fi = 0; fi < c.length; fi++) {
if (fi == i) {
c[fi] = random.nextInt(71);
} else {
c[fi] = random.nextInt(256);
}
}
return new Color(c[0], c[1], c[2]);
}
};
}
}
/** * 使用谷歌提供的验证码组件patchca来生成验证码 * * @author zhengzc * @date 2019-08-01 */
@Controller
@RequestMapping("/patchca")
public class CaptchaController {
@RequestMapping(value = "/getpatchcaImage")
public void getpatchcaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
ConfigurableCaptchaService cs = CaptchaUtil.getConfigurableCaptchaService();
CaptchaUtil.setFactory(cs, new Random().nextInt(5));
CaptchaUtil.setColorFactory(cs, new Random().nextInt(4));
HttpSession session = request.getSession(false);
if (session == null) {
session = request.getSession();
}
setResponseHeaders(response);
String token = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());
session.setAttribute("captchaToken", token);
System.out.println("当前的 sessionId:" + session.getId() + ",验证码:" + token);
}
protected void setResponseHeaders(HttpServletResponse response) {
response.setContentType("image/jpeg");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.setHeader("Pragma", "no-cache");
long time = System.currentTimeMillis();
response.setDateHeader("Last-Modified", time);
response.setDateHeader("Date", time);
response.setDateHeader("Expires", time);
}
}
访问效果
http://localhost:8080/patchca/getpatchcaImage
- [0,1]
- [0,3]
- [1,2]
- [1,3]
- [2,1]
- [2,3]
- [3,0]
- [3,1]
- [4,0]
还没有评论,来说两句吧...