|
@@ -25,6 +25,8 @@ import me.zhengjie.base.ResultCode;
|
|
|
import me.zhengjie.security.config.bean.SecurityProperties;
|
|
import me.zhengjie.security.config.bean.SecurityProperties;
|
|
|
import me.zhengjie.security.service.OnlineUserService;
|
|
import me.zhengjie.security.service.OnlineUserService;
|
|
|
import me.zhengjie.security.service.dto.OnlineUserDto;
|
|
import me.zhengjie.security.service.dto.OnlineUserDto;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
@@ -51,7 +53,7 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
private final SecurityProperties properties;
|
|
private final SecurityProperties properties;
|
|
|
private final OnlineUserService onlineUserService;
|
|
private final OnlineUserService onlineUserService;
|
|
|
private static Set<String> anonymousUrl = new HashSet<>();
|
|
private static Set<String> anonymousUrl = new HashSet<>();
|
|
|
-
|
|
|
|
|
|
|
+ private static final String TRACE_ID = "traceId";
|
|
|
/**
|
|
/**
|
|
|
* @param tokenProvider Token
|
|
* @param tokenProvider Token
|
|
|
* @param properties JWT
|
|
* @param properties JWT
|
|
@@ -62,7 +64,10 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
this.onlineUserService = onlineUserService;
|
|
this.onlineUserService = onlineUserService;
|
|
|
this.tokenProvider = tokenProvider;
|
|
this.tokenProvider = tokenProvider;
|
|
|
}
|
|
}
|
|
|
|
|
+ public static String uuid() {
|
|
|
|
|
|
|
|
|
|
+ return UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
+ }
|
|
|
/**
|
|
/**
|
|
|
* 设置匿名访问URL
|
|
* 设置匿名访问URL
|
|
|
* @param map
|
|
* @param map
|
|
@@ -97,13 +102,11 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
|
|
|
|
|
// 判断是否是app请求
|
|
// 判断是否是app请求
|
|
|
String reqUrl = httpServletRequest.getRequestURI();
|
|
String reqUrl = httpServletRequest.getRequestURI();
|
|
|
- // 先判断是否为前端登录
|
|
|
|
|
-// if (reqUrl.contains("/app/login")) {
|
|
|
|
|
-// filterChain.doFilter(servletRequest, servletResponse);
|
|
|
|
|
-// return;
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+ String traceId = httpServletRequest.getHeader(TRACE_ID);
|
|
|
|
|
+ if (StringUtils.isEmpty(traceId)) {
|
|
|
|
|
+ traceId = uuid();
|
|
|
|
|
+ }
|
|
|
|
|
+ MDC.put(TRACE_ID, traceId);
|
|
|
// 这里是处理app的接口
|
|
// 这里是处理app的接口
|
|
|
String token = resolveToken(httpServletRequest);
|
|
String token = resolveToken(httpServletRequest);
|
|
|
if (reqUrl.startsWith("/api/bank") || reqUrl.startsWith("/app/") || reqUrl.startsWith("/minipro/")) {
|
|
if (reqUrl.startsWith("/api/bank") || reqUrl.startsWith("/app/") || reqUrl.startsWith("/minipro/")) {
|
|
@@ -135,7 +138,6 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
// 这里是token的问题
|
|
// 这里是token的问题
|
|
|
if (StrUtil.isNotBlank(token)) {
|
|
if (StrUtil.isNotBlank(token)) {
|
|
|
OnlineUserDto onlineUserDto = null;
|
|
OnlineUserDto onlineUserDto = null;
|
|
|
-// boolean cleanUserCache = false;
|
|
|
|
|
try {
|
|
try {
|
|
|
String onlineUser = onlineUserService.getOne(properties.getOnlineKey() + token);
|
|
String onlineUser = onlineUserService.getOne(properties.getOnlineKey() + token);
|
|
|
JSONObject jsonObject = JSON.parseObject(onlineUser);
|
|
JSONObject jsonObject = JSON.parseObject(onlineUser);
|
|
@@ -148,22 +150,10 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
onlineUserDto.setAuthorities(authorityList);
|
|
onlineUserDto.setAuthorities(authorityList);
|
|
|
} catch (ExpiredJwtException e) {
|
|
} catch (ExpiredJwtException e) {
|
|
|
log.error(e.getMessage());
|
|
log.error(e.getMessage());
|
|
|
-// cleanUserCache = true;
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
log.error(e.getMessage());
|
|
|
-// cleanUserCache = true;
|
|
|
|
|
}
|
|
}
|
|
|
- // 不理解已经是空了,为什么还要删除在线人员,
|
|
|
|
|
-// finally {
|
|
|
|
|
-// try {
|
|
|
|
|
-// if (cleanUserCache || Objects.isNull(onlineUserDto)) {
|
|
|
|
|
-// userCacheClean.cleanUserCache(
|
|
|
|
|
-// String.valueOf(tokenProvider.getClaims(token).get(TokenProvider.AUTHORITIES_KEY)));
|
|
|
|
|
-// }
|
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
|
-// log.error(e.getMessage());
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+
|
|
|
if (onlineUserDto != null && StringUtils.hasText(token)) {
|
|
if (onlineUserDto != null && StringUtils.hasText(token)) {
|
|
|
Authentication authentication = new UsernamePasswordAuthenticationToken(onlineUserDto, token, new ArrayList<>());
|
|
Authentication authentication = new UsernamePasswordAuthenticationToken(onlineUserDto, token, new ArrayList<>());
|
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
@@ -172,6 +162,7 @@ public class TokenFilter extends GenericFilterBean {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
filterChain.doFilter(servletRequest, servletResponse);
|
|
filterChain.doFilter(servletRequest, servletResponse);
|
|
|
|
|
+ MDC.put(TRACE_ID, traceId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|