diff --git a/pom.xml b/pom.xml
index 4c69a18..c06874f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -13,7 +13,7 @@
group.flyfish
rest-proxy
- 1.0.2
+ 1.0.3
8
@@ -22,7 +22,7 @@
1.8
4.4
2.6
- 1.0.2
+ 1.0.3
pom
diff --git a/rest-proxy-api/pom.xml b/rest-proxy-api/pom.xml
index fe0d95e..fee2e36 100644
--- a/rest-proxy-api/pom.xml
+++ b/rest-proxy-api/pom.xml
@@ -5,7 +5,7 @@
group.flyfish
rest-proxy
- 1.0.2
+ 1.0.3
4.0.0
diff --git a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestService.java b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestService.java
index 9544b69..83183e5 100644
--- a/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestService.java
+++ b/rest-proxy-api/src/main/java/group/flyfish/rest/annotation/RestService.java
@@ -32,4 +32,11 @@ public @interface RestService {
* @return 结果
*/
int timeout() default -1;
+
+ /**
+ * 鉴权提供者类
+ *
+ * @return 具体实现了RestAuthProvider的类
+ */
+ Class> authProvider() default Object.class;
}
diff --git a/rest-proxy-core/pom.xml b/rest-proxy-core/pom.xml
index d86ad1d..cc8430d 100644
--- a/rest-proxy-core/pom.xml
+++ b/rest-proxy-core/pom.xml
@@ -5,7 +5,7 @@
group.flyfish
rest-proxy
- 1.0.2
+ 1.0.3
4.0.0
diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/configuration/RestClientProperties.java b/rest-proxy-core/src/main/java/group/flyfish/rest/configuration/RestClientProperties.java
index e978a1c..919252d 100644
--- a/rest-proxy-core/src/main/java/group/flyfish/rest/configuration/RestClientProperties.java
+++ b/rest-proxy-core/src/main/java/group/flyfish/rest/configuration/RestClientProperties.java
@@ -1,10 +1,12 @@
package group.flyfish.rest.configuration;
+import group.flyfish.rest.core.auth.RestAuthProvider;
import group.flyfish.rest.core.factory.PropertiesConfigurator;
import group.flyfish.rest.utils.DataUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
import java.time.Duration;
import java.util.HashMap;
@@ -40,6 +42,11 @@ public class RestClientProperties implements InitializingBean {
*/
private Map urls = new HashMap<>();
+ /**
+ * 默认的认证提供者
+ */
+ private RestAuthProvider authProvider;
+
/**
* 获取字典url
*
@@ -53,6 +60,11 @@ public class RestClientProperties implements InitializingBean {
return urls.get(key);
}
+ @Autowired(required = false)
+ public void setDefaultAuthProvider(RestAuthProvider provider) {
+ this.authProvider = provider;
+ }
+
@Override
public void afterPropertiesSet() {
PropertiesConfigurator.shared().configure(this);
diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/core/auth/RestAuthProvider.java b/rest-proxy-core/src/main/java/group/flyfish/rest/core/auth/RestAuthProvider.java
new file mode 100644
index 0000000..73fb1a8
--- /dev/null
+++ b/rest-proxy-core/src/main/java/group/flyfish/rest/core/auth/RestAuthProvider.java
@@ -0,0 +1,18 @@
+package group.flyfish.rest.core.auth;
+
+import group.flyfish.rest.core.client.RestClientBuilder;
+
+/**
+ * rest认证提供者
+ *
+ * @author wangyu
+ */
+public interface RestAuthProvider {
+
+ /**
+ * 通过入侵client提供鉴权
+ *
+ * @param builder rest客户端构建器
+ */
+ void provide(RestClientBuilder builder);
+}
diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/registry/proxy/RestProxyInvoker.java b/rest-proxy-core/src/main/java/group/flyfish/rest/registry/proxy/RestProxyInvoker.java
index 4b41a66..3b63494 100644
--- a/rest-proxy-core/src/main/java/group/flyfish/rest/registry/proxy/RestProxyInvoker.java
+++ b/rest-proxy-core/src/main/java/group/flyfish/rest/registry/proxy/RestProxyInvoker.java
@@ -5,6 +5,7 @@ import group.flyfish.rest.annotation.AutoMapping;
import group.flyfish.rest.annotation.RestApi;
import group.flyfish.rest.annotation.RestService;
import group.flyfish.rest.configuration.RestClientProperties;
+import group.flyfish.rest.core.auth.RestAuthProvider;
import group.flyfish.rest.core.client.RestClient;
import group.flyfish.rest.core.client.RestClientBuilder;
import group.flyfish.rest.core.factory.PropertiesConfigurable;
@@ -17,6 +18,7 @@ import group.flyfish.rest.registry.proxy.support.UrlCompiler;
import group.flyfish.rest.registry.wrapper.DefaultRestResultMapping;
import group.flyfish.rest.utils.DataUtils;
import group.flyfish.rest.utils.JacksonUtil;
+import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.springframework.aop.support.AopUtils;
@@ -32,7 +34,6 @@ import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Optional;
-import java.util.function.Consumer;
import java.util.stream.Stream;
/**
@@ -57,6 +58,8 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
private final RestApiRegistry registry;
// 结果映射
private RestResultMapping mapping;
+ // 鉴权提供者
+ private RestAuthProvider authProvider;
/**
* 构造器
@@ -93,11 +96,9 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
@Override
public void configure(RestClientProperties properties) {
this.properties = properties;
- this.config = RequestConfig.custom()
- .setConnectTimeout((int) properties.getConnectionTimeout().toMillis())
- .setSocketTimeout(restService.timeout())
- .build();
+ this.config = RequestConfig.custom().setConnectTimeout((int) properties.getConnectionTimeout().toMillis()).setSocketTimeout(restService.timeout()).build();
this.baseUrl = this.findBaseUrl();
+ this.authProvider = determineAuthProvider();
}
/**
@@ -129,10 +130,7 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
// 第一步就解析参数
ArgumentResolveContext context = composite.resolve(restApi, method, args);
// 构造和调用,这里的restClient不保存状态
- RestClientBuilder builder = RestClient.create()
- .url(determineUrl(restApi, context))
- .method(restApi.method())
- .config(config);
+ RestClientBuilder builder = RestClient.create().url(determineUrl(restApi, context)).method(restApi.method()).config(config);
// 需要带cookie的带上
if (restApi.credentials()) {
builder.withCredential();
@@ -149,7 +147,11 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
if (context.hasHeaders()) {
builder.headers(context.getHeaders());
}
- // 赋值头部
+ // 添加鉴权信息
+ if (null != authProvider) {
+ authProvider.provide(builder);
+ }
+ // 构建客户端
RestClient client = builder.build();
// 设置客户端
client.setClient(registry.getProvider());
@@ -235,10 +237,7 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
url = restApi.url();
} else {
// 构建基础url,优先级从小到大依次找。同时尝试取字典值
- Optional baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl)
- .filter(DataUtils::isNotBlank)
- .findFirst()
- .map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found);
+ Optional baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl).filter(DataUtils::isNotBlank).findFirst().map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found);
// 判定和赋值
if (baseUrl.isPresent() && DataUtils.isNotBlank(restApi.uri())) {
url = baseUrl.get() + restApi.uri();
@@ -249,4 +248,18 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigu
// 尝试解析路径参数
return context.hasPathParams() ? UrlCompiler.compile(url, context.getPathParams()) : url;
}
+
+ /**
+ * 确定鉴权提供者
+ *
+ * @return 实例化的提供者
+ */
+ @SneakyThrows
+ private RestAuthProvider determineAuthProvider() {
+ Class> candidate = restService.authProvider();
+ if (ClassUtils.isAssignable(RestAuthProvider.class, candidate)) {
+ return (RestAuthProvider) candidate.newInstance();
+ }
+ return properties.getAuthProvider();
+ }
}