Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
order-group
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
order-group-application
order-group
Commits
3fac2cd5
Commit
3fac2cd5
authored
Aug 10, 2022
by
pass商户管理员
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
X-Real-IP feign传递
parent
8f286ebe
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
0 deletions
+70
-0
order-application-service/src/main/java/cn/freemud/config/FeignSupportConfig.java
+19
-0
order-application-service/src/main/java/cn/freemud/interceptor/FeignInterceptor.java
+51
-0
No files found.
order-application-service/src/main/java/cn/freemud/config/FeignSupportConfig.java
0 → 100644
View file @
3fac2cd5
package
cn
.
freemud
.
config
;
import
cn.freemud.interceptor.FeignInterceptor
;
import
feign.RequestInterceptor
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
FeignSupportConfig
{
/**
* feign请求拦截器
*
* @return
*/
@Bean
public
RequestInterceptor
requestInterceptor
(){
return
new
FeignInterceptor
();
}
}
order-application-service/src/main/java/cn/freemud/interceptor/FeignInterceptor.java
0 → 100644
View file @
3fac2cd5
package
cn
.
freemud
.
interceptor
;
import
cn.freemud.utils.AppLogUtil
;
import
com.alibaba.fastjson.JSON
;
import
feign.RequestInterceptor
;
import
feign.RequestTemplate
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Enumeration
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
@Configuration
public
class
FeignInterceptor
implements
RequestInterceptor
{
@Override
public
void
apply
(
RequestTemplate
requestTemplate
)
{
Map
<
String
,
String
>
headers
=
getHeaders
(
getHttpServletRequest
());
for
(
String
headerName
:
headers
.
keySet
()){
requestTemplate
.
header
(
headerName
,
getHeaders
(
getHttpServletRequest
()).
get
(
headerName
));
}
}
private
HttpServletRequest
getHttpServletRequest
()
{
try
{
return
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
}
catch
(
Exception
e
)
{
AppLogUtil
.
errorLog
(
"FeignInterceptor error {}"
,
""
,
""
,
e
);
return
null
;
}
}
private
Map
<
String
,
String
>
getHeaders
(
HttpServletRequest
request
)
{
Map
<
String
,
String
>
map
=
new
LinkedHashMap
<>();
Enumeration
<
String
>
enumeration
=
request
.
getHeaderNames
();
while
(
enumeration
.
hasMoreElements
())
{
String
key
=
enumeration
.
nextElement
();
if
(
needThisHeader
(
key
)){
String
value
=
request
.
getHeader
(
key
);
map
.
put
(
key
,
value
);
}
}
AppLogUtil
.
printLog
(
"clientIp for feign client info:"
+
JSON
.
toJSONString
(
map
),
""
,
""
);
return
map
;
}
private
boolean
needThisHeader
(
String
headerName
){
// TODO:过滤哪些header需要传递,千万不能把所有的header传下去
return
true
;
}
}
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