easy_captcha生成算术登录验证码负数验证

废话不多说上代码,Ctrl+C 然后Ctrl+V 自己改吧改吧就阔以了。

1、maven方式引入

	<dependency>
      <groupId>com.github.whvcse</groupId>
      <artifactId>easy-captcha</artifactId>
      <version>1.6.2</version>
   </dependency>

2、项目中使用

控制层使用信息

@Controller
public class userController {
    ......
    @GetMapping("/validateCode")
    public void validateCode(HttpServletRequest request, HttpServletResponse response) throws ScriptException, IOException {
    	response.setHeader("Cache-Control", "no-cache");
    	//算术验证码生成
        ArithmeticCaptcha arithmeticCaptcha = userService.checkResult();
        // 获取运算的公式:3+2*7=? 这里的结果不会为负数
        String verifyCode = arithmeticCaptcha.getArithmeticString();  
        log.info("validateCode1:"+verifyCode);
        Session session = SecurityUtils.getSubject().getSession();
        session.removeAttribute("validateCode");
        session.setAttribute("validateCode", arithmeticCaptcha.text());
        response.setContentType("image/jpeg");
        arithmeticCaptcha.out(response.getOutputStream());
    }   
}

业务层信息

	/**校验计算公式结果
     * @return
     * @author nicai
     */
	public static ArithmeticCaptcha checkResult() throws ScriptException {
		//生成算术验证码
        ArithmeticCaptcha arithmeticCaptcha= new ArithmeticCaptcha(140 , 30);
        arithmeticCaptcha.setLen(3);  // 几位数运算,默认是两位
        //计算结果
        int sum = Integer.parseInt(arithmeticCaptcha.text());
        if (sum < 0) {
            //算术验证码
            ArithmeticCaptcha arithmeticCaptcha1 = new ArithmeticCaptcha(140 , 30);
            arithmeticCaptcha1.setLen(3);  // 几位数运算,默认是两位
            return checkResult();
        }
        return arithmeticCaptcha;
    }

3、前端请求、或者Postman请求

路径:127.0.0.1:8080/tsenglish/user/validateCode(IP和端口号选自己的)

返回结果:
在这里插入图片描述

总结: 这玩楞真好用,代码不用写那么多,还简单。总结后感觉就俩字:“啊,真香!”。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Koa2 中使用 svg-captcha 生成验证码可以按照以下步骤进行: 1. 安装 svg-captcha 和 koa-svgrouter 模块 ```bash npm install svg-captcha koa-svgrouter --save ``` 2. 在 Koa2 应用中引入相关模块 ```javascript const Koa = require('koa'); const Router = require('koa-router'); const svgrouter = require('koa-svgrouter'); const svgCaptcha = require('svg-captcha'); const app = new Koa(); const router = new Router(); ``` 3. 设置路由,生成验证码 ```javascript router.get('/captcha', async (ctx, next) => { const captcha = svgCaptcha.create(); ctx.session.captcha = captcha.text; ctx.type = 'svg'; ctx.body = captcha.data; }); ``` 4. 添加 SVG 路由 ```javascript app.use(svgrouter('/captcha/:id', { useSession: true, sessionName: 'captcha' })); ``` 5. 在需要使用验证码的地方,可以通过如下代码获取验证码图片 ```html <img src="/captcha/1" /> ``` 注意,上述代码中的数字 1 是路由参数,可以自行设置。 6. 验证验证码 在需要验证验证码的地方,可以通过如下代码获取用户输入的验证码,并与之前生成验证码进行比较。 ```javascript const userCaptcha = ctx.request.body.captcha; if (userCaptcha.toLowerCase() === ctx.session.captcha.toLowerCase()) { // 验证码输入正确 } else { // 验证码输入错误 } ``` 以上就是使用 svg-captcha 生成验证码的基本步骤。需要注意的是,在生成验证码验证验证码时都需要使用到 Koa2 的 session 功能,因此需要在应用中添加相关中间件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值