|
|
@@ -3,10 +3,14 @@ package cn.reghao.tnb.search.app.config.web;
|
|
|
import cn.reghao.jutil.web.ServletUtil;
|
|
|
import cn.reghao.tnb.common.auth.LoginUser;
|
|
|
import cn.reghao.tnb.common.auth.UserContext;
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.servlet.*;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* HTTP 请求过滤器
|
|
|
@@ -23,16 +27,32 @@ public class TokenFilter implements Filter {
|
|
|
@Override
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
|
|
throws IOException, ServletException {
|
|
|
+ String requestId = ServletUtil.getHeader("x-request-id");
|
|
|
+ MDC.put("request_id", requestId);
|
|
|
+
|
|
|
long userId = -1L;
|
|
|
String userIdStr = ServletUtil.getHeader("x-user-id");
|
|
|
if (userIdStr != null) {
|
|
|
userId = Long.parseLong(userIdStr);
|
|
|
}
|
|
|
|
|
|
- String loginId = ServletUtil.getHeader("x-login-id");
|
|
|
- LoginUser loginUser = new LoginUser(userId);
|
|
|
+ String loginId = "-1";
|
|
|
+ String loginIdStr = ServletUtil.getHeader("x-login-id");
|
|
|
+ if (loginIdStr != null) {
|
|
|
+ loginId = loginIdStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<String> roles = new HashSet<>();
|
|
|
+ String rolesStr = ServletUtil.getHeader("x-user-roles");
|
|
|
+ if (rolesStr != null) {
|
|
|
+ roles.addAll(Arrays.asList(rolesStr.split(",")));
|
|
|
+ }
|
|
|
+
|
|
|
+ LoginUser loginUser = new LoginUser(userId, loginId, roles);
|
|
|
try (UserContext context = new UserContext(loginUser)) {
|
|
|
chain.doFilter(request, response);
|
|
|
+ } finally {
|
|
|
+ MDC.clear();
|
|
|
}
|
|
|
}
|
|
|
|