package com.yoyo.authentication.controller;
import cn.hutool.core.util.IdcardUtil;
import com.yoyo.authentication.service.TbUserAuthinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "身份認證相關介面")
@RestController
public class AuthenticationController {
@Autowired
TbUserAuthinfoService tbUserAuthinfoService;
@ApiOperation("驗證身份資訊")
@PostMapping(value = "verifyIdentityInfo")
public Object verifyIdentityInfo( @RequestParam @ApiParam(name = "idCard", value = "身份證號碼", required = true) String idCard,
@RequestParam @ApiParam(name = "realName", value = "真實姓名", required = true) String realName) throws Exception {
if (!IdcardUtil.isValidCard18(idCard)) {
throw new Exception("身份證格式不正確");
}
return tbUserAuthinfoService.verifyIdentityInfo(idCard, realName);
}
package com.yoyo.authentication.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.yoyo.authentication.service.TbUserAuthinfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@Service
public class TbUserAuthinfoServiceImpl implements TbUserAuthinfoService {
@Value("${rrk.auth.url}")
private String authurl;
@Value("${rrk.auth.secret}")
private String authsecret;
@Override
public Object verifyIdentityInfo(String idCard, String realName) {
String returnStr = addAutoAuthentication(idCard, realName);
//決議認證結果
//{"error_code":0,"reason":"認證通過","result":{"realName":"","cardNo":"","details":{"addrCode":"","birth":"","sex":0,"length":,"checkBit":"","addr":"","province":"","city":"","area":""},"isok":1},"ordersign":""}
//{"error_code":0,"reason":"無此身份證號碼","result":{"realName":"測驗","cardNo":"422202199511112325","isok":-2},"ordersign":"20190826114737073025748529"}
JSONObject jsonObject = JSONObject.parseObject(returnStr);
Object object = jsonObject.get("result");
Object reason = jsonObject.get("reason");
if (((int) ((JSONObject) object).get("isok") == -1 ) || (int) ((JSONObject) object).get("isok") == -2) {
return "姓名和身份證號不匹配";
} else if ((int) ((JSONObject) object).get("isok") == 1) {
return reason;
} else {
return "姓名和身份證號不匹配";
}
}
//添加自動實名認證
private String addAutoAuthentication(String idCard, String realName) {
//構造需要請求的內容
StringBuffer sb = new StringBuffer();
sb.append("cardNo=" + idCard);
sb.append("&realName=" + realName);
//執行認證請求
return requestGet(authurl + "/idcard", sb.toString(), authsecret);
}
/**
* 執行認證請求
*
* @param strUrl
* @param param
* @param appcode
* @return
*/
public static String requestGet(String strUrl, String param, String appcode) {
String returnStr = null; // 回傳結果定義
URL url = null;
HttpURLConnection httpURLConnection = null;
try {
url = new URL(strUrl + "?" + param);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Authorization", "APPCODE " + appcode);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET"); // get
httpURLConnection.setUseCaches(false); // 不用快取
httpURLConnection.connect();
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
returnStr = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return returnStr;
}
}
package com.yoyo.authentication;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableSwagger2Doc
@SpringBootApplication
public class AuthenticationApplication {
public static void main(String[] args) {
SpringApplication.run(AuthenticationApplication.class, args);
System.out.println("啟動成功!!!!!!!!!!!");
}
}
<dependencies>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
server:
port: 8888
#swagger配置
swagger:
base-package: com.yoyo.authentication
title: center
version: 1.0.0
exclude-path: /ops/**, /error
contact:
name: yoyo
email: [email protected]
rrk:
auth:
url: 切換自己的
type: 切換自己的
secret: 切換自己的
}
uj5u.com熱心網友回復:
單元測驗的依賴就不用貼出來了吧?uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/202672.html
標籤:Web 開發
