|
|
|
@ -2,7 +2,6 @@ package com.shxy.xymanager_framework.filter;
|
|
|
|
|
|
|
|
|
|
import com.shxy.xymanager_common.bean.SysUser;
|
|
|
|
|
import com.shxy.xymanager_common.config.SecurityConfig;
|
|
|
|
|
import com.shxy.xymanager_common.exception.CustomException;
|
|
|
|
|
import com.shxy.xymanager_common.threadlocal.UserContextHolder;
|
|
|
|
|
import com.shxy.xymanager_common.util.TokenUtil;
|
|
|
|
|
import com.shxy.xymanager_service.service.SysUserService;
|
|
|
|
@ -12,12 +11,15 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.AntPathMatcher;
|
|
|
|
|
import org.springframework.util.PathMatcher;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
@Order(1)
|
|
|
|
|
@Slf4j
|
|
|
|
@ -34,32 +36,46 @@ public class UserInfoFilter extends AbstractAuthorizationFilter {
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PathMatcher pathMatcher = new AntPathMatcher();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
|
|
|
|
boolean valid = false;
|
|
|
|
|
try {
|
|
|
|
|
valid = initContextHolders(request);
|
|
|
|
|
} catch (CustomException ex) {
|
|
|
|
|
writeException(request, response, ex);
|
|
|
|
|
if (!config.getEnable()) {
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String path = request.getRequestURI();
|
|
|
|
|
int suffixIdx = path.lastIndexOf(".");
|
|
|
|
|
if (suffixIdx > -1 && Arrays.stream(config.getResourceSuffix().toArray())
|
|
|
|
|
.anyMatch(a -> a.equals(path.substring(suffixIdx)))) {
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (valid) {
|
|
|
|
|
for (String white : config.getUrlWhiteList()) {
|
|
|
|
|
if (pathMatcher.match(white, path)) {
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
initContextHolders(request);
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
} catch (JwtException ex) {
|
|
|
|
|
log.error("token error", ex);
|
|
|
|
|
writeException(request, response, new Exception("用户未登录或用户登录已过期"));
|
|
|
|
|
} finally {
|
|
|
|
|
UserContextHolder.reset();
|
|
|
|
|
}
|
|
|
|
|
UserContextHolder.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean initContextHolders(HttpServletRequest request) throws CustomException {
|
|
|
|
|
private boolean initContextHolders(HttpServletRequest request) {
|
|
|
|
|
String token = request.getHeader(config.getJwtHeader());
|
|
|
|
|
if (!StringUtils.isBlank(token)) {
|
|
|
|
|
try {
|
|
|
|
|
String userId = tokenUtil.getUserId(token);
|
|
|
|
|
if (!StringUtils.isBlank(userId)) {
|
|
|
|
|
SysUser user = userService.selectUserById(Integer.parseInt(userId));
|
|
|
|
|
UserContextHolder.setCurrentUserInfo(user);
|
|
|
|
|
}
|
|
|
|
|
} catch (JwtException ex) {
|
|
|
|
|
log.error(ex.getMessage(), ex);
|
|
|
|
|
throw new CustomException("用户未登录或用户登录已过期");
|
|
|
|
|
if (!StringUtils.isBlank(userId)) {
|
|
|
|
|
SysUser user = userService.selectUserById(Integer.parseInt(userId));
|
|
|
|
|
UserContextHolder.setCurrentUserInfo(user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|