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

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

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