Commit bfc6acef authored by 杨锐's avatar 杨锐

code review resolve discussion.

parent 77eec696
package cn.quantgroup.tech.brave.configuration;
import cn.quantgroup.tech.brave.interceptor.*;
import cn.quantgroup.tech.brave.interceptor.impl.HttpClientAuthRequestInterceptor;
import cn.quantgroup.tech.brave.interceptor.impl.HttpClientNoAuthRequestInterceptor;
import cn.quantgroup.tech.brave.interceptor.impl.RestTemplateAuthRequestInterceptor;
import cn.quantgroup.tech.brave.interceptor.impl.RestTemplateNoAuthRequestInterceptor;
import cn.quantgroup.tech.brave.job.TokenJob;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AuthAutoConfiguration {
@Configuration
public static class NoAuthConfiguration {
@Bean
@ConditionalOnMissingBean(HttpClientRequestInterceptor.class)
public HttpClientRequestInterceptor httpClientRequestInterceptor() {
return new HttpClientNoAuthRequestInterceptor();
}
@Bean
@ConditionalOnMissingBean(RestTemplateRequestInterceptor.class)
public RestTemplateRequestInterceptor restTemplateRequestInterceptor() {
return new RestTemplateNoAuthRequestInterceptor();
}
}
@Configuration
@ConditionalOnProperty(prefix = "quant.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public static class AuthConfiguration {
@Bean
public HttpClientRequestInterceptor httpClientRequestInterceptor() {
return new HttpClientAuthRequestInterceptor();
}
@Bean
public RestTemplateRequestInterceptor restTemplateRequestInterceptor() {
return new RestTemplateAuthRequestInterceptor();
}
@Bean
public TokenJob tokenJob() {
return new TokenJob();
}
}
}
package cn.quantgroup.tech.brave.handler;
public class TokenHandler {
private static String token;
public static void setToken(String token) {
TokenHandler.token = token;
}
public static String getToken() {
return token;
}
}
package cn.quantgroup.tech.brave.interceptor;
import cn.quantgroup.tech.brave.interceptor.job.TokenJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.protocol.HttpContext;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class HttpClientRequestInterceptor extends RequestInterceptor implements HttpRequestInterceptor {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) {
if (auth(((HttpRequestWrapper) httpRequest).getTarget().getHostName())) {
log.info("cn.quantgroup.tech.brave.interceptor.HttpClientRequestInterceptor.process token = 【{}】 ", TokenJob.token);
httpRequest.addHeader(AUTH, TokenJob.token);
}
}
public interface HttpClientRequestInterceptor extends HttpRequestInterceptor {
}
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.Date;
import java.util.List;
import java.util.Optional;
@Slf4j
public abstract class RequestInterceptor {
// @Data
// static class AuthInfoInMemory {
// private static boolean auth;
// private static long expiration;
// }
static final String AUTH = "quant-auth";
protected static final String AUTH = "quant-auth";
// 内网
@Value("#{'${quant.auth.http}'.split(',')}")
List<String> authHttpList;
boolean auth(String host) {
protected boolean auth(String host) {
// if (authCache()) return true;
if (authHttpList == null || authHttpList.isEmpty()) {
return false;
}
return authHttpList.stream()
boolean auth = authHttpList.stream()
.map(http -> {
URL url = null;
try {
......@@ -32,5 +41,16 @@ public abstract class RequestInterceptor {
})
.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;
// }
}
package cn.quantgroup.tech.brave.interceptor;
import cn.quantgroup.tech.brave.interceptor.job.TokenJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Slf4j
@Component
public class RestTemplateRequestInterceptor extends RequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
if (auth(request.getURI().getHost())) {
log.info("cn.quantgroup.tech.brave.interceptor.RestTemplateRequestInterceptor.intercept token = 【{}】 ", TokenJob.token);
request.getHeaders().add(AUTH, TokenJob.token);
}
return execution.execute(request, body);
}
public interface RestTemplateRequestInterceptor extends ClientHttpRequestInterceptor {
}
package cn.quantgroup.tech.brave.interceptor.impl;
import cn.quantgroup.tech.brave.handler.TokenHandler;
import cn.quantgroup.tech.brave.interceptor.HttpClientRequestInterceptor;
import cn.quantgroup.tech.brave.interceptor.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.protocol.HttpContext;
@Slf4j
public class HttpClientAuthRequestInterceptor extends RequestInterceptor implements HttpClientRequestInterceptor {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) {
if (auth(((HttpRequestWrapper) httpRequest).getTarget().getHostName())) {
log.info("cn.quantgroup.tech.brave.interceptor.HttpClientRequestInterceptor.process token = 【{}】 ", TokenHandler.getToken());
httpRequest.addHeader(AUTH, TokenHandler.getToken());
}
}
}
package cn.quantgroup.tech.brave.interceptor.impl;
import cn.quantgroup.tech.brave.interceptor.HttpClientRequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
@Slf4j
public class HttpClientNoAuthRequestInterceptor implements HttpClientRequestInterceptor {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) {
// nothing to do
}
}
package cn.quantgroup.tech.brave.interceptor.impl;
import cn.quantgroup.tech.brave.handler.TokenHandler;
import cn.quantgroup.tech.brave.interceptor.RequestInterceptor;
import cn.quantgroup.tech.brave.interceptor.RestTemplateRequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
@Slf4j
public class RestTemplateAuthRequestInterceptor extends RequestInterceptor implements RestTemplateRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
if (auth(request.getURI().getHost())) {
log.info("cn.quantgroup.tech.brave.interceptor.RestTemplateRequestInterceptor.intercept token = 【{}】 ", TokenHandler.getToken());
request.getHeaders().add(AUTH, TokenHandler.getToken());
}
return execution.execute(request, body);
}
}
package cn.quantgroup.tech.brave.interceptor.impl;
import cn.quantgroup.tech.brave.interceptor.RestTemplateRequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import java.io.IOException;
@Slf4j
public class RestTemplateNoAuthRequestInterceptor implements RestTemplateRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
return execution.execute(request, body);
}
}
package cn.quantgroup.tech.brave.interceptor.job;
package cn.quantgroup.tech.brave.job;
import cn.quantgroup.tech.brave.handler.TokenHandler;
import cn.quantgroup.tech.brave.service.ITechRestTemplateBuilder;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.annotation.EnableScheduling;
......@@ -29,9 +28,7 @@ class TokenResp {
}
@Slf4j
@Configuration
@EnableScheduling
@ConditionalOnProperty(prefix = "quant.auth", name = {"http", "clientId", "secret", "tokenUrl"})
public class TokenJob {
private static final String CREDENTIAL = "credential";
......@@ -60,8 +57,9 @@ public class TokenJob {
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestHeaders);
TokenResp tokenResp = restTemplate().postForObject(tokenUrl, httpEntity, TokenResp.class);
log.info("getToken tokenResp = 【{}】", tokenResp);
if (tokenResp != null && SUCCESS == tokenResp.getCode() && StringUtils.isNotBlank(tokenResp.getToken())) {
token = tokenResp.getToken();
boolean tokenValid = tokenResp != null && SUCCESS == tokenResp.getCode() && StringUtils.isNotBlank(tokenResp.getToken());
if (tokenValid) {
TokenHandler.setToken(tokenResp.getToken());
}
}
......
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.quantgroup.tech.brave.configuration.BraveAutoConfiguration
\ No newline at end of file
cn.quantgroup.tech.brave.configuration.BraveAutoConfiguration,\
cn.quantgroup.tech.brave.configuration.AuthAutoConfiguration
\ No newline at end of file
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