失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > SpringBoot实现身份证实名认证(阿里云实现)

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

时间:2019-02-15 15:59:26

相关推荐

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

文章目录

1、功能展示2、购买API3、 API 文档4、SpringBoot集成身份认证 4.1、IdProperties4.2、IdAutoConfiguration4.3、完成身份认证 4.3.1抽取 GeetestForm:4.3.2、UserAuthForm4.3.3、UserController:4.3.4、UserService:4.3.5、UserServiceImpl4.3.6、代码地址:

1、功能展示

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

2、购买API

购买成功的结果:

AppKey 图上面AppSecret 图上面AppCode 图上面

123

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")@Datapublic class IdProperties {/*** 身份认证的URL地址 // https://idcert./idcard?idCard=%s&name=%s*/private String url ;/**** 你购买的appKey*/private String appKey ;/**** 你购买的appSecret*/private String appSecret ;/**** 你购买的appCode*/private String appCode ;}

1234567891011121314151617181922232425262728293031

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);// /products/57000002/cmapi022049.html?spm=5176.52.101.2.2fe57218VVSjB0#sku=yuncode1604900000if(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 ;}}

123456789101112131415161718192223242526272829303132333435363738394041424344454647484950515253545556575859606162

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@Slf4jpublic 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("验证码验证异常") ;}}}

1234567891011121314151617181922232425262728293031323334353637383940414243444546474849505152535455565758596061

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 ;}

123456789101112131415161718192223242526

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("认证失败");}

12345678910111213

4.3.4、UserService:

/*** 用户实名认证* @param id 用户的id* @param userAuthForm 认证的数据* @return*/boolean identifyVerify(Long id, UserAuthForm userAuthForm);

1234567

4.3.5、UserServiceImpl

/*** 用户实名认证** @param id 用户的id* @param userAuthForm 认证的数据* @return*/@Overridepublic 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);}

如果觉得《SpringBoot实现身份证实名认证(阿里云实现)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。