fix: 解决html页面缓存的问题。
This commit is contained in:
parent
fff50fb038
commit
e7fe6e5d4e
|
@ -1,25 +1,33 @@
|
||||||
package io.metersphere.gateway.filter;
|
package io.metersphere.gateway.filter;
|
||||||
|
|
||||||
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class HtmlResourceFilter implements GatewayFilter, Ordered {
|
public class HtmlResourceFilter implements GatewayFilter, Ordered {
|
||||||
|
|
||||||
|
private static final String HTTP_SCHEME = "http";
|
||||||
private static final HashMap<String, String> NoCacheHeaders = new HashMap<>();
|
private static final HashMap<String, String> NoCacheHeaders = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
NoCacheHeaders.put(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
|
NoCacheHeaders.put(HttpHeaders.CACHE_CONTROL, "no-cache");
|
||||||
|
NoCacheHeaders.put("Cache", "no-cache");
|
||||||
NoCacheHeaders.put(HttpHeaders.PRAGMA, "no-cache");
|
NoCacheHeaders.put(HttpHeaders.PRAGMA, "no-cache");
|
||||||
NoCacheHeaders.put(HttpHeaders.EXPIRES, "0");
|
NoCacheHeaders.put(HttpHeaders.EXPIRES, "0");
|
||||||
}
|
}
|
||||||
|
@ -28,9 +36,22 @@ public class HtmlResourceFilter implements GatewayFilter, Ordered {
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
return chain.filter(exchange).then(
|
return chain.filter(exchange).then(
|
||||||
Mono.fromRunnable(() -> {
|
Mono.fromRunnable(() -> {
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
URI requestUrl = exchange.getRequiredAttribute(GATEWAY_REQUEST_URL_ATTR);
|
||||||
if (exchange.getRequest().getMethod() == HttpMethod.GET && response.getHeaders().getContentType() == MediaType.TEXT_HTML) {
|
if (!StringUtils.startsWithIgnoreCase(requestUrl.getScheme(), HTTP_SCHEME)) {
|
||||||
response.getHeaders().setAll(NoCacheHeaders);
|
// maybe ws or wss instead of http?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
if (response == null) {
|
||||||
|
// maybe backend service is unavailable or other scenario?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (exchange.getRequest().getMethod() == HttpMethod.GET && response.getHeaders().getContentType() == MediaType.TEXT_HTML && response.getStatusCode() == HttpStatus.OK) {
|
||||||
|
response.getHeaders().setAll(NoCacheHeaders);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtil.error("Fail to handle url " + requestUrl.getPath(), e);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue