Java开发者每天都在用的验证码功能,竟然是这么实现的「已开源」

今天给大家推荐的这个项目是「Happy Captcha」,一款易于使用的 Java 验证码软件包,旨在花最短的时间,最少的代码量,实现 Web 站点的验证码功能。

Happy Captcha 提供了图片和动画两种展现形式,验证码内容包括:

  • 中文(收录 3500 个常用汉字)
  • 阿拉伯数字 (09)
  • 中文数字 (零至九)
  • 中文大写数字 (零至玖)
  • 数字与字母混合 (09-az-AZ)
  • 数字与小写字母混合 (09-az)
  • 数字与大写字母混合 (09-AZ)
  • 纯小写字母
  • 纯大写字母
  • 大小写字母混合
  • 运算表达式(阿拉伯数字运算表达式和中文运算表达式)

等 12 种类型。

先体验一下效果:

image

安装:

如果你的项目使用的是 Maven 进行依赖管理,你只需向 pom.xml 文件添加下面的配置即可:

<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

<dependency>
    <groupId>com.ramostear</groupId>
    <artifactId>Happy-Captcha</artifactId>
    <version>1.0.1</version>
</dependency>

</pre>

Gradle 用户则可以通过引入如下的配置获取 Happy Captcha:

<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

implementation 'com.ramostear:Happy-Captcha:1.0.1'

</pre>

使用:

HappyCaptcha 在设计时力求过程的简洁,在默认情况下,你只需要书写一行代码即可生成漂亮的验证码图片。

下面是 HappyCaptcha的 使用示例:

<pre style="box-sizing: border-box; outline: none; margin: 0px; padding: 0px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 18px; line-height: inherit; font-family: couriernew, courier, monospace; vertical-align: baseline; color: rgb(93, 93, 93); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">

@Controller
public class HappyCaptchaController{
   @GetMapping("/captcha")
   public void happyCaptcha(HttpServletRequest reqeust,HttpServletResponseresponse){
       HappyCaptcha.require(request,response).build().finish();
  }
}

</pre>

对于 HappyCaptcha 而言,只有 request 和 response 是必须提供的参数,其余参数都可以使用缺省值。

在默认情况下,HappyCaptcha 生成的验证码以图片形式展现,内容为 09-az-A~Z 的字符随机组合,字符长度为 5,图片宽度为 160,高度为 50,字体为微软雅黑。

开源项目地址:https://github.com/ramostear/Happy-Captcha

开源项目作者:ramostear

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值