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
c5e15fb3
authored
Mar 18, 2026
by
lijinqi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.appId为空的处理
2.HttpUtils的优化 3.
parent
0bdbd9b8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
121 additions
and
14 deletions
+121
-14
computility-framework/computility-common/src/main/java/com/luhu/computility/framework/common/util/http/HttpUtils.java
+117
-14
computility-framework/computility-spring-boot-starter-protection/src/main/java/com/luhu/computility/framework/signature/core/aop/ApiSignatureAspect.java
+4
-0
No files found.
computility-framework/computility-common/src/main/java/com/luhu/computility/framework/common/util/http/HttpUtils.java
View file @
c5e15fb3
...
...
@@ -149,11 +149,34 @@ public class HttpUtils {
* @return 请求结果
*/
public
static
String
post
(
String
url
,
Map
<
String
,
String
>
headers
,
String
requestBody
)
{
try
(
HttpResponse
response
=
HttpRequest
.
post
(
url
)
.
addHeaders
(
headers
)
Map
<
String
,
String
>
safeHeaders
=
headers
==
null
?
new
java
.
util
.
HashMap
<>()
:
headers
;
HttpResponse
response
=
null
;
try
{
response
=
HttpRequest
.
post
(
url
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
body
(
requestBody
)
.
execute
();
// Manually follow 30x if not auto-followed
if
(
isRedirect
(
response
))
{
String
location
=
response
.
header
(
"Location"
);
if
(
StrUtil
.
isNotBlank
(
location
))
{
String
newUrl
=
resolveRedirectUrl
(
url
,
location
);
try
(
HttpResponse
redirected
=
HttpRequest
.
post
(
newUrl
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
body
(
requestBody
)
.
execute
())
{
return
redirected
.
body
();
}
}
}
return
response
.
body
();
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
...
...
@@ -167,34 +190,76 @@ public class HttpUtils {
* @return 请求结果
*/
public
static
String
get
(
String
url
,
Map
<
String
,
String
>
headers
)
{
try
(
HttpResponse
response
=
HttpRequest
.
get
(
url
)
.
addHeaders
(
headers
)
Map
<
String
,
String
>
safeHeaders
=
headers
==
null
?
new
java
.
util
.
HashMap
<>()
:
headers
;
HttpResponse
response
=
null
;
try
{
response
=
HttpRequest
.
get
(
url
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
execute
();
if
(
isRedirect
(
response
))
{
String
location
=
response
.
header
(
"Location"
);
if
(
StrUtil
.
isNotBlank
(
location
))
{
String
newUrl
=
resolveRedirectUrl
(
url
,
location
);
try
(
HttpResponse
redirected
=
HttpRequest
.
get
(
newUrl
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
execute
())
{
return
redirected
.
body
();
}
}
}
return
response
.
body
();
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
public
static
String
postIncludeImage
(
String
url
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
Object
>
formMaps
)
{
try
(
HttpResponse
response
=
openApiPost
(
url
)
.
addHeaders
(
headers
)
Map
<
String
,
String
>
safeHeaders
=
headers
==
null
?
new
java
.
util
.
HashMap
<>()
:
headers
;
HttpResponse
response
=
null
;
try
{
response
=
openApiPost
(
url
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
form
(
formMaps
)
.
execute
();
if
(
isRedirect
(
response
))
{
String
location
=
response
.
header
(
"Location"
);
if
(
StrUtil
.
isNotBlank
(
location
))
{
String
newUrl
=
resolveRedirectUrl
(
url
,
location
);
try
(
HttpResponse
redirected
=
openApiPost
(
newUrl
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
form
(
formMaps
)
//.form(fileName, file) // 上传文件,"fileName"是接口中定义的字段名
.
execute
())
{
return
redirected
.
body
();
}
}
}
return
response
.
body
();
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
public
static
HttpRequest
openApiPost
(
String
url
)
{
return
HttpRequest
.
post
(
url
)
.
setReadTimeout
(
300000
);
.
setReadTimeout
(
300000
)
.
setFollowRedirects
(
true
);
}
public
static
String
get
(
String
url
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
String
>
queryParams
)
{
try
{
// 构建查询参数字符串
StringBuilder
queryBuilder
=
new
StringBuilder
();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
queryParams
.
entrySet
())
{
if
(
queryBuilder
.
length
()
>
0
)
{
...
...
@@ -205,17 +270,36 @@ public class HttpUtils {
.
append
(
entry
.
getValue
());
}
// 拼接完整的 URL
String
fullUrl
=
url
;
if
(
queryBuilder
.
length
()
>
0
)
{
fullUrl
+=
"?"
+
queryBuilder
.
toString
()
;
fullUrl
+=
"?"
+
queryBuilder
;
}
// 发送 GET 请求并返回响应体
try
(
HttpResponse
response
=
HttpRequest
.
get
(
fullUrl
)
.
addHeaders
(
headers
)
Map
<
String
,
String
>
safeHeaders
=
headers
==
null
?
new
java
.
util
.
HashMap
<>()
:
headers
;
HttpResponse
response
=
null
;
try
{
response
=
HttpRequest
.
get
(
fullUrl
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
execute
();
if
(
isRedirect
(
response
))
{
String
location
=
response
.
header
(
"Location"
);
if
(
StrUtil
.
isNotBlank
(
location
))
{
String
newUrl
=
resolveRedirectUrl
(
fullUrl
,
location
);
try
(
HttpResponse
redirected
=
HttpRequest
.
get
(
newUrl
)
.
addHeaders
(
safeHeaders
)
.
setFollowRedirects
(
true
)
.
execute
())
{
return
redirected
.
body
();
}
}
}
return
response
.
body
();
}
finally
{
if
(
response
!=
null
)
{
response
.
close
();
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
@@ -223,5 +307,24 @@ public class HttpUtils {
}
}
private
static
boolean
isRedirect
(
HttpResponse
response
)
{
if
(
response
==
null
)
return
false
;
int
status
=
response
.
getStatus
();
return
status
==
301
||
status
==
302
||
status
==
303
||
status
==
307
||
status
==
308
;
}
private
static
String
resolveRedirectUrl
(
String
baseUrl
,
String
location
)
{
if
(
StrUtil
.
startWithIgnoreCase
(
location
,
"http://"
)
||
StrUtil
.
startWithIgnoreCase
(
location
,
"https://"
))
{
return
location
;
}
// Relative redirect; resolve against baseUrl
try
{
java
.
net
.
URI
base
=
java
.
net
.
URI
.
create
(
baseUrl
);
return
base
.
resolve
(
location
).
toString
();
}
catch
(
Exception
ignore
)
{
return
location
;
}
}
}
computility-framework/computility-spring-boot-starter-protection/src/main/java/com/luhu/computility/framework/signature/core/aop/ApiSignatureAspect.java
View file @
c5e15fb3
...
...
@@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.springframework.util.StringUtils
;
import
javax.annotation.Resource
;
import
javax.servlet.http.HttpServletRequest
;
...
...
@@ -82,6 +83,9 @@ public class ApiSignatureAspect {
//查询appId对应的Secret
String
appId
=
request
.
getHeader
(
signature
.
appId
());
if
(
StringUtils
.
isEmpty
(
appId
))
{
throw
new
ServiceException
(
INVALID_APPID
);
}
AppCredentialRespDTO
appCredentialRespDTO
=
appCredentialApi
.
getAppSecretByAppid
(
appId
);
if
(!
ObjectUtil
.
isEmpty
(
appCredentialRespDTO
))
{
Assert
.
notNull
(
appCredentialRespDTO
.
getAppId
(),
"[appId({})] 找不到对应的 appSecret"
,
appId
);
...
...
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