springBoot对接微信图形验证码

springBoot对接微信图形验证码

pom

		<dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>4.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>

工具类

package com.ztsk.framework.gateway.service.impl;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaMiniResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaMiniResultResponse;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.ztsk.framework.gateway.config.BaseConstant;
import com.ztsk.framework.gateway.config.captcha.CaptchaProperties;
import com.ztsk.framework.gateway.service.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class CaptchaServiceImpl {

    @Resource
    private CaptchaClient client;
    @Resource
    private CaptchaProperties captchaProperties;

    @Override
    public Boolean checkCaptcha(String ticket, String randStr, String ip, String channel) {
        if (StringUtils.isBlank(ip)){
            ip = "127.0.0.1";
        }
        if ("2".equals(channel)){
            return miniCheckCaptcha(ticket,ip);
        }else {
            return checkCaptcha(ticket,randStr,ip);
        }
    }

    private Boolean checkCaptcha(String ticket, String randStr, String ip){
        if (StringUtils.isBlank(ticket) || StringUtils.isBlank(randStr)){
            return false;
        }
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeCaptchaResultRequest req = new DescribeCaptchaResultRequest();
        req.setCaptchaAppId(captchaProperties.getCaptchaAppId());
        req.setAppSecretKey(captchaProperties.getAppSecretKey());
        req.setCaptchaType(captchaProperties.getCaptchaType());
        req.setRandstr(randStr);
        req.setUserIp(ip);
        req.setTicket(ticket);
        try {
            // 返回的resp是一个DescribeCaptchaResultResponse的实例,与请求对象对应
            DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);
            log.info("UserServiceImpl checkCaptcha resp:{}", JSON.toJSONString(resp));
            if (BaseConstant.OK.equals(resp.getCaptchaCode())) {
                return true;
            }
            return false;
        } catch (TencentCloudSDKException e) {
            log.error("UserServiceImpl checkCaptcha error:", e);
            return false;
        }
    }

    private Boolean miniCheckCaptcha(String ticket, String ip){
        if (StringUtils.isBlank(ticket)){
            return false;
        }
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeCaptchaMiniResultRequest req = new DescribeCaptchaMiniResultRequest();
        req.setCaptchaAppId(captchaProperties.getCaptchaAppId());
        req.setAppSecretKey(captchaProperties.getAppSecretKey());
        req.setCaptchaType(captchaProperties.getCaptchaType());
        req.setUserIp(ip);
        req.setTicket(ticket);
        try {
            // 返回的resp是一个DescribeCaptchaResultResponse的实例,与请求对象对应
            DescribeCaptchaMiniResultResponse resp = client.DescribeCaptchaMiniResult(req);
            log.info("UserServiceImpl checkCaptcha resp:{}", JSON.toJSONString(resp));
            if (BaseConstant.OK.equals(resp.getCaptchaCode())) {
                return true;
            }
            return false;
        } catch (TencentCloudSDKException e) {
            log.error("UserServiceImpl checkCaptcha error:", e);
            return false;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值