提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.security.core.rpc; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
4 |
import com.iailab.framework.security.core.LoginUser; |
|
5 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
6 |
import feign.RequestInterceptor; |
|
7 |
import feign.RequestTemplate; |
|
8 |
import lombok.SneakyThrows; |
|
9 |
import lombok.extern.slf4j.Slf4j; |
|
10 |
|
|
11 |
import java.net.URLEncoder; |
|
12 |
import java.nio.charset.StandardCharsets; |
|
13 |
|
|
14 |
/** |
|
15 |
* LoginUser 的 RequestInterceptor 实现类:Feign 请求时,将 {@link LoginUser} 设置到 header 中,继续透传给被调用的服务 |
|
16 |
* |
|
17 |
* @author iailab |
|
18 |
*/ |
|
19 |
@Slf4j |
|
20 |
public class LoginUserRequestInterceptor implements RequestInterceptor { |
|
21 |
|
|
22 |
@Override |
|
23 |
@SneakyThrows |
|
24 |
public void apply(RequestTemplate requestTemplate) { |
|
25 |
LoginUser user = SecurityFrameworkUtils.getLoginUser(); |
|
26 |
if (user == null) { |
|
27 |
return; |
|
28 |
} |
|
29 |
try { |
|
30 |
String userStr = JsonUtils.toJsonString(user); |
|
31 |
userStr = URLEncoder.encode(userStr, StandardCharsets.UTF_8.name()); // 编码,避免中文乱码 |
|
32 |
requestTemplate.header(SecurityFrameworkUtils.LOGIN_USER_HEADER, userStr); |
|
33 |
} catch (Exception ex) { |
|
34 |
log.error("[apply][序列化 LoginUser({}) 发生异常]", user, ex); |
|
35 |
throw ex; |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
} |