SpringBoot实现身份证实名认证(阿里云实现)
程序员文章站
2022-03-26 15:49:23
文章目录1、功能展示2、购买API3、 API 文档1、功能展示地址:阿里云身份证实名认证2、购买API购买成功的结果:AppKey 图上面AppSecret 图上面AppCode 图上面3、 API 文档地址:api文档...
请配合我的 极验验证一起看,这里面有些代码在这
文章目录
1、功能展示
地址:阿里云身份证实名认证
2、购买API
购买成功的结果:
AppKey 图上面
AppSecret 图上面
AppCode 图上面
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 ;
}
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());
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 ;
}
}
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, 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("验证码验证异常") ;
}
}
}
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 ;
}
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("认证失败");
}
4.3.4、UserService:
/**
* 用户实名认证
* @param id 用户的id
* @param userAuthForm 认证的数据
* @return
*/
boolean identifyVerify(Long id, UserAuthForm userAuthForm);
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);
}
4.3.6、代码地址:
https://gitee.com/zhouzhz/coin-exchange/tree/master/coin-member/member-service/src/main/java/com/zhz
本文地址:https://blog.csdn.net/zhouhengzhe/article/details/112131087
推荐阅读
-
SpringBoot 2.0整合阿里云OSS,实现动静分离架构
-
SpringBoot整合阿里云OSS对象存储服务实现文件上传
-
springboot整合阿里云OSS实现文件上传
-
Springboot2整合阿里云OSS实现文件上传、下载、删除、查看
-
SpringBoot整合阿里云OSS对象存储服务的实现
-
SpringBoot实现阿里云快递物流查询的示例代码
-
SpringBoot+阿里云OSS实现在线视频播放的示例
-
SpringBoot实现身份证实名认证(阿里云实现)
-
springboot-springsecurity调用阿里云API发送短信验证码实现账号注册
-
SpringBoot整合阿里云OSS对象存储服务实现文件上传