|
@@ -2,6 +2,7 @@ package com.usky.common.security.interceptor;
|
|
|
|
|
|
import com.usky.common.core.constants.SecurityConstants;
|
|
|
import com.usky.common.core.context.SecurityContextHolder;
|
|
|
+import com.usky.common.core.util.IpUtils;
|
|
|
import com.usky.common.core.util.JwtUtils;
|
|
|
import com.usky.common.core.util.ServletUtils;
|
|
|
import com.usky.common.core.util.StringUtils;
|
|
@@ -26,6 +27,8 @@ import java.util.Objects;
|
|
|
@Slf4j
|
|
|
public class HeaderInterceptor implements AsyncHandlerInterceptor
|
|
|
{
|
|
|
+ public static final String REAL_IP_KEY = "X-Real-IP";
|
|
|
+
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
|
|
|
{
|
|
@@ -33,12 +36,15 @@ public class HeaderInterceptor implements AsyncHandlerInterceptor
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
- log.info("access {} form realIP[{}] ",request.getRequestURI(),request.getRemoteAddr());
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ String remoteHost = request.getRemoteHost();
|
|
|
+ String realIP = request.getHeader(REAL_IP_KEY);
|
|
|
+ log.info("access {} form realIP[{}] with proxyIP[{}] ",request.getRequestURI(),realIP==null?remoteHost:realIP,remoteHost);
|
|
|
SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
|
|
|
SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
|
|
|
SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
|
|
|
SecurityContextHolder.setTenantId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_TENANT_ID));
|
|
|
-
|
|
|
+ SecurityContextHolder.set("startTime",String.valueOf(startTime));
|
|
|
String token = SecurityUtils.getToken();
|
|
|
if (StringUtils.isNotEmpty(token))
|
|
|
{
|
|
@@ -52,7 +58,8 @@ public class HeaderInterceptor implements AsyncHandlerInterceptor
|
|
|
SecurityContextHolder.setUserName(userName);
|
|
|
SecurityContextHolder.setUserKey(userKey);
|
|
|
SecurityContextHolder.setTenantId(tenantId);
|
|
|
- log.info("userId:{},userName:{},tenantId:{}",userId,userName,tenantId);
|
|
|
+
|
|
|
+ log.info("access session user the userId {} and userName {} and tenantId {}",userId,userName,tenantId);
|
|
|
//TODO 2022-05-03 gez 每次请求都要从rdis获取用户信息,并刷新时间,这个代码拉低每次请求效率
|
|
|
// 如果不影响其它业务模块,考虑去掉这块逻辑
|
|
|
LoginUser loginUser = AuthUtil.getLoginUser(token);
|
|
@@ -66,9 +73,15 @@ public class HeaderInterceptor implements AsyncHandlerInterceptor
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
|
|
- throws Exception
|
|
|
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
|
|
|
{
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ String startTime = SecurityContextHolder.get("startTime");
|
|
|
+ if(StringUtils.isNotEmpty(startTime)) {
|
|
|
+ long startTimeOfLong = Long.valueOf(startTime);
|
|
|
+ long l = endTime - startTimeOfLong;
|
|
|
+ log.info("access {} finish cost [{}] ms", request.getRequestURI(), l);
|
|
|
+ }
|
|
|
SecurityContextHolder.remove();
|
|
|
}
|
|
|
}
|