问题原因
这是你网络游戏防沉迷实名认证系统还没通过调试接口,需要先将网络游戏防沉迷后台的调试接口通过。
解决步骤
一、白名单配置
二、接口测试
1、页面点击开始测试
点击开始测试后获取测试码
注意:此处白名单需要填写正确
2、下载说明文档
3、接口文档
4、Java代码
4.1、测试接口
/**
* 测试实名认证
*/
@RequestMapping(value = "/testIdAuth.do", method = {RequestMethod.POST, RequestMethod.GET})
public String testIdAuth(@RequestParam(value = "url") String url,
@RequestParam(value = "ai") String ai,
@RequestParam(value = "name") String name,
@RequestParam(value = "idNum") String idNum,
@RequestParam(value = "bizId") String bizId,
@RequestParam(value = "type") String type,
@RequestParam(value = "si") String si,
@RequestParam(value = "isTourists") String isTourists
) {
// String url = "https://wlc.nppa.gov.cn/test/authentication/check/" + testCode;
String timestamps = System.currentTimeMillis() + "";
String authResult = "";
if ("1".equals(type)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("ai", ai);
jsonObject.put("name", name);
jsonObject.put("idNum", idNum);
/**加密请求参数*/
String aesData = secureParams("Secret Key", jsonObject);
/**生成签名*/
String sign = signParams("APPID", bizId, "Secret Key", timestamps, null, aesData);
/**接口调用*/
authResult = HttpRequest.post(url)
.header(Header.CONTENT_TYPE, "application/json; charset=utf-8")
.header("appId", "APPID").header("bizId", bizId).header("timestamps", timestamps)
.header("sign", sign)
.body(aesData)
.timeout(5000)
.execute().body();
}else if ("2".equals(type)) {
Map<String, Object> params = Maps.newHashMap();
params.put("ai", ai);
Map<String, String> paramsData = Maps.newHashMap();
paramsData.put("ai", ai);
/**签名*/
String sign = signParams("APPID", bizId, "Secret Key", timestamps, paramsData, null);
authResult = HttpRequest.get(url)
.header(Header.CONTENT_TYPE, "application/json; charset=utf-8")
.header("appId", "APPID").header("bizId", bizId).header("timestamps", timestamps)
.header("sign", sign)
.form(params)
.timeout(5000)
.execute().body();
}else if ("3".equals(type)) {
JSONObject data = new JSONObject();
JSONArray collections = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("no", 1);
jsonObject.put("si", si);
jsonObject.put("bt", 1);
jsonObject.put("ot", DateUtil.currentSeconds() - 2);
if ("2".equals(isTourists)) {
jsonObject.put("ct", 2);
jsonObject.put("di", "9wawbt7vij7bxm0nx3wxefxbmswyscx0");
}else {
jsonObject.put("ct", 0);
jsonObject.put("pi", ai);
}
collections.add(jsonObject);
data.put("collections", collections);
/**加密请求参数*/
String aesData = secureParams("Secret Key", data);
/**生成签名*/
String sign = signParams("APPID", bizId, "Secret Key", timestamps, null, aesData);
/**接口调用*/
authResult = HttpRequest.post(url)
.header(Header.CONTENT_TYPE, "application/json; charset=utf-8")
.header("appId", "APPID").header("bizId", bizId).header("timestamps", timestamps)
.header("sign", sign)
.body(aesData)
.timeout(5000)
.execute().body();
}
return authResult;
}
4.2、工具接口
/**
* 加密请求参数
*
* @param jsonObject
* @return
*/
public static String secureParams(String secretKey, JSONObject jsonObject) {
String params = jsonObject.toJSONString();
return secureParams(secretKey, params);
}
/**
* 加密请求参数
*
* @param jsonArray
* @return
*/
public static String secureParams(String secretKey, JSONArray jsonArray) {
String params = JSONUtil.toJsonStr(jsonArray);
return secureParams(secretKey, params);
}
/**
* 加密请求参数
*
* @param params
* @return
*/
public static String secureParams(String secretKey, String params) {
String secureParams = aesEncrypt(params, secretKey);
JSONObject paramsJson = new JSONObject();
paramsJson.put("data", secureParams);
return paramsJson.toJSONString();
}
/**
* AES-128/GCM + BASE64算法加密
*
* @param content
* @param secretKey
* @return
*/
private static String aesEncrypt(String content, String secretKey) {
try {
byte[] hexStr = HexUtils.fromHexString(secretKey);
//加密算法:AES/GCM/PKCS5Padding
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5Padding");
SecretKeySpec skeySpec = new SecretKeySpec(hexStr, "AES");
//随机生成iv 12位
byte[] iv = new byte[12];
SecureRandom.getInstance("SHA1PRNG").nextBytes(iv);
//数据加密, AES-GCM-128
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new GCMParameterSpec(128, iv));
byte[] encrypted = cipher.doFinal(content.getBytes()); //数据加密
//iv+加密数据 拼接 iv在前,加密数据在后
ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + encrypted.length);
byteBuffer.put(iv);
byteBuffer.put(encrypted);
byte[] cipherMessage = byteBuffer.array();
//转换为Base64 Base64算法有多种变体, 这里使用的是java.util.Base64
return Base64.getEncoder().encodeToString(cipherMessage);
} catch (Exception e) {
log.error("content:" + content, e);
}
return null;
}
/**
* 参数签名
*
* @return
*/
public static String signParams(String appId, String bizId, String secretKey, String timestamps, Map<String, String> paramsData, String aesData) {
HashMap<String, String> params = Maps.newHashMap();
params.put("appId", appId);
params.put("bizId", bizId);
params.put("timestamps", timestamps);
if (paramsData != null) {
params.putAll(paramsData);
}
TreeMap<String, String> sortedParams = new TreeMap<>(params);
// 遍历排序后的字典,将所有参数按"key=value"格式拼接在一起
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> param : sortedParams.entrySet()) {
String value = param.getValue();
if (StrKit.isBlank(value)) {
continue;
}
sb.append(param.getKey()).append(value);
}
String signString = secretKey + sb.toString();
if (aesData != null) {
signString += aesData;
}
System.out.println(signString);
String sign = SecureUtil.sha256(signString);
return sign;
}