提交 | 用户 | 时间
|
94c44e
|
1 |
package com.iailab.module.shasteel.config; |
D |
2 |
|
|
3 |
import com.iailab.module.shasteel.util.token.IailabClient; |
|
4 |
import feign.RequestInterceptor; |
|
5 |
import feign.RequestTemplate; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
import org.springframework.http.HttpHeaders; |
|
8 |
import org.springframework.web.context.request.RequestContextHolder; |
|
9 |
import org.springframework.web.context.request.ServletRequestAttributes; |
|
10 |
|
|
11 |
@Configuration |
|
12 |
public class FeignTokenInterceptor implements RequestInterceptor { |
|
13 |
|
|
14 |
@Override |
|
15 |
public void apply(RequestTemplate requestTemplate) { |
|
16 |
// 从当前请求上下文中获取Token |
|
17 |
String token = getTokenFromCurrentRequest(); |
|
18 |
|
|
19 |
if (token == null) { |
|
20 |
// 如果没有获取到Token,从system-server中获取token |
|
21 |
token = IailabClient.getToken(); |
259bea
|
22 |
if (token!= null) { |
D |
23 |
requestTemplate.header(HttpHeaders.AUTHORIZATION, token); |
|
24 |
} |
94c44e
|
25 |
} |
D |
26 |
|
|
27 |
Long tenantId = IailabClient.getTenantId(); |
|
28 |
if (tenantId != null) { |
|
29 |
requestTemplate.header("tenant-id", String.valueOf(tenantId)); |
|
30 |
} |
|
31 |
} |
|
32 |
|
|
33 |
private String getTokenFromCurrentRequest() { |
|
34 |
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
35 |
if (attributes!= null) { |
|
36 |
return attributes.getRequest().getHeader(HttpHeaders.AUTHORIZATION); |
|
37 |
} |
|
38 |
return null; |
|
39 |
} |
|
40 |
} |