Commit d6a8aa68 authored by 杨锐's avatar 杨锐

refactor URL

parent 9cacff85
...@@ -6,18 +6,19 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -6,18 +6,19 @@ import org.springframework.beans.factory.annotation.Value;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
@Slf4j @Slf4j
public abstract class RequestInterceptor { public abstract class RequestInterceptor {
// @Data @Data
// static class AuthInfoInMemory { static class AuthInfoInMemory {
// private static boolean auth; private static List<String> hosts = new ArrayList<>();
// private static long expiration; private static long expiration;
// } }
protected static final String AUTH = "quant-auth"; protected static final String AUTH = "quant-auth";
// 内网 // 内网
...@@ -25,12 +26,26 @@ public abstract class RequestInterceptor { ...@@ -25,12 +26,26 @@ public abstract class RequestInterceptor {
List<String> authHttpList; List<String> authHttpList;
protected boolean auth(String host) { protected boolean auth(String host) {
// if (authCache()) return true;
if (authHttpList == null || authHttpList.isEmpty()) { if (authHttpList == null || authHttpList.isEmpty()) {
return false; return false;
} }
boolean auth = authHttpList.stream() if(AuthInfoInMemory.hosts.isEmpty() || System.currentTimeMillis() > AuthInfoInMemory.expiration){
.map(http -> { hostsCache();
}
return AuthInfoInMemory.hosts.contains(host);
}
private void hostsCache() {
AuthInfoInMemory.hosts = authHttpList.stream()
.map(RequestInterceptor::apply)
.filter(Optional::isPresent)
.map(Optional::get)
.map(URL::getHost)
.collect(Collectors.toList());
AuthInfoInMemory.expiration = System.currentTimeMillis() + 30 * 60 * 60 * 1000;
}
private static Optional<URL> apply(String http) {
URL url = null; URL url = null;
try { try {
url = new URL(http); url = new URL(http);
...@@ -38,19 +53,5 @@ public abstract class RequestInterceptor { ...@@ -38,19 +53,5 @@ public abstract class RequestInterceptor {
log.error("无效quant.auth.http = 【{}】", http); log.error("无效quant.auth.http = 【{}】", http);
} }
return Optional.ofNullable(url); return Optional.ofNullable(url);
})
.filter(Optional::isPresent)
.anyMatch(url -> url.get().getHost().equals(host));
// buildAuthCache(auth);
return auth;
} }
// private boolean authCache() {
// return AuthInfoInMemory.auth && System.currentTimeMillis() < AuthInfoInMemory.expiration;
// }
//
// private void buildAuthCache(boolean auth) {
// AuthInfoInMemory.auth = auth;
// AuthInfoInMemory.expiration = System.currentTimeMillis() + 10 * 60 * 60;
// }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment