使用Google提供的kaptcha实现验证码功能

  1. 选择背景:早期所里项目用的是读取式的验证码获取方式,整体大概流程就是预先加载验证码的文本(含有大量的随机字母和数字组成的一个个验证码文本),随着这次项目做微服务的架构升级和开放API的选择,最终将验证码的获取方式替换成了生成式,采用的是Google的kaptcha包提供的API(PS:如果不是系统架构的变动,这边要求一次性升级,真不想动屎山)

  1. 下面是具体的实现:

a.引入jar包

<!-- 版本大家可以按照自己项目要求来,这里做演示就随便引入的版本 -->
<!-- PS:版本选择是一个很麻烦的过程,之前接触不到所以都没关注过 -->
<kaptcha.version>0.0.9</kaptcha.version>
<dependency>
    <groupId>com.github.axet</groupId>
    <artifactId>kaptcha</artifactId>
    <version>${kaptcha.version}</version>
</dependency>

b.配置验证码的相关参数(没有配在xml文件里,直接写的配置config)

/**
 * 生成验证码配置
 */
@Configuration
public class KaptchaConfig {

    //DefaultKaptcha是Producer的实现类
    @Bean
    public DefaultKaptcha producer() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.space", "5");
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}

c.生成验证码文本和图片-CaptchaService

   //DefaultKaptcha 配置就是为了注入到Producer里的  不懂的可以去看下开发文档
    @Autowired
    private Producer producer;

    @Override
    public BufferedImage getCaptcha(String uuid) {
        //这里UUID是为了验证唯一性
        if(StringUtils.isBlank(uuid)){
            throw new RRException("uuid不能为空");
        }
        //生成文字验证码
        String code = producer.createText();
        //SysCaptchaEntity验证码实体类 *入库
        SysCaptchaEntity captchaEntity = new SysCaptchaEntity();
        captchaEntity.setUuid(uuid);
        captchaEntity.setCode(code);
        //5分钟后过期
        captchaEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), 5));
        this.save(captchaEntity);
        //生成图片
        return producer.createImage(code);
    }

d.返回给controller,推置前端,结束。

@Autowired
private CaptchaService captchaService;
/**
 * 验证码
 */
@GetMapping("captcha.jpg")
public void captcha(HttpServletResponse response, String uuid)throws IOException {
    response.setHeader("Cache-Control", "no-store, no-cache");
    response.setContentType("image/jpeg");

    //获取图片验证码
    BufferedImage image = captchaService.getCaptcha(uuid);

    ServletOutputStream out = response.getOutputStream();
    ImageIO.write(image, "jpg", out);
    IOUtils.closeQuietly(out);
}

3.上面就是使用Google的kaptcha生成验证码的过程,至于验证码的验证过程还是老的那一套,通过uuid去库中取,为空就返回特定错误给前端进行友好提示;有的话就先删除库里的验证码那条数据,然后将用户输入的验证码和库里获取到的比对,同时验证是否过期即可。这里没有做什么改动,就不贴代码了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,基于SSM框架的系统中,可以通过以下步骤来调用kaptcha实现验证功能: 1.首先需要在pom.xml文件中导入kaptcha的依赖,如下所示: ``` <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> ``` 2.在Spring的配置文件(如applicationContext.xml)中添加kaptcha的配置,如下所示: ``` <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <map> <entry key="kaptcha.border" value="no" /> <entry key="kaptcha.textproducer.font.color" value="black" /> <entry key="kaptcha.textproducer.char.space" value="5" /> <entry key="kaptcha.image.width" value="100" /> <entry key="kaptcha.image.height" value="40" /> <entry key="kaptcha.textproducer.font.size" value="30" /> <entry key="kaptcha.textproducer.char.length" value="4" /> <entry key="kaptcha.noise.color" value="black" /> <entry key="kaptcha.obscurificator.impl" value="com.google.code.kaptcha.impl.ShadowGimpy" /> </map> </constructor-arg> </bean> </property> </bean> ``` 3.在jsp页面中添加验证码的代码,如下所示: ``` <img src="${pageContext.request.contextPath}/captcha.jpg" onclick="this.src='${pageContext.request.contextPath}/captcha.jpg?'+Math.random();" /> <input type="text" name="code" /> ``` 其中,${pageContext.request.contextPath}表示项目的根路径。 4.在后台Controller中获取验证码并进行验证,如下所示: ``` @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(HttpServletRequest request, HttpServletResponse response) { String code = request.getParameter("code"); String captcha = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if (StringUtils.isEmpty(code) || !code.equalsIgnoreCase(captcha)) { //验证码错误 return "login"; } //验证码正确 //其他逻辑处理 return "index"; } ``` 其中,com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY为kaptcha在Session中保存验证码的key值。 以上就是基于SSM框架的系统中调用kaptcha实现验证功能的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值