feat: 1.0.3版本升级,增加鉴权管理

This commit is contained in:
wangyu 2023-01-29 10:25:55 +08:00
parent fc8a6293e8
commit 871cf00c0b
7 changed files with 68 additions and 18 deletions

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.0.2</sdk.version>
<sdk.version>1.0.3</sdk.version>
</properties>
<packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -32,4 +32,11 @@ public @interface RestService {
* @return 结果
*/
int timeout() default -1;
/**
* 鉴权提供者类
*
* @return 具体实现了RestAuthProvider的类
*/
Class<?> authProvider() default Object.class;
}

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -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<String, String> 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);

View File

@ -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);
}

View File

@ -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<T> implements InvocationHandler, PropertiesConfigu
private final RestApiRegistry registry;
// 结果映射
private RestResultMapping mapping;
// 鉴权提供者
private RestAuthProvider authProvider;
/**
* 构造器
@ -93,11 +96,9 @@ public class RestProxyInvoker<T> 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<T> 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<T> 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<T> implements InvocationHandler, PropertiesConfigu
url = restApi.url();
} else {
// 构建基础url优先级从小到大依次找同时尝试取字典值
Optional<String> baseUrl = Stream.of(restApi.baseUrl(), this.baseUrl)
.filter(DataUtils::isNotBlank)
.findFirst()
.map(found -> found.startsWith("#") ? properties.getDictUrl(found.substring(1)) : found);
Optional<String> 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<T> 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();
}
}