使用前提
- 已经阅读过文档【oinone 7天从入门到精通】的6.2章节-集成平台
- 已经依赖了内置模块集成平台
eip
boot启动工程pom.xml
新增jar依赖<dependency> <groupId>pro.shushi.pamirs.core</groupId> <artifactId>pamirs-eip2-core</artifactId> </dependency> <dependency> <groupId>pro.shushi.pamirs.core</groupId> <artifactId>pamirs-eip2-view</artifactId> </dependency>
配置文件
application.yml
新增启动依赖模块pamirs: boot: modules: - eip eip: open-api: enabled: true route: # 开放接口访问IP,开放外网可以配置为0.0.0.0 host: 127.0.0.1 # 开放接口访问端口 port: 8094 # 认证Token加密的AES密钥 aes-key: NxDZUddmvdu3QQpd5jIww2skNx6U0w0uOAXj3NUCLu8=
一、新增开放接口示例代码
开放接口类定义
package pro.shushi.pamirs.demo.api.open;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
@Fun(TestOpenApiModelService.FUN_NAMESPACE)
public interface TestOpenApiModelService {
String FUN_NAMESPACE = "demo.open.TestOpenApiModelService";
@Function
TestOpenApiModel queryById(Long id);
}
开放接口实现类
package pro.shushi.pamirs.demo.core.open;
import org.apache.camel.ExtendedExchange;
import org.springframework.stereotype.Component;
import pro.shushi.pamirs.core.common.SuperMap;
import pro.shushi.pamirs.demo.api.open.TestEipConfig;
import pro.shushi.pamirs.demo.api.open.TestOpenApiModel;
import pro.shushi.pamirs.demo.api.open.TestOpenApiModelService;
import pro.shushi.pamirs.demo.api.open.TestOpenApiResponse;
import pro.shushi.pamirs.eip.api.IEipContext;
import pro.shushi.pamirs.eip.api.annotation.Open;
import pro.shushi.pamirs.eip.api.constant.EipFunctionConstant;
import pro.shushi.pamirs.eip.api.enmu.EipExpEnumerate;
import pro.shushi.pamirs.eip.api.entity.openapi.OpenEipResult;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import java.util.Optional;
@Fun(TestOpenApiModelService.FUN_NAMESPACE)
@Component
public class TestOpenApiModelServiceImpl implements TestOpenApiModelService {
@Override
@Function
public TestOpenApiModel queryById(Long id) {
return new TestOpenApiModel().queryById(id);
}
@Function
@Open
@Open.Advanced(
authenticationProcessorFun = EipFunctionConstant.DEFAULT_AUTHENTICATION_PROCESSOR_FUN,
authenticationProcessorNamespace = EipFunctionConstant.FUNCTION_NAMESPACE
)
public OpenEipResult<TestOpenApiResponse> queryById4Open(IEipContext<SuperMap> context , ExtendedExchange exchange) {
String id = Optional.ofNullable(String.valueOf(context.getInterfaceContext().getIteration("id"))).orElse("");
TestOpenApiModel temp = queryById(Long.valueOf(id));
TestOpenApiResponse response = new TestOpenApiResponse();
if(temp != null ) {
response.setAge(temp.getAge());
response.setId(temp.getId());
response.setName(temp.getName());
}else{
response.setAge(1);
response.setId(1L);
response.setName("oinone eip test");
}
OpenEipResult<TestOpenApiResponse> result = new OpenEipResult<TestOpenApiResponse>(response);
return result;
}
@Function
@Open(config = TestEipConfig.class,path = "error")
@Open.Advanced(
httpMethod = "post",
authenticationProcessorFun = EipFunctionConstant.DEFAULT_AUTHENTICATION_PROCESSOR_FUN,
authenticationProcessorNamespace = EipFunctionConstant.FUNCTION_NAMESPACE
)
public OpenEipResult<TestOpenApiResponse> queryById4OpenError(IEipContext<SuperMap> context , ExtendedExchange exchange) {
throw PamirsException.construct(EipExpEnumerate.SYSTEM_ERROR).appendMsg("测试异常").errThrow();
}
}
启动工程后去开放接口配置表,将接口的业务域字段conn_group_code
设置为其他
分类的编码DOM00007
SELECT * from eip_eip_open_interface where is_deleted = 0 order by id desc;
二、为调用方创建应用
通过集成接口
的应用创建,可以选择加密方式和开发接口列表中哪些接口授权该应用开放使用
应用创建完成后,可以在表格操作栏通过查看密钥
动作查看appKey
、appSecret
和数据传输加密密钥的公钥
三、验证请求
根据appKey和appSecret获取accessToken
curl --location 'http://127.0.0.1:8094/openapi/get/access-token?tenant=pamirs' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'appKey=c65a8d76a93f4427b3702c55903ddda2' \
--data-urlencode 'appSecret=W2cMGZ0X6Av0kQOoKT4TWTaBalXkGsignr1M0Yi7+JJWJHVniiTz2mb9THGA7W5F'
调用开放接口
调用方加密请求体工具类
package pro.shushi.pamirs.demo.core.open;
import com.alibaba.fastjson.JSON;
import pro.shushi.pamirs.core.common.EncryptHelper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 加密请求工具类
*/
public class SignRequestUtil {
/**
* 加密请求
* @param params 请求参数
* @param publicKey 数据传输加密密钥-公钥
* @param signMethod AES/RSA
* @return
*/
public static String AESSignRequest(Map<String, Object> params, String publicKey, String signMethod) {
String paramStr = JSON.toJSONString(params);
try {
String encryptParamStr = EncryptHelper.encryptByKey(EncryptHelper.getSecretKeySpec(signMethod, publicKey), paramStr);
return "{ result: \"" + encryptParamStr + "\" }";
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
Map<String, Object> params = new HashMap<>();
params.put("id", "111");
String encryptParam = AESSignRequest(params, "FUMxjil6tdrjjBCFFy34t4ScosZOfh6hPkEwYrNFBFQ=", "AES");
System.out.println(encryptParam);
// 得到结果 { result: "4hSjNkx9TUDjQI8OGkbiFQ=="}
}
}
curl --location 'http://127.0.0.1:8094/openapi/pamirs/queryById4Open' \
--header 'accessToken: T1SSd8aiJCFhsTyQepLCzRqcVsDH3wTjg9srXEZTp/S0rUbRAAIwmAX8Dgjbw0MM' \
--header 'Content-Type: application/json' \
--data '{ result: "4hSjNkx9TUDjQI8OGkbiFQ=="}'
Oinone社区 作者:nation原创文章,如若转载,请注明出处:https://doc.oinone.top/kai-fa-shi-jian/14901.html
访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验