|
|
@@ -6,6 +6,7 @@ import com.usoftchina.saas.account.dto.AccountDTO;
|
|
|
import com.usoftchina.saas.account.dto.UrlResourceDTO;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtHelper;
|
|
|
import com.usoftchina.saas.auth.common.jwt.JwtInfo;
|
|
|
+import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
import com.usoftchina.saas.exception.BizException;
|
|
|
import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
import com.usoftchina.saas.gateway.error.PermissionException;
|
|
|
@@ -40,17 +41,26 @@ public class AuthFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
@Override
|
|
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
|
|
- if (!isIgnore(exchange.getRequest())) {
|
|
|
- // 鉴别身份信息
|
|
|
- JwtInfo jwt = getJwtInfoFromHeader(exchange.getRequest());
|
|
|
- AccountDTO accountDTO = AccountCache.of(jwt.getUserId()).getAccount();
|
|
|
- if (null == accountDTO) {
|
|
|
- throw new BizException(ExceptionCode.USER_NOT_EXIST);
|
|
|
+ try {
|
|
|
+ if (!isIgnore(exchange.getRequest())) {
|
|
|
+ // 鉴别身份信息
|
|
|
+ String token = getAuthHeaderToken(exchange.getRequest());
|
|
|
+ JwtInfo jwt = JwtHelper.getInfoFromToken(token, authConfig.getPublicKey());
|
|
|
+ BaseContextHolder.setAppId(jwt.getAppId());
|
|
|
+ BaseContextHolder.setUserId(jwt.getUserId());
|
|
|
+ BaseContextHolder.setCompanyId(jwt.getCompanyId());
|
|
|
+ BaseContextHolder.setToken(token);
|
|
|
+ AccountDTO accountDTO = AccountCache.of(jwt.getUserId()).getAccount();
|
|
|
+ if (null == accountDTO) {
|
|
|
+ throw new BizException(ExceptionCode.USER_NOT_EXIST);
|
|
|
+ }
|
|
|
+ // 鉴别角色权限
|
|
|
+ checkPermission(exchange.getRequest(), jwt, accountDTO);
|
|
|
}
|
|
|
- // 鉴别角色权限
|
|
|
- checkPermission(exchange.getRequest(), jwt, accountDTO);
|
|
|
+ return chain.filter(exchange);
|
|
|
+ } finally {
|
|
|
+ BaseContextHolder.remove();
|
|
|
}
|
|
|
- return chain.filter(exchange);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -97,7 +107,7 @@ public class AuthFilter implements GlobalFilter, Ordered {
|
|
|
return authConfig.getIgnores().stream().anyMatch(ignore -> ignore.equals(path));
|
|
|
}
|
|
|
|
|
|
- private JwtInfo getJwtInfoFromHeader(ServerHttpRequest request) {
|
|
|
+ private String getAuthHeaderToken(ServerHttpRequest request) {
|
|
|
if (!request.getHeaders().containsKey(authConfig.getAuthHeader())) {
|
|
|
throw new BizException(ExceptionCode.JWT_ILLEGAL_ARGUMENT);
|
|
|
}
|
|
|
@@ -105,8 +115,7 @@ public class AuthFilter implements GlobalFilter, Ordered {
|
|
|
if (headers.isEmpty()) {
|
|
|
throw new BizException(ExceptionCode.JWT_ILLEGAL_ARGUMENT);
|
|
|
}
|
|
|
- String token = headers.get(0).trim();
|
|
|
- return JwtHelper.getInfoFromToken(token, authConfig.getPublicKey());
|
|
|
+ return headers.get(0).trim();
|
|
|
}
|
|
|
|
|
|
@Override
|