gpt4 book ai didi

java - 使用 Camel 进行 REST 服务调用,需要先调用身份验证 api

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:13 24 4
gpt4 key购买 nike

Camel 必须为某些集成调用 REST 服务,但是,REST 服务有一个身份验证 api (POST api),需要先调用它来获取 token ,然后其他后续 api 调用必须使用嵌入的 token 来调用在 HTTP 请求的 header 中。

Spring Restemplate 或 apache camel 是否有一些 api 来支持它们?

最佳答案

按照@gusto2 的方法,它工作得很好。

所以,我创建了两个路由 --> 第一个是基于计时器的,如下所示,它生成 token ,定期刷新它(因为路由是基于计时器的)并将 token 存储在局部变量中以供某些人重用其他路线。

@Component
public class RestTokenProducerRoute extends RouteBuilder {

private String refreshedToken;

@Override
public void configure() throws Exception {

restConfiguration().producerComponent("http4");

from("timer://test?period=1200000") //called every 20 mins
.process(
exchange -> exchange.getIn().setBody(
new UserKeyRequest("apiuser", "password")))
.marshal(userKeyRequestJacksonFormat) //convert it to JSON
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http4://localhost:8085/Service/Token")
.unmarshal(userKeyResponseJacksonFormat)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
UserKeyResponse response= exchange.getIn().getBody(
UserKeyResponse.class); //get the response object
System.out.println(response + "========>>>>>>" +
response.getResult());
setRefreshedToken(response.getResult()); //store the token in some object
}
}).log("${body}");
}

public String getRefreshedToken() {
return refreshedToken;
}

public void setRefreshedToken(String refreshedToken) {
this.refreshedToken = refreshedToken;
}
}

而第二个路由可以调用后续的api,这些api将使用第一个路由生成的token,就像这样。必须添加错误处理方案,其中 token 可能无效或已过期。但我想这将是需要单独解决的问题。

@Component
public class RestTokenUserOnboardRoute extends RouteBuilder {

private JacksonDataFormat OtherDomainUserRequestJacksonFormat = new JacksonDataFormat(
OtherDomainUserRequest.class);
private JacksonDataFormat OtherDomainUserResponseJacksonFormat = new JacksonDataFormat(
OtherDomainUserResponse.class);
@Override
public void configure() throws Exception {

restConfiguration().producerComponent("http4");

//This route is subscribed to a Salesforce topic, which gets invoked when there is any new messages in the topic.
from("salesforce:CamelTestTopic?sObjectName=MyUser__c&sObjectClass="+MyUser__c.class.getName()))
.convertBodyTo(OtherDomainUserRequest.class)
.marshal(OtherDomainUserRequestJacksonFormat).log("${body}")
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.log("The token being passed is ==> ${bean:tokenObj?method=getRefreshedToken}")
.setHeader("Authorization", simple("${bean:tokenObj?method=getRefreshedToken}"))
.to("http4://localhost:8085/Service/DomainUser")
.unmarshal(OtherDomainUserResponseJacksonFormat)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
OtherDomainUserResponse response = exchange.getIn().getBody(
OtherDomainUserResponse.class);
System.out.println(response + "==================>>>>>> " + response.getStatusCode());
}
}).log("${body}");
}
}

因此,这里 token 从 tokenObj bean(RestTokenProducerRoute 的实例,它定义了方法 getRefreshedToken()。它返回存储的 token 。

不用说,您已经在 camelcontext 注册表中设置了 bean 以及其他设置(如组件、路由等),如下所示。就我而言,情况如下。

@Autowired
public RestTokenUserOnboardRoute userOnboardRoute;
@Autowired
public RestTokenProducerRoute serviceTokenProducerRoute;

@Autowired
private RestTokenProducerRoute tokenObj;

@Override
protected CamelContext createCamelContext() throws Exception {
SimpleRegistry registry = new SimpleRegistry();
registry.put("tokenObj", tokenObj); //the tokenObj bean,which can be used anywhere in the camelcontext
SpringCamelContext camelContext = new SpringCamelContext();
camelContext.setRegistry(registry); //add the registry
camelContext.setApplicationContext(getApplicationContext());
camelContext.addComponent("salesforce", salesforceComponent());
camelContext.getTypeConverterRegistry().addTypeConverter(DomainUserRequest.class, MyUser__c.class, new MyTypeConverter());
camelContext.addRoutes(route()); //Some other route
camelContext.addRoutes(serviceTokenProducerRoute); //Token producer Route
camelContext.addRoutes(userOnboardRoute); //Subsequent API call route
camelContext.start();
return camelContext;
}

这解决了我动态设置 token 的问题, token 是由于执行其他路由而生成的。

关于java - 使用 Camel 进行 REST 服务调用,需要先调用身份验证 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com