From c4dde0b62ac25e2975d7f24cba41eb5042e9a303 Mon Sep 17 00:00:00 2001
From: wans <747101512@qq.com>
Date: Tue, 5 Jul 2022 18:06:10 +0800
Subject: [PATCH] =?UTF-8?q?=E8=92=99=E9=80=9F=E5=8A=9E=E5=AF=B9=E6=8E=A5?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
cloud-common-msbh5/pom.xml | 7 +-
.../inspurmsbh5auth/icity/AuthUtil.java | 4 +-
.../inspurmsbh5auth/icity/CbcMsbUtil.java | 207 ++++++++++++++++++
.../icity/InspurAuthConfig.java | 51 ++++-
.../inspurmsbh5auth/icity/demoTest.java | 35 ---
.../inspurfilter/InspurAuthFilter.java | 10 +-
6 files changed, 264 insertions(+), 50 deletions(-)
create mode 100644 cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/CbcMsbUtil.java
delete mode 100644 cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/demoTest.java
diff --git a/cloud-common-msbh5/pom.xml b/cloud-common-msbh5/pom.xml
index 8a19ac1..e99368f 100644
--- a/cloud-common-msbh5/pom.xml
+++ b/cloud-common-msbh5/pom.xml
@@ -50,12 +50,17 @@
${project.basedir}/lib/json-20180813.jar
-
+
+
+ com.squareup.okhttp3
+ okhttp
+ 3.14.0
diff --git a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/AuthUtil.java b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/AuthUtil.java
index 75ddb35..ceaa1b1 100644
--- a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/AuthUtil.java
+++ b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/AuthUtil.java
@@ -12,8 +12,8 @@ import java.util.*;
public class AuthUtil {
- //private static final String BASE_URL = "https://app.zwfw.nmg.gov.cn:4443";
- private static final String BASE_URL = "http://10.12.23.222";
+ private static final String BASE_URL = "https://app.zwfw.nmg.gov.cn:4443";
+ //private static final String BASE_URL = "http://10.12.23.222";
public static class AuthToken {
diff --git a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/CbcMsbUtil.java b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/CbcMsbUtil.java
new file mode 100644
index 0000000..b6621a8
--- /dev/null
+++ b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/CbcMsbUtil.java
@@ -0,0 +1,207 @@
+package com.cm.common.inspurmsbh5auth.icity;
+
+import com.alibaba.fastjson.JSON;
+
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SignatureException;
+import java.security.spec.InvalidKeySpecException;
+import java.util.*;
+
+/**
+ * @author xwangs
+ * @create 2022-07-05 16:29
+ * @description
+ */
+public class CbcMsbUtil {
+
+ public static AuthToken getTokenByCode(String appId, String code, String privateKey) throws Exception {
+ Map params = new HashMap<>();
+ params.put("app_id", appId);
+ params.put("code", code);
+ params.put("grant_type", "authorization_code");
+ params.put("sign", getSign(params, privateKey));
+ String res = OkHttp3ClientUtil.getInstance().postSync("http://10.56.65.73:8081/apimsb/", null, params);
+ return parseAuthTokenResult(res);
+ }
+
+ private static AuthToken parseAuthTokenResult(String res) throws Exception {
+ com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(res);
+ if (jsonObject.getInteger("code") == 1000) {
+ com.alibaba.fastjson.JSONObject data = jsonObject.getJSONObject("data");
+ com.alibaba.fastjson.JSONObject authTokenJson = data.getJSONObject("auth_token");
+ AuthToken authToken = new AuthToken();
+ authToken.setToken(authTokenJson.getString("auth_token"));
+ authToken.setRefreshToken(authTokenJson.getString("refresh_token"));
+ authToken.setTokenExpiredTime(new Date(authTokenJson.getLong("token_expired_time")));
+ authToken.setRefreshTokenExpiredTime(new Date(authTokenJson.getLong("ref_token_expired_time")));
+ authToken.setOpenId(authTokenJson.getString("open_id"));
+ return authToken;
+ } else {
+ throw new Exception("get token by code error,result is :" + res);
+ }
+ }
+
+ public static String getSign(Map params, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
+ List keys = new ArrayList<>();
+ for (String k : params.keySet()) {
+ if (params.get(k) != null && params.get(k).length() > 0) {
+ keys.add(k);
+ }
+ }
+ Collections.sort(keys);
+ StringBuilder signBuilder = new StringBuilder();
+ int count = 0;
+ for (String k : keys) {
+ if (count > 0) {
+ signBuilder.append("&");
+ }
+ signBuilder.append(k);
+ signBuilder.append("=");
+ signBuilder.append(params.get(k));
+ count++;
+ }
+ return RsaSignUtil.sign(privateKey, signBuilder.toString());
+ }
+
+ /**
+ * 当使用token获取用户信息时候,token存在过期的情况,如果token过期
+ * 此时应重新授权,获取使用refresh_token刷新,
+ * 调用者应处理token过期时的 TokenExpiredException
+ */
+ public static UserAuthInfo getUserAuthInfo(String appId, String token, String privateKey) throws Exception {
+ Map params = getRequestInfoParams(appId, token, privateKey);
+ String res = OkHttp3ClientUtil.getInstance().getSync("http://10.56.65.73:8081/apimsbuser/", null, params);
+ com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(res);
+ int code = jsonObject.getInteger("code");
+ if (code == 1000) {
+ com.alibaba.fastjson.JSONObject data = jsonObject.getJSONObject("data");
+ com.alibaba.fastjson.JSONObject authInfo = data.getJSONObject("auth_info");
+ UserAuthInfo userAuthInfo = new UserAuthInfo();
+ userAuthInfo.setIdCard(authInfo.getString("id_card"));
+ userAuthInfo.setRealName(authInfo.getString("real_name"));
+ userAuthInfo.setCheckPhone(authInfo.getString("check_phone"));
+ userAuthInfo.setMobilePhone(authInfo.getString("mobile_phone"));
+ userAuthInfo.setOpenId(authInfo.getString("open_id"));
+ return userAuthInfo;
+ } else if (code == 1008) {//token已过期
+ throw new TokenExpiredException("token已过期");
+ } else {
+ throw new Exception("getUserFaceAuthInfo error,result " + res);
+ }
+ }
+
+ private static Map getRequestInfoParams(String appId, String token, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
+ Map params = new HashMap<>();
+ params.put("app_id", appId);
+ params.put("auth_token", token);
+ params.put("sign", getSign(params, privateKey));
+ return params;
+ }
+
+ /**
+ * token 内部类
+ */
+ public static class AuthToken {
+ private String token;
+ private String refreshToken;
+ private Date tokenExpiredTime;
+ private Date refreshTokenExpiredTime;
+ private String openId;
+
+
+ public AuthToken() {
+ }
+
+ public String getToken() {
+ return token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ public String getRefreshToken() {
+ return refreshToken;
+ }
+
+ public void setRefreshToken(String refreshToken) {
+ this.refreshToken = refreshToken;
+ }
+
+ public Date getTokenExpiredTime() {
+ return tokenExpiredTime;
+ }
+
+ public void setTokenExpiredTime(Date tokenExpiredTime) {
+ this.tokenExpiredTime = tokenExpiredTime;
+ }
+
+ public Date getRefreshTokenExpiredTime() {
+ return refreshTokenExpiredTime;
+ }
+
+ public void setRefreshTokenExpiredTime(Date refreshTokenExpiredTime) {
+ this.refreshTokenExpiredTime = refreshTokenExpiredTime;
+ }
+
+ public String getOpenId() {
+ return openId;
+ }
+
+ public void setOpenId(String openId) {
+ this.openId = openId;
+ }
+
+ }
+
+ public static class UserAuthInfo {
+ private String realName;
+ private String idCard;
+ private String openId;
+ private String checkPhone;
+ private String mobilePhone;
+
+
+ public String getMobilePhone() {
+ return mobilePhone;
+ }
+
+ public void setMobilePhone(String mobilePhone) {
+ this.mobilePhone = mobilePhone;
+ }
+
+ public String getOpenId() {
+ return openId;
+ }
+
+ public void setOpenId(String openId) {
+ this.openId = openId;
+ }
+
+ public String getCheckPhone() {
+ return checkPhone;
+ }
+
+ public void setCheckPhone(String checkPhone) {
+ this.checkPhone = checkPhone;
+ }
+
+
+ public String getRealName() {
+ return realName;
+ }
+
+ public void setRealName(String realName) {
+ this.realName = realName;
+ }
+
+ public String getIdCard() {
+ return idCard;
+ }
+
+ public void setIdCard(String idCard) {
+ this.idCard = idCard;
+ }
+ }
+}
diff --git a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/InspurAuthConfig.java b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/InspurAuthConfig.java
index d01238a..78cae20 100644
--- a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/InspurAuthConfig.java
+++ b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/InspurAuthConfig.java
@@ -1,8 +1,11 @@
package com.cm.common.inspurmsbh5auth.icity;
-import ink.wgink.util.ResourceUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
+import org.springframework.lang.Nullable;
+import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.StringUtils;
import java.io.*;
@@ -15,6 +18,11 @@ import java.io.*;
@ConfigurationProperties(prefix = "inspur-auth")
public class InspurAuthConfig {
+ @Nullable
+ private Class> clazz;
+ @Nullable
+ private ClassLoader classLoader;
+
private String appId;
private String redirectUri;
@@ -37,7 +45,7 @@ public class InspurAuthConfig {
public String getPrivateKey() {
String s = "";
try {
- InputStream in = ResourceUtil.getJarResourceInputStream("privateKey.txt");
+ InputStream in = getJarResourceInputStream("privateKey.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer content = new StringBuffer();
while ((s = br.readLine())!=null){
@@ -46,13 +54,46 @@ public class InspurAuthConfig {
return content.toString();
} catch (UnsupportedEncodingException e) {
System.out.println("1{{{{{{{{{{{{{{{ERROR: privateKey.txt 文件为找到!}}}}}}}}}}}}}}}}}");
- return "";
} catch (FileNotFoundException e) {
System.out.println("2{{{{{{{{{{{{{{{ERROR: privateKey.txt 文件为找到!}}}}}}}}}}}}}}}}}");
- return "";
} catch (IOException e) {
System.out.println("3{{{{{{{{{{{{{{{ERROR: privateKey.txt 文件为找到!}}}}}}}}}}}}}}}}}");
- return "";
+ } catch (Exception e) {
+ System.out.println("4{{{{{{{{{{{{{{{ERROR: privateKey.txt 文件为找到!}}}}}}}}}}}}}}}}}");
}
+ return "";
+ }
+
+ private InputStream getJarResourceInputStream(String resourcePath) throws Exception{
+ Assert.notNull(resourcePath, "Path must not be null");
+ String pathToUse = StringUtils.cleanPath(resourcePath);
+ if (pathToUse.startsWith("/")) {
+ pathToUse = pathToUse.substring(1);
+ }
+ this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
+ InputStream is;
+ if (this.clazz != null) {
+ is = this.clazz.getResourceAsStream(pathToUse);
+ }
+ else if (this.classLoader != null) {
+ is = this.classLoader.getResourceAsStream(pathToUse);
+ }
+ else {
+ is = ClassLoader.getSystemResourceAsStream(pathToUse);
+ }
+ if (is == null) {
+ StringBuilder builder = new StringBuilder("class path resource [");
+ if (this.clazz != null && ! pathToUse.startsWith("/")) {
+ builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
+ builder.append('/');
+ }
+ if (pathToUse.startsWith("/")) {
+ pathToUse = pathToUse.substring(1);
+ }
+ builder.append(pathToUse);
+ builder.append(']');
+ throw new FileNotFoundException(builder.toString() + " cannot be opened because it does not exist");
+ }
+ return is;
}
}
diff --git a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/demoTest.java b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/demoTest.java
deleted file mode 100644
index 0ce18e7..0000000
--- a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/icity/demoTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.cm.common.inspurmsbh5auth.icity;
-
-public class demoTest {
-
- public static void main(String[] args) throws Exception {
- String appId = "9cadc6c3-53df-407f-b83f-fe42f36a3488";
- String code = "b9a8ffc0-8a50-11ec-9433-094c0f9bd754";
- String privateKey = "-----BEGIN PRIVATE KEY-----\n" +
- "MIICXAIBAAKBgQCs5mYn9oTlN4tKWkPSdfkhrp1IouzfggNIJvQBMkCqffc3qLAT\n" +
- "Gd3Jh245hdUrDIi3L/r5H3b+MwAgNxX52ywiXdrEhk7FxtHRXTZJ8YNxmDuL8XqR\n" +
- "H1DcNaDPS74Qx80EsXjSzhQQ+GCvJ8cUw2d+TbxM1KFo6I2nCJP/m+0ChQIDAQAB\n" +
- "AoGAOMeI8MLCLcwp0AcIksrvP6JqQHpQs62sijtQkjwAhHzNfNfGyt8JuK7jAbzz\n" +
- "nimFlDMLhlhQ6XWyKuIsM1tIiI4mZs1Nji/9mJoMylRjqKrZDI213nSY7FHq2CCf\n" +
- "Ti8dVSlwLzfEM+2y2/R9MyX7tqmFxvuDfgCGSxK4z9hhEIECQQDh1/BrdnGyx68t\n" +
- "bckXuZmSqGk1THGk9zyh4FAuBdXjaRVUwOKdSnE6evCrdsVUSAPtGHLeuxJDQJUK\n" +
- "iV11XVLRAkEAw/yt8yji2Fqhau5xd/c4yrj9+JnK8qla7yNFPgheHpv65BbTwzgz\n" +
- "pJmGuwJnVByLUyVnU6w56FxG0SbjgqPZdQJAC5lxqbNNp6cF4klvOJ65uoSX0rr4\n" +
- "Y0angJDqzVJTZ/ivtciqWvXF1wEVd1kETY3dVcIZtELbb0TT4tSnTi3a4QJAdsLK\n" +
- "Idv3wkUBJYwB5EFB/84j0B5Zlbw8J0cHMXTem8vHN6oBfn2zBZ4mv3HQbW6ymprC\n" +
- "xzhJCt3H5/uZx9ND9QJBALt/kH8vSHEo/JLZk87SX7NQwk9OCLpaedPRIiB3dA5U\n" +
- "US1pl0Ixp/acwuUfsvZ4IAcNW9FC0+Qs0xowKMJIiiY=\n" +
- "-----END PRIVATE KEY-----";
-
- AuthUtil.AuthToken token = AuthUtil.getTokenByCode(appId, code, privateKey);
- //System.out.println(token);
-
- //AuthUtil.UserAuthInfo userFaceInfo = AuthUtil.getUserAuthInfo(appId, token.getToken(), privateKey);
- //System.out.println(userFaceInfo.getFaceUrl());
- //System.out.println(userFaceInfo.getIdCard());
- //System.out.println(userFaceInfo.getOpenId());
- //System.out.println(userFaceInfo.getRealName());
- //System.out.println(userFaceInfo.getMobilePhone());
-//
- }
-}
diff --git a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/inspurfilter/InspurAuthFilter.java b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/inspurfilter/InspurAuthFilter.java
index 3223902..d534af8 100644
--- a/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/inspurfilter/InspurAuthFilter.java
+++ b/cloud-common-msbh5/src/main/java/com/cm/common/inspurmsbh5auth/inspurfilter/InspurAuthFilter.java
@@ -1,10 +1,8 @@
package com.cm.common.inspurmsbh5auth.inspurfilter;
-import com.cm.common.inspurmsbh5auth.icity.AuthUtil;
+import com.cm.common.inspurmsbh5auth.icity.CbcMsbUtil;
import com.cm.common.inspurmsbh5auth.icity.InspurAuthConfig;
import com.cm.common.inspurmsbh5auth.icity.InspurUserInfo;
-import ink.wgink.pojo.dtos.user.UserDTO;
-import ink.wgink.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -14,8 +12,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
/**
* @author xwangs
@@ -52,8 +48,8 @@ public class InspurAuthFilter implements Filter {
}
if(code != null) {
try {
- AuthUtil.AuthToken token = AuthUtil.getTokenByCodeV2(appId, code, privateKey);
- AuthUtil.UserAuthInfo userAuthInfo = AuthUtil.getUserAuthInfoV2(appId, token.getToken(), privateKey);
+ CbcMsbUtil.AuthToken token = CbcMsbUtil.getTokenByCode(appId, code, privateKey);
+ CbcMsbUtil.UserAuthInfo userAuthInfo = CbcMsbUtil.getUserAuthInfo(appId, token.getToken(), privateKey);
InspurUserInfo inspurUser = new InspurUserInfo();
try {
inspurUser.setRealName(userAuthInfo.getRealName());