Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b67bf3a2
authored
Oct 14, 2026
by
renyizhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改bug 保存长期令牌
parent
0ca86060
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
143 additions
and
30 deletions
+143
-30
computility-framework/computility-common/src/main/java/com/luhu/computility/framework/common/pojo/PageParam.java
+2
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/AiTokenService.java
+55
-7
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/NewApiClient.java
+83
-20
computility-server/src/main/resources/application-local.yaml
+2
-2
pom.xml
+1
-1
No files found.
computility-framework/computility-common/src/main/java/com/luhu/computility/framework/common/pojo/PageParam.java
View file @
b67bf3a2
...
@@ -2,6 +2,7 @@ package com.luhu.computility.framework.common.pojo;
...
@@ -2,6 +2,7 @@ package com.luhu.computility.framework.common.pojo;
import
io.swagger.v3.oas.annotations.media.Schema
;
import
io.swagger.v3.oas.annotations.media.Schema
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.Accessors
;
import
lombok.experimental.Accessors
;
import
javax.validation.constraints.Min
;
import
javax.validation.constraints.Min
;
...
@@ -11,6 +12,7 @@ import java.io.Serializable;
...
@@ -11,6 +12,7 @@ import java.io.Serializable;
@Schema
(
description
=
"分页参数"
)
@Schema
(
description
=
"分页参数"
)
@Data
@Data
@NoArgsConstructor
@Accessors
(
chain
=
true
)
@Accessors
(
chain
=
true
)
public
class
PageParam
implements
Serializable
{
public
class
PageParam
implements
Serializable
{
...
...
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/AiTokenService.java
View file @
b67bf3a2
...
@@ -83,8 +83,10 @@ public class AiTokenService {
...
@@ -83,8 +83,10 @@ public class AiTokenService {
log
.
info
(
"[AiTokenService.createTokenStep2] 准备登录: username={}"
,
username
);
log
.
info
(
"[AiTokenService.createTokenStep2] 准备登录: username={}"
,
username
);
try
{
try
{
// ① 登录拿短期 JWT
NewApiResponse
<
NewApiLoginRespVO
>
loginResp
=
newApiClient
.
login
(
username
,
password
);
NewApiResponse
<
NewApiLoginRespVO
>
loginResp
=
newApiClient
.
login
(
username
,
password
);
log
.
info
(
"[AiTokenService.createTokenStep2] 登录结果: success={}, message={}"
,
loginResp
.
getSuccess
(),
loginResp
.
getMessage
());
log
.
info
(
"[AiTokenService.createTokenStep2] 登录结果: success={}, message={}"
,
loginResp
.
getSuccess
(),
loginResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
loginResp
.
getSuccess
())
||
loginResp
.
getData
()
==
null
)
{
if
(!
Boolean
.
TRUE
.
equals
(
loginResp
.
getSuccess
())
||
loginResp
.
getData
()
==
null
)
{
return
AiTokenResult
.
builder
()
return
AiTokenResult
.
builder
()
...
@@ -95,14 +97,53 @@ public class AiTokenService {
...
@@ -95,14 +97,53 @@ public class AiTokenService {
.
build
();
.
build
();
}
}
String
shortJwt
=
loginResp
.
getData
().
getAccessToken
();
Long
newApiUserId
=
loginResp
.
getData
().
getUserId
();
Long
newApiUserId
=
loginResp
.
getData
().
getUserId
();
String
accessToken
=
loginResp
.
getData
().
getAccessToken
();
log
.
info
(
"[AiTokenService.createTokenStep2] 登录成功, newApiUserId={}, shortJwt={}***"
,
log
.
info
(
"[AiTokenService.createTokenStep2] 登录成功, newApiUserId={}, accessToken={}***"
,
newApiUserId
,
safePrefix
(
shortJwt
));
newApiUserId
,
accessToken
.
substring
(
0
,
Math
.
min
(
5
,
accessToken
.
length
())));
// NewAPI v3 登录响应已直接返回 access_token,无需再调 /api/user/token;
// ② 密码方式安全验证,拿一次性 proof_token
// 直接以该 token 作为 Bearer 凭据进入 step4 创建 API Key。
NewApiResponse
<
String
>
verifyResp
=
newApiClient
.
verifyPassword
(
return
createTokenStep4
(
newApiUserId
.
intValue
(),
accessToken
,
userId
,
username
,
password
);
shortJwt
,
newApiUserId
.
intValue
(),
password
,
"access_token.generate"
);
log
.
info
(
"[AiTokenService.createTokenStep2] 安全验证结果: success={}, message={}"
,
verifyResp
.
getSuccess
(),
verifyResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
verifyResp
.
getSuccess
())
||
verifyResp
.
getData
()
==
null
||
verifyResp
.
getData
().
isEmpty
())
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"NewAPI安全验证失败: "
+
verifyResp
.
getMessage
())
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
newApiUserId
(
newApiUserId
)
.
build
();
}
String
proof
=
verifyResp
.
getData
();
// ③ 拿长期 AccessToken(29~33 位)
NewApiResponse
<
String
>
tokenResp
=
newApiClient
.
generateAccessToken
(
shortJwt
,
newApiUserId
.
intValue
(),
proof
);
log
.
info
(
"[AiTokenService.createTokenStep2] 长期AccessToken结果: success={}, message={}"
,
tokenResp
.
getSuccess
(),
tokenResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
tokenResp
.
getSuccess
())
||
tokenResp
.
getData
()
==
null
||
tokenResp
.
getData
().
isEmpty
())
{
// 降级:用短期 JWT 直接走 step4,newapi_access_token 此时会写入短期 JWT
// (依赖日志告警 + 下次成功 login 自然刷新)
log
.
warn
(
"[AiTokenService.createTokenStep2] 获取长期AccessToken失败,降级用短期JWT: message={}"
,
tokenResp
.
getMessage
());
return
createTokenStep4
(
newApiUserId
.
intValue
(),
shortJwt
,
userId
,
username
,
password
);
}
String
longTermToken
=
tokenResp
.
getData
();
log
.
info
(
"[AiTokenService.createTokenStep2] 长期AccessToken获取成功: {}***"
,
safePrefix
(
longTermToken
));
// ④ 用长期 token 走 step4(accessToken 形参 = 长期 token,最终自然写入 result.newApiAccessToken)
return
createTokenStep4
(
newApiUserId
.
intValue
(),
longTermToken
,
userId
,
username
,
password
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"[AiTokenService.createTokenStep2] 登录异常"
,
e
);
log
.
error
(
"[AiTokenService.createTokenStep2] 登录异常"
,
e
);
return
AiTokenResult
.
builder
()
return
AiTokenResult
.
builder
()
...
@@ -114,6 +155,13 @@ public class AiTokenService {
...
@@ -114,6 +155,13 @@ public class AiTokenService {
}
}
}
}
private
static
String
safePrefix
(
String
s
)
{
if
(
s
==
null
||
s
.
isEmpty
())
{
return
"null"
;
}
return
s
.
substring
(
0
,
Math
.
min
(
5
,
s
.
length
()));
}
private
AiTokenResult
createTokenStep4
(
Integer
newApiUserId
,
String
accessToken
,
Long
userId
,
private
AiTokenResult
createTokenStep4
(
Integer
newApiUserId
,
String
accessToken
,
Long
userId
,
String
username
,
String
password
)
{
String
username
,
String
password
)
{
String
tokenName
=
"puhui_"
+
userId
+
"_token"
;
String
tokenName
=
"puhui_"
+
userId
+
"_token"
;
...
...
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/NewApiClient.java
View file @
b67bf3a2
...
@@ -210,54 +210,117 @@ public class NewApiClient {
...
@@ -210,54 +210,117 @@ public class NewApiClient {
}
}
}
}
public
String
getSessionCookie
(
HttpResponse
loginResponse
)
{
/**
String
session
=
loginResponse
.
getCookieValue
(
"session"
);
* 密码方式安全验证:POST /api/verify
log
.
info
(
"[NewApiClient.getSessionCookie] session={}"
,
session
!=
null
?
session
:
"null"
);
*
return
session
;
* <p>用于 NewAPI v3 的敏感操作(如 access_token.generate)需要先验证当前密码,
}
* 拿到一次性 proof_token 后再调后续接口。
* <p>接口:{@code POST /api/verify}
* <p>Header:{@code New-Api-User=<id>}, {@code Authorization: Bearer <accessToken>}
* <p>Body:{@code {"method":"password","scope":"<scope>","password":"<pwd>"}}
* <p>响应 data 字段包含 {@code proof_token},需透传给后续接口的 {@code X-Security-Proof} Header。
*
* @param accessToken 短期 JWT(来自 /api/user/login 响应)
* @param userId NewAPI 用户 ID
* @param password 用户明文密码(puhui 这边自动注册的 12 位随机密码)
* @param scope 验证范围,如 {@code "access_token.generate"}
*/
public
NewApiResponse
<
String
>
verifyPassword
(
String
accessToken
,
Integer
userId
,
String
password
,
String
scope
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/verify"
;
log
.
info
(
"[NewApiClient.verifyPassword] 请求URL: {}, scope={}"
,
url
,
scope
);
public
NewApiResponse
<
String
>
generateAccessToken
(
String
session
,
Integer
userId
)
{
Map
<
String
,
Object
>
body
=
new
HashMap
<>();
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/token"
;
body
.
put
(
"method"
,
"password"
)
;
log
.
info
(
"[NewApiClient.generateAccessToken] 请求URL: {}"
,
url
);
body
.
put
(
"scope"
,
scope
);
log
.
info
(
"[NewApiClient.generateAccessToken] session={}"
,
session
!=
null
?
session
:
"null"
);
body
.
put
(
"password"
,
password
);
try
{
try
{
HttpResponse
response
=
HttpRequest
.
ge
t
(
url
)
HttpResponse
response
=
HttpRequest
.
pos
t
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
"Cookie"
,
"session="
+
session
)
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
body
(
JSONUtil
.
toJsonStr
(
body
))
.
contentType
(
"application/json"
)
.
timeout
(
30000
)
.
timeout
(
30000
)
.
execute
();
.
execute
();
log
.
info
(
"[NewApiClient.generateAccessToken] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.verifyPassword] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.generateAccessToken] 响应内容: {}"
,
response
.
body
());
return
parseVerifyResponse
(
response
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.verifyPassword] 请求异常"
,
e
);
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
(
e
.
getMessage
()
==
null
?
e
.
getClass
().
getSimpleName
()
:
e
.
getMessage
()))
.
build
();
}
}
return
parseResponse
(
response
,
String
.
class
);
/**
* 解析 /api/verify 响应,仅提取 data.proof_token。
* 不抛异常,统一封装 NewApiResponse。
*/
private
NewApiResponse
<
String
>
parseVerifyResponse
(
HttpResponse
response
)
{
try
{
String
body
=
response
.
body
();
if
(
StrUtil
.
isBlank
(
body
))
{
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
message
(
"Empty response"
)
.
build
();
}
JSONObject
json
=
JSONUtil
.
parseObj
(
body
);
NewApiResponse
<
String
>
result
=
new
NewApiResponse
<>();
result
.
setSuccess
(
json
.
getBool
(
"success"
));
result
.
setMessage
(
json
.
getStr
(
"message"
));
if
(
Boolean
.
TRUE
.
equals
(
json
.
getBool
(
"success"
)))
{
JSONObject
data
=
json
.
getJSONObject
(
"data"
);
if
(
data
!=
null
)
{
String
proof
=
data
.
getStr
(
"proof_token"
);
if
(
proof
!=
null
&&
!
proof
.
isEmpty
())
{
result
.
setData
(
proof
);
}
}
}
return
result
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.
generateAccessToken] 请求
异常"
,
e
);
log
.
error
(
"[NewApiClient.
parseVerifyResponse] 解析响应
异常"
,
e
);
return
NewApiResponse
.<
String
>
builder
()
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
success
(
false
)
.
message
(
"
请求异常: "
+
e
.
getMessage
(
))
.
message
(
"
解析响应失败: "
+
(
e
.
getMessage
()
==
null
?
e
.
getClass
().
getSimpleName
()
:
e
.
getMessage
()
))
.
build
();
.
build
();
}
}
}
}
public
NewApiResponse
<
String
>
generateAccessTokenByToken
(
String
accessToken
,
Integer
userId
)
{
/**
* 生成长期 AccessToken:GET /api/user/token
*
* <p>NewAPI v3 中此接口需要先完成安全验证({@link #verifyPassword}),
* 通过 {@code X-Security-Proof} Header 提交 proof_token。
* <p>接口:{@code GET /api/user/token}
* <p>Header:{@code New-Api-User=<id>}, {@code Authorization: Bearer <accessToken>}, {@code X-Security-Proof=<proof>}
* <p>响应 data 字段是 29~33 位的随机字符串,会被 NewAPI 写入 {@code users.access_token} 列。
* <p>该 token 是长期有效的,可替代短期 JWT 作为后续管理类接口的 Bearer 凭据。
*/
public
NewApiResponse
<
String
>
generateAccessToken
(
String
accessToken
,
Integer
userId
,
String
proofToken
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/token"
;
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/token"
;
log
.
info
(
"[NewApiClient.generateAccessToken
ByToken
] 请求URL: {}"
,
url
);
log
.
info
(
"[NewApiClient.generateAccessToken] 请求URL: {}"
,
url
);
try
{
try
{
HttpResponse
response
=
HttpRequest
.
get
(
url
)
HttpResponse
response
=
HttpRequest
.
get
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
header
(
"X-Security-Proof"
,
proofToken
)
.
timeout
(
30000
)
.
timeout
(
30000
)
.
execute
();
.
execute
();
log
.
info
(
"[NewApiClient.generateAccessToken] 响应状态: {}"
,
response
.
getStatus
());
return
parseResponse
(
response
,
String
.
class
);
return
parseResponse
(
response
,
String
.
class
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.generateAccessToken
ByToken
] 请求异常"
,
e
);
log
.
error
(
"[NewApiClient.generateAccessToken] 请求异常"
,
e
);
return
NewApiResponse
.<
String
>
builder
()
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
(
))
.
message
(
"请求异常: "
+
(
e
.
getMessage
()
==
null
?
e
.
getClass
().
getSimpleName
()
:
e
.
getMessage
()
))
.
build
();
.
build
();
}
}
}
}
...
...
computility-server/src/main/resources/application-local.yaml
View file @
b67bf3a2
...
@@ -212,8 +212,8 @@ wx:
...
@@ -212,8 +212,8 @@ wx:
# 芋道配置项,设置当前项目所有自定义的配置
# 芋道配置项,设置当前项目所有自定义的配置
computility
:
computility
:
new-api
:
new-api
:
base-url
:
http://
172.25.164.0
:3000
base-url
:
http://
localhost
:3000
admin-token
:
nfcJyVtYb6xtREHA2MyL4f4o5TDg/qU
=
admin-token
:
f454ND2IhDKJqSxST4XBCLyR+wDOVEo
=
admin-user-id
:
1
admin-user-id
:
1
captcha
:
captcha
:
enable
:
false
# 本地环境,暂时关闭图片验证码,方便登录等接口的测试;
enable
:
false
# 本地环境,暂时关闭图片验证码,方便登录等接口的测试;
...
...
pom.xml
View file @
b67bf3a2
...
@@ -46,7 +46,7 @@
...
@@ -46,7 +46,7 @@
<maven-compiler-plugin.version>
3.14.0
</maven-compiler-plugin.version>
<maven-compiler-plugin.version>
3.14.0
</maven-compiler-plugin.version>
<flatten-maven-plugin.version>
1.6.0
</flatten-maven-plugin.version>
<flatten-maven-plugin.version>
1.6.0
</flatten-maven-plugin.version>
<!-- maven-surefire-plugin 暂时无法通过 bom 的依赖读取(兼容老版本 IDEA 2024 及以前版本) -->
<!-- maven-surefire-plugin 暂时无法通过 bom 的依赖读取(兼容老版本 IDEA 2024 及以前版本) -->
<lombok.version>
1.18.3
8
</lombok.version>
<lombok.version>
1.18.3
0
</lombok.version>
<spring.boot.version>
2.7.18
</spring.boot.version>
<spring.boot.version>
2.7.18
</spring.boot.version>
<mapstruct.version>
1.6.3
</mapstruct.version>
<mapstruct.version>
1.6.3
</mapstruct.version>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
...
...
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