Commit d6a8aa68 authored by 杨锐's avatar 杨锐

refactor URL

parent 9cacff85
......@@ -6,18 +6,19 @@ import org.springframework.beans.factory.annotation.Value;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Slf4j
public abstract class RequestInterceptor {
// @Data
// static class AuthInfoInMemory {
// private static boolean auth;
// private static long expiration;
// }
@Data
static class AuthInfoInMemory {
private static List<String> hosts = new ArrayList<>();
private static long expiration;
}
protected static final String AUTH = "quant-auth";
// 内网
......@@ -25,12 +26,26 @@ public abstract class RequestInterceptor {
List<String> authHttpList;
protected boolean auth(String host) {
// if (authCache()) return true;
if (authHttpList == null || authHttpList.isEmpty()) {
return false;
}
boolean auth = authHttpList.stream()
.map(http -> {
if(AuthInfoInMemory.hosts.isEmpty() || System.currentTimeMillis() > AuthInfoInMemory.expiration){
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;
try {
url = new URL(http);
......@@ -38,19 +53,5 @@ public abstract class RequestInterceptor {
log.error("无效quant.auth.http = 【{}】", http);
}
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