导语
验证码,这东西现在在互联网中可以说是刚需吧。大概凡是有登陆的地方,都会有验证码,当然了,验证码也各式各样,有静态的,动态的。静态的又分为图片的,汉字的,算术的等等,动态的分为gif的,滑块的,点击汉字的等等。下面小编就介绍两款验证码,有需要的可以收藏,旨在为大家节省自己开发的时间。
Happy Captcha
Happy Captcha 遵循Apache 2.0开源许可协议,你可以自由使用该软件。它提供了图片和动画两种展现形式,可以根据自己的需要实现。
- maven 接入,只需引入maven依赖即可。
<dependency>
<groupId>com.ramostear</groupId>
<artifactId>Happy-Captcha</artifactId>
<version>1.0.1</version>
</dependency>
- 生成验证码
以下是最基本的使用,其中request 和 response 是必传参数,其中其他的参数可选。在这种情况下,生成的验证码是图片,内容是09-azA-Z 的字符随机组合,长度为5,图片宽度160,高度50,字体微软雅黑。
@Controller
public class HappyCaptchaController{
@GetMapping("/captcha")
public void happyCaptcha(HttpServletRequest request,HttpServletResponse response){
HappyCaptcha.require(request,response).build().finish();
}
}
- 校验验证码
其中第三个参数表示是否忽略大小写,true是忽略,false是要校验大小写。
@Controller
public class CaptchaController{
@PostMapping("/verify")
public String verify(String code,HttpServletRequest request){
//Verification Captcha
boolean flag = HappyCaptcha.verification(request,code,true);
if(flag){
//Other operations...
}
}
}
- 清理验证码
验证码用了以后可以根据需要手动清理。清理也非常简单,只需要一行代码
@Controller
public class HappyCaptchaController{
@GetMapping("/remove/captcha")
public void removeCaptcha(HttpServletRequest request){
HappyCaptcha.remove(request);
}
}
- 高级特性
HappyCaptcha有几个高级特性:style, type ,length,width, height,font。
- style:有两个值,CaptchaStyle.ANIM,CaptchaStyle.IMG,前者生成动态验证码,后者生成静态图片。只需要在生成验证码的代码中加入 style(CaptchaStyle.ANIM) 就可以了。
- type有以下可选值, 加入type(CaptchaType.WORD) 即可
3.length,width,height 引入都类似,都是在代码中加入对应的,length(xxx), width(xxxx),height(xx) 等等,不做多的介绍了。
4.font 引入需要 font(Fonts.getInstance().zhFont()),一共有四种。
注意:这些特性都是可以链式调用的。
- 效果展示
EasyCaptcha
图形验证码,支持中文,算数,gif等
- maven引入
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
- 生成验证码
@Controller
public class CaptchaController {
@RequestMapping("/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
CaptchaUtil.out(request, response);
}
}
- 校验验证码
@Controller
public class LoginController {
@PostMapping("/login")
public JsonResult login(String username,String password,String verCode){
if (!CaptchaUtil.ver(verCode, request)) { //校验
CaptchaUtil.clear(request); // 清除session中的验证码
// other....
}
}
}
- 自定义验证码样式
// 设置位数
CaptchaUtil.out(5, request, response);
// 设置宽、高、位数
CaptchaUtil.out(130, 48, 5, request, response);
- 验证码类型
// png类型
SpecCaptcha captcha = new SpecCaptcha(130, 48);
// gif类型
GifCaptcha captcha = new GifCaptcha(130, 48);
// 中文类型
ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
// 中文gif类型
ChineseGifCaptcha captcha = new ChineseGifCaptcha(130, 48);
// 算术类型
ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
- 前后端分离中使用样例
@Controller
public class CaptchaController {
@Autowired
private RedisUtil redisUtil;
@ResponseBody
@RequestMapping("/captcha")
public JsonResult captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
// 存入redis并设置过期时间为30分钟
redisUtil.setEx(key, verCode, 30, TimeUnit.MINUTES);
// 将key和base64返回给前端
return JsonResult.ok().put("key", key).put("image", specCaptcha.toBase64());
}
@ResponseBody
@PostMapping("/login")
public JsonResult login(String username,String password,String verCode,String verKey){
// 获取redis中的验证码
String redisCode = redisUtil.get(verKey);
// 判断验证码
if (verCode==null || !redisCode.equals(verCode.trim().toLowerCase())) {
return JsonResult.error("验证码不正确");
}
}
}
- 效果展示
对比
这两款验证码相比各自有优缺点,第一款验证码比较丰富,但是前后端分离项目中,不好使用,没有类似redis这种存储;第二款验证码适合前后端分离,相对前者丰富性差点。不过一般的基本上能用。
总结
由于篇幅有限,今天就简单的介绍到这里了。喜欢的小伙伴点个关注+评论+赞哦。私信【验证码】 小编,获得源码地址。
本文暂时没有评论,来添加一个吧(●'◡'●)