|
@@ -10,6 +10,7 @@ import com.ywt.gateway.configuration.BizCfg;
|
|
import com.ywt.gateway.decorator.RecorderServerHttpRequestDecorator;
|
|
import com.ywt.gateway.decorator.RecorderServerHttpRequestDecorator;
|
|
import com.ywt.gateway.model.*;
|
|
import com.ywt.gateway.model.*;
|
|
import com.ywt.gateway.utils.BizUtil;
|
|
import com.ywt.gateway.utils.BizUtil;
|
|
|
|
+import jodd.net.MimeTypes;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -43,6 +44,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
|
*/
|
|
*/
|
|
@Component
|
|
@Component
|
|
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostLocationInfo> {
|
|
public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostLocationInfo> {
|
|
|
|
+ public static final String SLASH = "/";
|
|
|
|
+ public static final String DOT = ".";
|
|
private final Logger logger = LoggerFactory.getLogger(AuthGatewayFilterFactory.class);
|
|
private final Logger logger = LoggerFactory.getLogger(AuthGatewayFilterFactory.class);
|
|
public static final String AUTH_TYPE_WEB = "web";
|
|
public static final String AUTH_TYPE_WEB = "web";
|
|
public static final String AUTH_TYPE_WECHATMP = "wechatmp";
|
|
public static final String AUTH_TYPE_WECHATMP = "wechatmp";
|
|
@@ -82,7 +85,7 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
|
|
".ico",
|
|
".ico",
|
|
};
|
|
};
|
|
if (requestUrl != null && !requestUrl.isEmpty()) {
|
|
if (requestUrl != null && !requestUrl.isEmpty()) {
|
|
- if (requestUrl.endsWith("/")) {
|
|
|
|
|
|
+ if (requestUrl.endsWith(SLASH)) {
|
|
requestUrl = requestUrl.substring(0, requestUrl.length() - 1);
|
|
requestUrl = requestUrl.substring(0, requestUrl.length() - 1);
|
|
}
|
|
}
|
|
for (String suffix : staticFilesSuffix) {
|
|
for (String suffix : staticFilesSuffix) {
|
|
@@ -92,6 +95,19 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private String tryGettingExtFromUrl(String requestUrl) {
|
|
|
|
+ if (requestUrl != null && !requestUrl.isEmpty()) {
|
|
|
|
+ if (requestUrl.endsWith(SLASH)) {
|
|
|
|
+ requestUrl = requestUrl.substring(0, requestUrl.length() - 1);
|
|
|
|
+ }
|
|
|
|
+ int lastDotIdx = requestUrl.lastIndexOf(DOT);
|
|
|
|
+ if (lastDotIdx > 0 && !requestUrl.endsWith(DOT)) {
|
|
|
|
+ return requestUrl.substring(lastDotIdx + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public GatewayFilter apply(HostLocationInfo config) {
|
|
public GatewayFilter apply(HostLocationInfo config) {
|
|
return (exchange, chain) -> {
|
|
return (exchange, chain) -> {
|
|
@@ -124,10 +140,14 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
|
|
try {
|
|
try {
|
|
String auth = config.getAuth();
|
|
String auth = config.getAuth();
|
|
String protocol = config.getProtocol();
|
|
String protocol = config.getProtocol();
|
|
- if (auth == null || auth.isEmpty() || isRequest4StaticFiles(requestUrl)) {
|
|
|
|
|
|
+ boolean isStaticFiles = isRequest4StaticFiles(requestUrl);
|
|
|
|
+ if (auth == null || auth.isEmpty() || isStaticFiles) {
|
|
// 不需要授权
|
|
// 不需要授权
|
|
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
|
|
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
|
|
proxyPass(config, null, exchange.getResponse().getHeaders(), exchange.getResponse());
|
|
proxyPass(config, null, exchange.getResponse().getHeaders(), exchange.getResponse());
|
|
|
|
+ if (isStaticFiles) {
|
|
|
|
+ exchange.getResponse().getHeaders().set(HttpHeaders.CONTENT_TYPE, MimeTypes.getMimeType(tryGettingExtFromUrl(requestUrl)));
|
|
|
|
+ }
|
|
}));
|
|
}));
|
|
} else {
|
|
} else {
|
|
AuthInfo authInfo = bizCfg.getAuths().stream().filter(i -> auth.equals(i.getName())).findFirst().orElse(null);
|
|
AuthInfo authInfo = bizCfg.getAuths().stream().filter(i -> auth.equals(i.getName())).findFirst().orElse(null);
|
|
@@ -300,7 +320,7 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
|
|
if (!cookieName.isEmpty()) tokenName = cookieName;
|
|
if (!cookieName.isEmpty()) tokenName = cookieName;
|
|
ResponseCookie cookie = ResponseCookie.from(tokenName, token)
|
|
ResponseCookie cookie = ResponseCookie.from(tokenName, token)
|
|
.httpOnly(false)
|
|
.httpOnly(false)
|
|
- .path("/")
|
|
|
|
|
|
+ .path(SLASH)
|
|
// .maxAge(authInfo.getMaxAge())
|
|
// .maxAge(authInfo.getMaxAge())
|
|
.domain(authInfo.getCookieDomain())
|
|
.domain(authInfo.getCookieDomain())
|
|
.build();
|
|
.build();
|
|
@@ -312,7 +332,7 @@ public class AuthGatewayFilterFactory extends AbstractGatewayFilterFactory<HostL
|
|
if (!cookieName.isEmpty()) tokenName = cookieName;
|
|
if (!cookieName.isEmpty()) tokenName = cookieName;
|
|
ResponseCookie cookie = ResponseCookie.from(tokenName, "")
|
|
ResponseCookie cookie = ResponseCookie.from(tokenName, "")
|
|
.httpOnly(false)
|
|
.httpOnly(false)
|
|
- .path("/")
|
|
|
|
|
|
+ .path(SLASH)
|
|
.maxAge(-1)
|
|
.maxAge(-1)
|
|
.domain(authInfo.getCookieDomain())
|
|
.domain(authInfo.getCookieDomain())
|
|
.build();
|
|
.build();
|