Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bme-access-guangdong-upload
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李洪明
bme-access-guangdong-upload
Commits
1a585e59
Commit
1a585e59
authored
Dec 18, 2025
by
李洪明
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加登录鉴权接口
parent
e4592450
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
4 deletions
+54
-4
LoginResult.java
...main/java/com/bme/access/guangdong/model/LoginResult.java
+1
-0
LoginService.java
...n/java/com/bme/access/guangdong/service/LoginService.java
+53
-4
No files found.
src/main/java/com/bme/access/guangdong/model/LoginResult.java
View file @
1a585e59
...
...
@@ -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
;
...
...
src/main/java/com/bme/access/guangdong/service/LoginService.java
View file @
1a585e59
...
...
@@ -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"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment