fix(认证): 新增AccessKey接口安全认证的方式

This commit is contained in:
lqyan
2024-07-24 16:35:44 +08:00
parent 40df7537d1
commit 0ac882f645
26 changed files with 830 additions and 519 deletions

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>com.jhinno</groupId>
<artifactId>jhinno-openapi-java-sdk-parent</artifactId>
<version>1.0.2</version>
<version>2.0.2</version>
</parent>
@@ -22,7 +22,7 @@
<dependency>
<groupId>com.jhinno</groupId>
<artifactId>jhinno-openapi-java-sdk</artifactId>
<version>1.0.2</version>
<version>2.0.2</version>
</dependency>
<dependency>

View File

@@ -1,6 +1,7 @@
package com.jhinno.sdk.openapi.autoconfigure;
import com.jhinno.sdk.openapi.client.JHApiClient;
import com.jhinno.sdk.openapi.client.JHApiHttpClientImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@@ -8,6 +9,7 @@ import org.springframework.context.annotation.Configuration;
/**
* openapi客户端自动配置
*
* @author yanlongqi
* @date 2024/6/4 16:01
*/
@@ -18,15 +20,18 @@ public class JHOpenapiClientAutoConfigure {
@Bean
@ConditionalOnMissingBean
public JHApiClient jhApiClient(JHOpenapiProperties properties){
return JHApiClient.build(
properties.getServerUrl(),
properties.getMaxTotal(),
properties.getMaxPerRout(),
properties.getSocketTimeout(),
properties.getConnectTimeout(),
properties.getConnectRequestTimeout()
);
public JHApiClient jhApiClient(JHOpenapiProperties properties) {
JHApiClient jhApiClient = new JHApiClient(properties.getServerUrl());
JHApiHttpClientImpl jhApiHttpClient = new JHApiHttpClientImpl();
jhApiHttpClient.setMaxPerRoute(properties.getMaxPerRout());
jhApiHttpClient.setSocketTimeout(properties.getSocketTimeout());
jhApiHttpClient.setMaxTotal(properties.getMaxTotal());
jhApiHttpClient.setConnectTimeout(properties.getConnectTimeout());
jhApiHttpClient.setConnectRequestTimeout(properties.getConnectRequestTimeout());
jhApiHttpClient.init();
jhApiHttpClient.createHttpClients();
jhApiClient.setApiHttpClient(jhApiHttpClient);
return jhApiClient;
}
}

View File

@@ -12,6 +12,9 @@ import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* openapi执行器自动配置
*
@@ -27,59 +30,24 @@ public class JHOpenapiExecutionAutoconfigure {
private final JHApiClient client;
@Bean
public JHAppApiExecution appApiExecution() {
JHAppApiExecution execution = new JHAppApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
}
@Bean
public JHDataApiExecution dataApiExecution() {
JHDataApiExecution execution = new JHDataApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
}
@Bean
public JHFileApiExecution fileApiExecution() {
JHFileApiExecution execution = new JHFileApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
}
@Bean
public JHJobApiExecution jobApiExecution() {
JHJobApiExecution execution = new JHJobApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
}
@Bean
public JHDepartmentApiExecution departmentApiExecution() {
JHDepartmentApiExecution execution = new JHDepartmentApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
}
@Bean
public JHUserApiExecution userApiExecution() {
JHUserApiExecution execution = new JHUserApiExecution(client);
execution.setTokenTimeout(properties.getTokenTimeout());
execution.setTokenResidueTime(properties.getTokenResidueTime());
execution.setUsedServerTime(properties.isUsedServerTime());
return execution;
public List<JHApiExecution> ApiExecution() {
List<JHApiExecution> executions = new ArrayList<>();
executions.add(new JHAppApiExecution());
executions.add(new JHDataApiExecution());
executions.add(new JHFileApiExecution());
executions.add(new JHJobApiExecution());
executions.add(new JHDepartmentApiExecution());
executions.add(new JHUserApiExecution());
executions.forEach(t -> {
t.setJhApiClient(client);
t.setForceGetToken(properties.isForceGetToken());
t.setAuthType(properties.getAuthType());
t.setAccessKey(properties.getAccessKey());
t.setAccessKeySecret(properties.getAccessKeySecret());
t.setTokenTimeout(properties.getTokenTimeout());
t.setTokenResidueTime(properties.getTokenResidueTime());
t.setUsedServerTime(properties.isUsedServerTime());
});
return executions;
}
}

View File

@@ -1,6 +1,6 @@
package com.jhinno.sdk.openapi.autoconfigure;
import com.jhinno.sdk.openapi.CommonConstant;
import com.jhinno.sdk.openapi.AuthType;
import com.jhinno.sdk.openapi.client.DefaultHttpClientConfig;
import com.jhinno.sdk.openapi.constant.CommonConstants;
import lombok.Data;
@@ -26,9 +26,9 @@ public class JHOpenapiProperties {
/**
* 设置服务每次能并行接收的请求数量,默认{@link DefaultHttpClientConfig#MAX_PER_ROUT}
* 设置服务每次能并行接收的请求数量,默认{@link DefaultHttpClientConfig#MAX_PER_ROUTE}
*/
private int maxPerRout = DefaultHttpClientConfig.MAX_PER_ROUT;
private int maxPerRout = DefaultHttpClientConfig.MAX_PER_ROUTE;
/**
* 设置服务socket连接超时的时间(单位:毫秒),默认{@link DefaultHttpClientConfig#SOCKET_TIMEOUT}
@@ -50,16 +50,38 @@ public class JHOpenapiProperties {
/**
* token的超时时间单位分钟
*/
private int tokenTimeout = CommonConstant.DEFAULT_TOKEN_EFFECTIVE_TIME;
private int tokenTimeout = DefaultHttpClientConfig.DEFAULT_TOKEN_EFFECTIVE_TIME;
/**
* token提前获取的时间单位分钟
*/
private int tokenResidueTime = CommonConstant.DEFAULT_TOKEN_RESIDUE_TIME;
private int tokenResidueTime = DefaultHttpClientConfig.DEFAULT_TOKEN_RESIDUE_TIME;
/**
* 是否使用服务器时间
*/
private boolean usedServerTime = CommonConstant.DEFAULT_IS_USED_SERVER_TIME;
private boolean usedServerTime = DefaultHttpClientConfig.DEFAULT_IS_USED_SERVER_TIME;
/**
* 是否强制获取用户的token默认{@link DefaultHttpClientConfig#DEFAULT_IS_FORCE_GET_TOKEN},
* 如果强制获取token则每次请求都去获取token
*/
private boolean isForceGetToken = DefaultHttpClientConfig.DEFAULT_IS_FORCE_GET_TOKEN;
/**
* 接口请求的认证类型
*/
private AuthType authType = AuthType.ACCESS_SECRET_MODE;
/**
* 访问密钥
*/
private String accessKey;
/**
* 访问密钥密码
*/
private String accessKeySecret;
}