栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

easy

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

easy

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

1、maven方式引入
	
      com.github.whvcse
      easy-captcha
      1.6.2
   
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());
    }   
}

业务层信息

	
	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和端口号选自己的)

返回结果:

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462340.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号