验证码的实现

榛子云短信平台用户中心
http://sms_developer.zhenzikj.com/zhenzisms_user/index.html
然后再pom中加入如下配置,告诉maven导入本地jar

   <dependency>
        <groupId>com.zhenzikj</groupId>
        <artifactId>zhenzisms</artifactId>
        <version>1.0.x</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/libs/ZhenziSmsSDK.jar</systemPath>
    </dependency>

在pom中给spring boot的打包插件设置一下includeSystemScope参数即可

       <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>
        </plugin>

这样就将包引入了,此外还要引入一个阿里巴巴的json包

   <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.49</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/libs/fastjson-1.2.49.jar</systemPath>
    </dependency>

方案一:存 session,用不同浏览器会出现问题,只能部署一台服务器

/**
     * 获取验证码
     * @param tellPhone
     * @param httpSession
     * @return
     */
    @GetMapping("/getCode")
    public String getCode(@RequestParam("tellPhone") String tellPhone,HttpSession httpSession ){

        if (!TString.isEmpty(tellPhone)){
            try {
                JSONObject json = null;
                String code = String.valueOf(new Random().nextInt(999999));
                System.out.println("验证码是:"+code+".....................");
//            String success = Integer.toString(code);
//            return  success;
                ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
                String result = client.send(tellPhone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!");
                json = JSONObject.parseObject(result);
                if (json.getIntValue("code")!= 0) {//发送短信失败
                    return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
                }
                //将验证码存到session中,同时存入创建时间
                //以json存放,这里使用的是阿里的fastjson
                json = new JSONObject();
                json.put("tellPhone",tellPhone);
                json.put("code",code);
                json.put("createTime",System.currentTimeMillis());
                // 将认证码存入SESSION
                httpSession.setAttribute("code",json);
                return createModel(new ResultBean().setMsg("发送短信成功").setSuccess(true));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));

    }

    @PostMapping(value = "checkCode")
    public String checkCode (String code,HttpSession httpSession){
        //校验验证码
        String  what = httpSession.getAttribute("code").toString();
        if (!TString.isEmpty(what)){
            JSONObject jb = JSONObject.parseObject(what);
            if (!jb.getString("code").equals(code)) {
                return createModel(new ResultBean().setMsg("验证码不对").setSuccess(false));
            }
            return createModel(new ResultBean().setMsg("验证通过").setSuccess(true));
        }
        return createModel(new ResultBean().setMsg("session为空").setSuccess(false));
    }

方案二:存redis
    /**
     * 获取验证码
     * 1、先校验手机号码格式 TODO
     * @param tellPhone
     * @param httpSession
     * @return
     */
    @GetMapping("/getCode")
    public String getCode(@RequestParam("tellPhone") String tellPhone, HttpSession httpSession ){

        if (!TString.isEmpty(tellPhone)){
            try {
                JSONObject json = null;
                String code = String.valueOf(new Random().nextInt(999999));
                System.out.println("验证码是:"+code+".....................");
//            String success = Integer.toString(code);
//            return  success;
                ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
                String result = client.send(tellPhone, "您的验证码为:" + code + ",该码有效期为5分钟,该码只能使用一次!");
                json = JSONObject.parseObject(result);
                if (json.getIntValue("code")!= 0) {//发送短信失败
                    return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));
                }
                //将验证码存到session中,同时存入创建时间
                //以json存放,这里使用的是阿里的fastjson
                json = new JSONObject();
                json.put("tellPhone",tellPhone);
                json.put("code",code);
                json.put("createTime",System.currentTimeMillis());
                // 将认证码存入SESSION
//                httpSession.setAttribute("code",json);
                RedisUtil.putOpsObject(tellPhone , json.toJSONString());

                return createModel(new ResultBean().setMsg("发送短信成功").setSuccess(true));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return createModel(new ResultBean().setMsg("发送短信失败").setSuccess(false));

    }
    /**
     * 检验验证
     * @param map
     * @param httpSession
     * @return
     */
    @PostMapping(value = "checkCode")
    public String checkCode (@RequestBody Map<String,String> map, HttpSession httpSession){
        if (TMap.isEmpty(map)){
            return createModel(new ResultBean().setMsg("验证码为空").setSuccess(false));
        }
        //校验验证码
        Map json = JSON.parseObject((String) RedisUtil.getOpsObject(map.get("tellPhone")));
        try{
            if (!TObject.isEmpty(json)){
                if (!json.get("code").equals(map.get("code"))) {
                    return createModel(new ResultBean().setMsg("验证码不对").setSuccess(false));
                }
                return createModel(new ResultBean().setMsg("验证通过").setSuccess(true));
            }
            return createModel(new ResultBean().setMsg("获取对比验证码为空").setSuccess(false));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return createModel(new ResultBean().setMsg("请重新获取验证码").setSuccess(false));
    }

参考文档:https://www.jianshu.com/p/ab3a13c5a8cb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值