Commit 1a585e59 authored by 李洪明's avatar 李洪明

添加登录鉴权接口

parent e4592450
......@@ -7,6 +7,7 @@ public class LoginResult {
private Integer code;
private LoginData data;
@Data
public class LoginData {
private String access_token;
private Integer expires_in;
......
......@@ -9,11 +9,10 @@ import org.json.JSONObject;
import org.springframework.stereotype.Service;
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;
......@@ -36,6 +35,15 @@ public class LoginService {
HttpResponse response = HttpUtils.sendPost(host, path, null, body);
String result = EntityUtils.toString(response.getEntity());
loginResult = JSON.parseObject(result, LoginResult.class);
if (loginResult.getData() != null) {
String aes_secret = loginResult.getData().getAes_secret();
String aesSecret = decryptWithPublicKey(aes_secret);
// 用RSA公钥进行解密
loginResult.getData().setAes_secret(aesSecret);
String accessToken = aesDecrypt(loginResult.getData().getAccess_token(), aesSecret);
loginResult.getData().setAccess_token(accessToken);
return loginResult;
}
} catch (Exception e) {
e.printStackTrace();
}
......@@ -115,4 +123,45 @@ public class LoginService {
return "";
}
private static String decryptWithPublicKey(String encryptedText) {
String publicKeyPEM = "-----BEGIN PUBLIC KEY-----\n" +
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4cgA3y8+0K7jDi5UrhqP\n" +
"Cxl3Tmp4dOphlcIvCpk1EQcGVNo3fYq4rWBMTlueAbk7mIimWi5/Dfc6DHEM5+Dg\n" +
"DU4f/95KaqXDb8smeYLLEWpiDO9O5tLbwtOkcNNvb36sGFBWoIoDQoXRfSmim75D\n" +
"BQb8jMrv0CM/hEqdmBHmrsF9YNuAnJ8qnWtTvdeM9qAXEF5gbGRIykfp8Nf0Yx3s\n" +
"nPX1HnGN5SCjDfUf50nQ9K0xgzJymm5dJdvF0vPX6IMvVFl/RI8fZPdf73IkAM2G\n" +
"QQ+83XyvvcvGeFDUZfP97A7QsaTvp5JrjEi9U2CXwY3pSXxb5j2scffAhsDVwKdU\n" +
"lQIDAQAB\n" +
"-----END PUBLIC KEY-----";
// 移除PEM头尾并解码Base64
String publicKeyContent = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s", ""); // 移除所有空白字符,包括换行符和空格
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyContent);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
// AES密钥解密
public static String aesDecrypt(String encryptedData, String key) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, "UTF-8");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment