|
|
@ -7,7 +7,10 @@ import com.epmet.auth.InternalAuthProcessor; |
|
|
|
import com.epmet.commons.tools.constant.AppClientConstant; |
|
|
|
import com.epmet.commons.tools.constant.Constant; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.constant.AuthTypeConstant; |
|
|
|
import com.epmet.constant.TokenHeaderKeyConstant; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -40,16 +43,6 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
|
|
|
|
|
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
|
|
|
|
|
|
private final AntPathMatcher antPathMatcher = new AntPathMatcher(); |
|
|
|
|
|
|
|
public static final String AUTH_TYPE_INTERNAL = "internal"; |
|
|
|
public static final String AUTH_TYPE_EXTERNAL = "external"; |
|
|
|
public static final String AUTH_TYPE_NO_NEED = "no_need"; |
|
|
|
public static final String AUTH_TYPE_UNKNOW = "unknow"; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CpProperty cpProperty; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private InternalAuthProcessor internalAuthProcessor; |
|
|
|
|
|
|
@ -79,16 +72,17 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
|
|
|
|
|
String authType = getAuthType(request); |
|
|
|
|
|
|
|
switch (authType) { |
|
|
|
case AUTH_TYPE_EXTERNAL: |
|
|
|
return externalAuthProcessor.auth(exchange, chain); |
|
|
|
case AUTH_TYPE_INTERNAL: |
|
|
|
return internalAuthProcessor.auth(exchange, chain); |
|
|
|
case AUTH_TYPE_NO_NEED: |
|
|
|
break; |
|
|
|
default: |
|
|
|
return response(exchange, new Result<>().error(EpmetErrorCode.ERR401.getCode(), |
|
|
|
EpmetErrorCode.ERR401.getMsg())); |
|
|
|
try { |
|
|
|
switch (authType) { |
|
|
|
case AuthTypeConstant.AUTH_TYPE_EXTERNAL: |
|
|
|
return externalAuthProcessor.auth(exchange, chain); |
|
|
|
case AuthTypeConstant.AUTH_TYPE_INTERNAL: |
|
|
|
return internalAuthProcessor.auth(exchange, chain); |
|
|
|
} |
|
|
|
} catch (RenException e) { |
|
|
|
return response(exchange, new Result<>().error(e.getCode(), e.getMessage())); |
|
|
|
} catch (Exception e) { |
|
|
|
return response(exchange, new Result<>().error(e.getMessage())); |
|
|
|
} |
|
|
|
|
|
|
|
return chain.filter(exchange); |
|
|
@ -100,40 +94,23 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private String getAuthType(ServerHttpRequest request) { |
|
|
|
String requestUri = request.getPath().pathWithinApplication().value(); |
|
|
|
|
|
|
|
boolean existsInInternal = false; |
|
|
|
boolean existsInExternal = false; |
|
|
|
|
|
|
|
for (String url : cpProperty.getInternalAuthUrls()) { |
|
|
|
if (antPathMatcher.match(url, requestUri)) { |
|
|
|
existsInInternal = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (String url : cpProperty.getExternalAuthUrls()) { |
|
|
|
if (antPathMatcher.match(url, requestUri)) { |
|
|
|
existsInExternal = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!existsInInternal && !existsInExternal) { |
|
|
|
// 既不再内部认证url,也不在外部认证url,那么不需要认证
|
|
|
|
return AUTH_TYPE_NO_NEED; |
|
|
|
} |
|
|
|
|
|
|
|
// 内部认证
|
|
|
|
if (StringUtils.isNotBlank(getHeader(request, Constant.AUTHORIZATION_HEADER)) |
|
|
|
&& existsInInternal) { |
|
|
|
return AUTH_TYPE_INTERNAL; |
|
|
|
} |
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(getHeader(request, Constant.ACCESS_TOKEN_HEADER)) |
|
|
|
&& existsInExternal) { |
|
|
|
return AUTH_TYPE_EXTERNAL; |
|
|
|
//String requestUri = request.getPath().pathWithinApplication().value();
|
|
|
|
|
|
|
|
// 是否在外部认证列表中(外部认证列表中的url,是对外部应用开放的,只有在这个列表中的url才对外部应用开放)
|
|
|
|
//boolean inExtAuthPaths = false;
|
|
|
|
//
|
|
|
|
//for (String url : cpProperty.getExternalAuthUrls()) {
|
|
|
|
// if (antPathMatcher.match(url, requestUri)) {
|
|
|
|
// inExtAuthPaths = true;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(request.getHeaders().getFirst(TokenHeaderKeyConstant.ACCESS_TOKEN_HEADER_KEY))) { |
|
|
|
// url对外部应用开放,并且头里面有AccessToken,那么走外部应用认证
|
|
|
|
return AuthTypeConstant.AUTH_TYPE_EXTERNAL; |
|
|
|
} |
|
|
|
|
|
|
|
return AUTH_TYPE_UNKNOW; |
|
|
|
return AuthTypeConstant.AUTH_TYPE_INTERNAL; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|