Commit 2d68647d authored by 杨锐's avatar 杨锐

新增全局开关,auth.http代码优化。

parent 5793523c
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -34,25 +34,29 @@ public class AuthAutoConfiguration {
}
@Configuration
@ConditionalOnProperty(prefix = "quant.auth", name = {"http", "clientId", "secret", "tokenUrl"})
@ConditionalOnProperty(prefix = "tech.auth", name = "enabled", havingValue = "true")
public static class AuthConfiguration {
@Bean
@ConditionalOnProperty(prefix = "tech.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public HttpClientRequestInterceptor httpClientRequestInterceptor() {
return new HttpClientAuthRequestInterceptor();
}
@Bean
@ConditionalOnProperty(prefix = "tech.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public OkHttpClientInterceptor okHttpClientInterceptor() {
return new OkHttpClientAuthInterceptor();
}
@Bean
@ConditionalOnProperty(prefix = "tech.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public RestTemplateRequestInterceptor restTemplateRequestInterceptor() {
return new RestTemplateAuthRequestInterceptor();
}
@Bean
@ConditionalOnProperty(prefix = "tech.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public TokenJob tokenJob() {
return new TokenJob();
}
......
package cn.quantgroup.tech.brave.interceptor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
......@@ -14,35 +12,26 @@ import java.util.stream.Collectors;
@Slf4j
public abstract class RequestInterceptor {
@Data
static class AuthInfoInMemory {
private static List<String> hosts = new ArrayList<>();
private static long expiration;
}
protected static final String AUTH = "quant-auth";
// 内网
@Value("#{'${quant.auth.http}'.split(',')}")
List<String> authHttpList;
protected boolean auth(String host) {
if (authHttpList == null || authHttpList.isEmpty()) {
return false;
}
if(AuthInfoInMemory.hosts.isEmpty() || System.currentTimeMillis() > AuthInfoInMemory.expiration){
hostsCache();
}
return AuthInfoInMemory.hosts.contains(host);
}
List<String> authHttpList;
private void hostsCache() {
AuthInfoInMemory.hosts = authHttpList.stream()
// 内网
@Value("#{'${tech.auth.http}'.split(',')}")
public void setAuthHttpList(List<String> authHttpList) {
this.authHttpList = 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;
}
protected boolean auth(String host) {
if (authHttpList == null || authHttpList.isEmpty()) {
return false;
}
return authHttpList.contains(host);
}
private static Optional<URL> apply(String http) {
......@@ -50,7 +39,7 @@ public abstract class RequestInterceptor {
try {
url = new URL(http);
} catch (MalformedURLException e) {
log.error("无效quant.auth.http = 【{}】", http);
log.error("无效tech.auth.http = 【{}】", http);
}
return Optional.ofNullable(url);
}
......
......@@ -36,11 +36,11 @@ public class TokenJob {
private static final String CREDENTIAL = "credential";
private static final int SUCCESS = 200;
@Value("${quant.auth.clientId}")
@Value("${tech.auth.clientId}")
private String clientId;
@Value("${quant.auth.secret}")
@Value("${tech.auth.secret}")
private String secret;
@Value("${quant.auth.tokenUrl}")
@Value("${tech.auth.tokenUrl}")
private String tokenUrl;
@Resource
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<groupId>cn.quantgroup</groupId>
<artifactId>enoch-agent-spring-boot-starter</artifactId>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
......@@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.quantgroup</groupId>
<artifactId>commons-parent</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
<parent>
<groupId>org.springframework.boot</groupId>
......@@ -35,7 +35,7 @@
<lombok.version>1.16.20</lombok.version>
<guava.version>23.0</guava.version>
<apollo.client.version>0.10.2</apollo.client.version>
<common.parent.version>0.3.0</common.parent.version>
<common.parent.version>0.3.1</common.parent.version>
</properties>
<dependencies>
......
......@@ -5,7 +5,7 @@
<parent>
<artifactId>commons-parent</artifactId>
<groupId>cn.quantgroup</groupId>
<version>0.3.0</version>
<version>0.3.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
......
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