Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
customer-service
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
SuperHive
back-end
customer-service
Commits
e4a77357
Commit
e4a77357
authored
Jun 11, 2020
by
huangcb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Feign/数据权限,功能优化
parent
28da5c33
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
198 additions
and
35 deletions
+198
-35
FeignRequestInterceptor.java
.../customer/common/interceptor/FeignRequestInterceptor.java
+82
-0
ReqUtils.java
...n/java/com/esv/freight/customer/common/util/ReqUtils.java
+5
-3
ThreadLocalCacheUtils.java
...v/freight/customer/common/util/ThreadLocalCacheUtils.java
+69
-0
FeignConfigure.java
.../java/com/esv/freight/customer/config/FeignConfigure.java
+0
-32
TestController.java
...eight/customer/module/test/controller/TestController.java
+14
-0
ThreadLocalCacheUtilsTest.java
...eight/customer/common/util/ThreadLocalCacheUtilsTest.java
+28
-0
No files found.
src/main/java/com/esv/freight/customer/common/interceptor/FeignRequestInterceptor.java
0 → 100644
View file @
e4a77357
package
com
.
esv
.
freight
.
customer
.
common
.
interceptor
;
import
com.alibaba.fastjson.JSONObject
;
import
com.esv.freight.customer.common.util.ReqUtils
;
import
com.esv.gateway.common.GatewayHeaders
;
import
feign.Request
;
import
feign.RequestInterceptor
;
import
feign.RequestTemplate
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.MDC
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.lang.reflect.Field
;
import
java.util.Collection
;
import
java.util.Map
;
/**
* @description: Feign请求拦截器
* @project: freight-customer-service
* @name: com.esv.freight.customer.common.interceptor.FeignRequestInterceptor
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/06/11 9:17
* @version:1.0
*/
@Slf4j
@Component
public
class
FeignRequestInterceptor
implements
RequestInterceptor
{
@Value
(
"${spring.application.name}"
)
private
String
applicationName
;
@Override
public
void
apply
(
RequestTemplate
requestTemplate
)
{
requestTemplate
.
header
(
"trace_id"
,
MDC
.
get
(
"traceId"
));
requestTemplate
.
header
(
"application_name"
,
applicationName
);
Class
cls
=
GatewayHeaders
.
class
;
Field
[]
fields
=
cls
.
getDeclaredFields
();
for
(
int
i
=
0
;
i
<
fields
.
length
;
i
++)
{
Field
f
=
fields
[
i
];
f
.
setAccessible
(
true
);
String
key
;
try
{
key
=
String
.
valueOf
(
f
.
get
(
cls
));
requestTemplate
.
header
(
key
,
ReqUtils
.
getRequestHeader
(
key
));
}
catch
(
IllegalAccessException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
}
// 判断header是否有租户信息
boolean
isTenant
=
false
;
Map
<
String
,
Collection
<
String
>>
headers
=
requestTemplate
.
headers
();
for
(
Map
.
Entry
<
String
,
Collection
<
String
>>
entry
:
headers
.
entrySet
())
{
if
(
GatewayHeaders
.
TENANT_ID
.
equals
(
entry
.
getKey
()))
{
isTenant
=
true
;
break
;
}
}
// 设置租户信息及数据权限(0-全部;1-租户;2-部门)
if
(!
isTenant
)
{
byte
[]
bytes
=
requestTemplate
.
requestBody
().
asBytes
();
if
(
null
==
bytes
)
{
requestTemplate
.
header
(
GatewayHeaders
.
DATA_PERM
,
"0"
);
}
else
{
String
bodyStr
=
new
String
(
bytes
);
JSONObject
bodyJson
=
JSONObject
.
parseObject
(
bodyStr
);
if
(!
bodyJson
.
isEmpty
()
&&
bodyJson
.
containsKey
(
GatewayHeaders
.
TENANT_ID
))
{
requestTemplate
.
header
(
GatewayHeaders
.
TENANT_ID
,
bodyJson
.
getString
(
GatewayHeaders
.
TENANT_ID
));
requestTemplate
.
header
(
GatewayHeaders
.
DATA_PERM
,
"1"
);
bodyJson
.
remove
(
GatewayHeaders
.
TENANT_ID
);
bodyStr
=
bodyJson
.
toJSONString
();
requestTemplate
.
body
(
Request
.
Body
.
encoded
(
bodyStr
.
getBytes
(
requestTemplate
.
requestCharset
()),
requestTemplate
.
requestCharset
()));
}
else
{
requestTemplate
.
header
(
GatewayHeaders
.
DATA_PERM
,
"0"
);
}
}
}
}
}
src/main/java/com/esv/freight/customer/common/util/ReqUtils.java
View file @
e4a77357
...
@@ -30,11 +30,13 @@ public class ReqUtils {
...
@@ -30,11 +30,13 @@ public class ReqUtils {
public
static
String
getRequestHeader
(
String
headerKey
)
{
public
static
String
getRequestHeader
(
String
headerKey
)
{
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
if
(
null
==
servletRequestAttributes
)
{
if
(
null
==
servletRequestAttributes
)
{
return
null
;
Object
value
=
ThreadLocalCacheUtils
.
getByKey
(
headerKey
);
}
return
null
==
value
?
null
:
value
.
toString
();
}
else
{
HttpServletRequest
request
=
servletRequestAttributes
.
getRequest
();
HttpServletRequest
request
=
servletRequestAttributes
.
getRequest
();
return
request
.
getHeader
(
headerKey
);
return
request
.
getHeader
(
headerKey
);
}
}
}
/**
/**
* description 获取当前请求的用户ID
* description 获取当前请求的用户ID
...
...
src/main/java/com/esv/freight/customer/common/util/ThreadLocalCacheUtils.java
0 → 100644
View file @
e4a77357
package
com
.
esv
.
freight
.
customer
.
common
.
util
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @description: ThreadLocal Cache工具类
* @project: freight-customer-service
* @name: com.esv.freight.customer.common.util.ThreadLocalCacheUtils
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/06/11 15:27
* @version:1.0
*/
public
class
ThreadLocalCacheUtils
{
private
static
ThreadLocal
<
Map
<
String
,
Object
>>
threadLocal
=
new
ThreadLocal
<>();
/**
* 向ThreadLocal缓存值
**/
public
static
void
setByKey
(
String
key
,
Object
value
)
{
Map
<
String
,
Object
>
map
=
threadLocal
.
get
();
if
(
null
==
map
)
{
map
=
new
HashMap
<>(
8
);
}
map
.
put
(
key
,
value
);
threadLocal
.
set
(
map
);
}
/**
* 从ThreadLocal里获取缓存Key的值
**/
public
static
Object
getByKey
(
String
key
)
{
Map
<
String
,
Object
>
map
=
threadLocal
.
get
();
if
(
null
==
map
)
{
return
null
;
}
else
{
return
map
.
get
(
key
);
}
}
/**
* 从ThreadLocal里移除缓存Key
**/
public
static
void
removeByKey
(
String
key
)
{
Map
<
String
,
Object
>
map
=
threadLocal
.
get
();
if
(
null
!=
map
)
{
map
.
remove
(
key
);
threadLocal
.
set
(
map
);
}
}
/**
* 从ThreadLocal里获取缓存
**/
public
static
Object
get
()
{
return
threadLocal
.
get
();
}
/**
* 移除当前线程缓存
* 用于释放当前线程ThreadLocal资源
*/
public
static
void
remove
()
{
threadLocal
.
remove
();
}
}
src/main/java/com/esv/freight/customer/config/FeignConfigure.java
View file @
e4a77357
package
com
.
esv
.
freight
.
customer
.
config
;
package
com
.
esv
.
freight
.
customer
.
config
;
import
com.esv.freight.customer.common.util.ReqUtils
;
import
com.esv.gateway.common.GatewayHeaders
;
import
feign.RequestInterceptor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.slf4j.MDC
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.client.RestTemplate
;
import
java.lang.reflect.Field
;
/**
/**
* @description: Feign接口配置
* @description: Feign接口配置
* @project: freight-customer-service
* @project: freight-customer-service
...
@@ -27,34 +20,9 @@ import java.lang.reflect.Field;
...
@@ -27,34 +20,9 @@ import java.lang.reflect.Field;
@Slf4j
@Slf4j
public
class
FeignConfigure
{
public
class
FeignConfigure
{
@Value
(
"${spring.application.name}"
)
private
String
applicationName
;
@Bean
@Bean
public
RestTemplate
restTemplate
()
{
public
RestTemplate
restTemplate
()
{
return
new
RestTemplate
();
return
new
RestTemplate
();
}
}
@Bean
public
RequestInterceptor
requestInterceptor
()
{
RequestInterceptor
requestInterceptor
=
((
requestTemplate
->
{
requestTemplate
.
header
(
"trace_id"
,
MDC
.
get
(
"traceId"
));
requestTemplate
.
header
(
"application_name"
,
applicationName
);
Class
cls
=
GatewayHeaders
.
class
;
Field
[]
fields
=
cls
.
getDeclaredFields
();
for
(
int
i
=
0
;
i
<
fields
.
length
;
i
++)
{
Field
f
=
fields
[
i
];
f
.
setAccessible
(
true
);
String
key
;
try
{
key
=
String
.
valueOf
(
f
.
get
(
cls
));
requestTemplate
.
header
(
key
,
ReqUtils
.
getRequestHeader
(
key
));
}
catch
(
IllegalAccessException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
}
}));
return
requestInterceptor
;
}
}
}
src/main/java/com/esv/freight/customer/module/test/controller/TestController.java
View file @
e4a77357
package
com
.
esv
.
freight
.
customer
.
module
.
test
.
controller
;
package
com
.
esv
.
freight
.
customer
.
module
.
test
.
controller
;
import
com.esv.freight.customer.common.response.EResponse
;
import
com.esv.freight.customer.common.response.EResponse
;
import
com.esv.freight.customer.common.util.ThreadLocalCacheUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
@@ -25,4 +26,17 @@ public class TestController {
...
@@ -25,4 +26,17 @@ public class TestController {
return
EResponse
.
ok
();
return
EResponse
.
ok
();
}
}
@GetMapping
(
"/test1"
)
public
EResponse
test1
()
{
ThreadLocalCacheUtils
.
setByKey
(
"thread"
,
"test1"
);
log
.
info
(
ThreadLocalCacheUtils
.
getByKey
(
"thread"
).
toString
());
return
EResponse
.
ok
(
ThreadLocalCacheUtils
.
getByKey
(
"thread"
).
toString
());
}
@GetMapping
(
"/test2"
)
public
EResponse
test2
()
{
ThreadLocalCacheUtils
.
setByKey
(
"thread"
,
"test2"
);
log
.
info
(
ThreadLocalCacheUtils
.
getByKey
(
"thread"
).
toString
());
return
EResponse
.
ok
(
ThreadLocalCacheUtils
.
getByKey
(
"thread"
).
toString
());
}
}
}
src/test/java/com/esv/freight/customer/common/util/ThreadLocalCacheUtilsTest.java
0 → 100644
View file @
e4a77357
package
com
.
esv
.
freight
.
customer
.
common
.
util
;
import
lombok.extern.slf4j.Slf4j
;
import
org.junit.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
/**
* @description:
* @project: freight-customer-service
* @name: com.esv.freight.customer.common.util.ThreadLocalCacheUtilsTest
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/06/11 15:57
* @version:1.0
*/
@SpringBootTest
@Slf4j
public
class
ThreadLocalCacheUtilsTest
{
@Test
public
void
test
()
{
ThreadLocalCacheUtils
.
setByKey
(
"time"
,
System
.
currentTimeMillis
());
log
.
info
(
"{}"
,
ThreadLocalCacheUtils
.
getByKey
(
"time"
).
toString
());
log
.
info
(
"{}"
,
ThreadLocalCacheUtils
.
get
().
toString
());
ThreadLocalCacheUtils
.
remove
();
}
}
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