package com.fr.plugin; import com.fr.json.JSONObject; import com.fr.third.org.apache.http.client.methods.CloseableHttpResponse; import com.fr.third.org.apache.http.client.methods.HttpGet; import com.fr.third.org.apache.http.impl.client.CloseableHttpClient; import com.fr.third.org.apache.http.impl.client.HttpClients; import com.fr.third.org.apache.http.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; public class SsoHttpUtil { private static String publicKey = ""; public static String getPublicKey() { return publicKey; } public static void initPublicKey() throws IOException { publicKey = getPublickey(); } /** * @return 公钥 publickey */ public static String getPublickey() throws IOException { MTConfig mtConfig = MTConfig.getInstance(); String apiUrl = mtConfig.getApiUrl(); String clientId = mtConfig.getClientId(); String clientSecret = mtConfig.getClientSecret(); String url = apiUrl + "/fedauth/api/publickey"; HttpGet httpGet = new HttpGet(url); // 设置header String baUri = "/fedauth/api/publickey"; Map map = SsoHttpUtil.getSignedHeaders("GET", baUri, clientId, clientSecret); httpGet.addHeader("Authorization", map.get("Authorization")); httpGet.addHeader("Content-Type", map.get("Content-Type")); httpGet.addHeader("Date", map.get("Date")); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { String responseString = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject jsonObject = new JSONObject(responseString); JSONObject data = jsonObject.getJSONObject("data"); return data.getString("publicKey"); } return null; } public static Map getSignedHeaders(String method, String uri, String key, String token) { String date = BaUtil.getAuthDate(new Date()); method = method.toUpperCase(); String authorization = BaUtil.getAuthorization(uri, method, date, key, token); Map headers = new HashMap<>(); headers.put("Authorization", authorization); headers.put("Content-Type", "application/json"); headers.put("Date", date); return headers; } private static class BaUtil { public static String getAuthDate(Date date) { DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH); df.setTimeZone(TimeZone.getTimeZone("GMT")); return df.format(date); } public static String getAuthorization(String uri, String method, String date, String clientId, String secret) { String stringToSign = method + " " + uri + "\n" + date; String signature = getSignature(stringToSign, secret); String authorization = "MWS " + clientId + ":" + signature; return authorization; } public static String getSignature(String data, String secret) { try { SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey); byte[] rawHmac = mac.doFinal(data.getBytes()); String result = Base64.getEncoder().encodeToString(rawHmac); result = result.replaceAll("\r|\n", ""); return result; } catch (Exception var6) { throw new RuntimeException("Failed to generate HMAC : " + var6.getMessage()); } } } }