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;
}
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 {
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;
}
DescribeCaptchaMiniResultRequest req = new DescribeCaptchaMiniResultRequest();
req.setCaptchaAppId(captchaProperties.getCaptchaAppId());
req.setAppSecretKey(captchaProperties.getAppSecretKey());
req.setCaptchaType(captchaProperties.getCaptchaType());
req.setUserIp(ip);
req.setTicket(ticket);
try {
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;
}
}
}