请配合我的 极验验证一起看,这里面有些代码在这
AppKey 图上面
AppSecret 图上面
AppCode 图上面
地址:api文档
package com.zhz.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "identify")
@Data
public class IdProperties {
/**
* 身份认证的URL地址 // https://idcert.market.alicloudapi.com/idcard?idCard=%s&name=%s
*/
private String url ;
/***
* 你购买的appKey
*/
private String appKey ;
/***
* 你购买的appSecret
*/
private String appSecret ;
/***
* 你购买的appCode
*/
private String appCode ;
}
package com.zhz.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
@Configuration
@EnableConfigurationProperties(IdProperties.class)
public class IdAutoConfiguration {
private static IdProperties idProperties;
/**
* 发请求的工具
*/
private static RestTemplate restTemplate = new RestTemplate() ;
public IdAutoConfiguration(IdProperties idProperties){
IdAutoConfiguration.idProperties = idProperties ;
}
/**
* 用户信息的实名认证
* @param realName
* 用户的真实信息
* @param cardNum
* 用户的身份证号
* @return
* 验证的结果
*/
public static boolean check(String realName ,String cardNum){
/**
* 本次请求我们是AppCode的形式验证: Authorization:APPCODE 你自己的AppCode
* -H Authorization:APPCODE 你自己的AppCode
*/
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","APPCODE "+idProperties.getAppCode());
ResponseEntity<String> responseEntity = restTemplate.exchange(
//%s 是变量,
String.format(idProperties.getUrl(), cardNum, realName),
HttpMethod.GET,
new HttpEntity<>(null, httpHeaders),
String.class
);
// https://market.aliyun.com/products/57000002/cmapi022049.html?spm=5176.2020520132.101.2.2fe57218VVSjB0#sku=yuncode1604900000
if(responseEntity.getStatusCode()== HttpStatus.OK){
String body = responseEntity.getBody();
JSONObject jsonObject = JSON.parseObject(body);
String status = jsonObject.getString("status");
if("01".equals(status)){
// 验证成功
return true ;
}
}
return false ;
}
}
package com.zhz.model;
import com.alibaba.fastjson.JSON;
import com.zhz.geetest.GeetestLib;
import com.zhz.geetest.GeetestLibResult;
import com.zhz.util.IpUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap;
import java.util.Map;
@Data
@Slf4j
public class GeetestForm {
/**
* 极验的数据包
*/
private String geetest_challenge ;
private String geetest_seccode ;
private String geetest_validate ;
private String uuid ;
public void check(GeetestLib geetestLib, RedisTemplate<String,Object> redisTemplate){
String challenge = this.getGeetest_challenge();
String validate = this.getGeetest_validate();
String seccode = this.getGeetest_seccode();
int status = 0;
String userId = "";
// session必须取出值,若取不出值,直接当做异常退出
String statusStr = redisTemplate.opsForValue().get(GeetestLib.GEETEST_SERVER_STATUS_SESSION_KEY).toString();
status = Integer.valueOf(statusStr).intValue();
userId = redisTemplate.opsForValue().get(GeetestLib.GEETEST_SERVER_USER_KEY + ":" + this.getUuid()).toString();
GeetestLibResult result = null;
if (status == 1) {
/*
自定义参数,可选择添加
user_id 客户端用户的唯一标识,确定用户的唯一性;作用于提供进阶数据分析服务,可在register和validate接口传入,不传入也不影响验证服务的使用;若担心用户信息风险,可作预处理(如哈希处理)再提供到极验
client_type 客户端类型,web:电脑上的浏览器;h5:手机上的浏览器,包括移动应用内完全内置的web_view;native:通过原生sdk植入app应用的方式;unknown:未知
ip_address 客户端请求sdk服务器的ip地址
*/
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("user_id", userId);
paramMap.put("client_type", "web");
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
paramMap.put("ip_address", IpUtil.getIpAddr(servletRequestAttributes.getRequest()));
result = geetestLib.successValidate(challenge, validate, seccode, paramMap);
log.info("验证的结果为{}", JSON.toJSONString(result));
} else {
result = geetestLib.failValidate(challenge, validate, seccode);
}
if(result.getStatus()!=1){
log.error("验证异常",JSON.toJSONString(result,true));
throw new IllegalArgumentException("验证码验证异常") ;
}
}
}
package com.zhz.model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Data
@ApiModel(value = "用户的身份认证信息")
public class UserAuthForm extends GeetestForm{
@NotBlank
@ApiModelProperty(value = "用户的真实名称")
private String realName ;
@NotNull
@ApiModelProperty(value = "用户的证件类型")
private Integer idCardType ;
@NotBlank
@ApiModelProperty(value = "用户的证件号码")
private String idCard ;
}
@PostMapping("/authAccount")
@ApiOperation(value = "用户的实名认证")
@ApiImplicitParams({
@ApiImplicitParam(name = "userAuthForm", value = "userAuthForm的数据")
})
public R identifyCheck(@RequestBody UserAuthForm userAuthForm) {
String idStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
boolean isOk = userService.identifyVerify(Long.valueOf(idStr), userAuthForm);
if (isOk) {
return R.ok();
}
return R.fail("认证失败");
}
/**
* 用户实名认证
* @param id 用户的id
* @param userAuthForm 认证的数据
* @return
*/
boolean identifyVerify(Long id, UserAuthForm userAuthForm);
/**
* 用户实名认证
*
* @param id 用户的id
* @param userAuthForm 认证的数据
* @return
*/
@Override
public boolean identifyVerify(Long id, UserAuthForm userAuthForm) {
User user = getById(id);
Assert.notNull(user, "认证的用户不存在");
Byte authStatus = user.getAuthStatus();
if (!authStatus.equals((byte) 0)) {
throw new IllegalArgumentException("该用户已经认证成功");
}
//执行认证
checkForm(userAuthForm);//极验
//实名认证
boolean check = IdAutoConfiguration.check(userAuthForm.getRealName(), userAuthForm.getIdCard());
if (!check) {
throw new IllegalArgumentException("该用户信息错误,请检查");
}
//设置用户的值
user.setAuthtime(new Date());
user.setAuthStatus((byte) 1);
user.setRealName(userAuthForm.getRealName());
user.setIdCard(userAuthForm.getIdCard());
user.setIdCardType(userAuthForm.getIdCardType());
return updateById(user);
}
private void checkForm(UserAuthForm userAuthForm) {
userAuthForm.check(geetestLib, redisTemplate);
}
https://gitee.com/zhouzhz/coin-exchange/tree/master/coin-member/member-service/src/main/java/com/zhz