文章目录


 ​​https://blog.51cto.com/u_15254813​

1、功能展示

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot

地址:​​阿里云身份证实名认证​

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot_02

2、购买API

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot_03

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot_04

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot_05

SpringBoot实现身份证实名认证(阿里云实现)_SpringBoot_06

购买成功的结果:

AppKey      图上面
AppSecret 图上面
AppCode 图上面
  • 1
  • 2
  • 3

3、 API 文档

地址:​​api文档​

4、SpringBoot集成身份认证

4.1、IdProperties

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 ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

4.2、IdAutoConfiguration

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());

ResponseEntityresponseEntity = 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 ;
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

4.3、完成身份认证

4.3.1抽取 GeetestForm:

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, RedisTemplateredisTemplate){
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地址
*/
MapparamMap = new HashMap();
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("验证码验证异常") ;
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

4.3.2、UserAuthForm

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 ;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

4.3.3、UserController:

@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("认证失败");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4.3.4、UserService:

/**
* 用户实名认证
* @param id 用户的id
* @param userAuthForm 认证的数据
* @return
*/
boolean identifyVerify(Long id, UserAuthForm userAuthForm);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4.3.5、UserServiceImpl

/**
* 用户实名认证
*
* @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);
}