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,32 +26,32 @@ public abstract class RequestInterceptor { ...@@ -25,32 +26,32 @@ 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();
URL url = null; }
try { return AuthInfoInMemory.hosts.contains(host);
url = new URL(http); }
} catch (MalformedURLException e) {
log.error("无效quant.auth.http = 【{}】", http); private void hostsCache() {
} AuthInfoInMemory.hosts = authHttpList.stream()
return Optional.ofNullable(url); .map(RequestInterceptor::apply)
})
.filter(Optional::isPresent) .filter(Optional::isPresent)
.anyMatch(url -> url.get().getHost().equals(host)); .map(Optional::get)
// buildAuthCache(auth); .map(URL::getHost)
return auth; .collect(Collectors.toList());
AuthInfoInMemory.expiration = System.currentTimeMillis() + 30 * 60 * 60 * 1000;
} }
// private boolean authCache() { private static Optional<URL> apply(String http) {
// return AuthInfoInMemory.auth && System.currentTimeMillis() < AuthInfoInMemory.expiration; URL url = null;
// } try {
// url = new URL(http);
// private void buildAuthCache(boolean auth) { } catch (MalformedURLException e) {
// AuthInfoInMemory.auth = auth; log.error("无效quant.auth.http = 【{}】", http);
// AuthInfoInMemory.expiration = System.currentTimeMillis() + 10 * 60 * 60; }
// } return Optional.ofNullable(url);
}
} }
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