【问题标题】:Camel Rest api consumer using SpringOAuthResttemplateCamel Rest api 消费者使用 SpringOAuthResttemplate
【发布时间】:2018-06-02 02:31:32
【问题描述】:

我必须从我的 Spring Boot 应用程序中调用几个 Rest Web 服务。我打算使用 Camel 来配置流程和其他 EIP 用例。一些端点正在使用 oAuth2 身份验证。我打算使用 Spring oAuthResttempalte。互联网上的所有示例都使用restlet、CXF 或camel-http。

Camel Rest Consmer

我无法找到仅使用 spring resttemplate 的单个示例。有人使用 Spring Resttemplate 实现 Camel Rest 消费者吗?

互联网上的一些示例使用码头服务器来使用休息端点。为什么你需要一个简单的休息消费者的码头服务器?

【问题讨论】:

  • 如果您有 Camel in Action 第 2 版书籍,请阅读 REST 章节,因为它介绍了如何将 Spring RestTemplate 与 Camel 一起使用
  • @ClausIbsen 我有这本书。认为你的意思是第 7 章-微服务 RestTemplate 用于构建微服务。 camelRestConfiguration 用于消费。它没有使用 spring 将请求发布到微服务。
  • 您可以按原样使用spring rest控制器,然后注入生产者模板等,并在需要时使用它来调用camel路由,然后您不需要使用rest-dsl但可以使用spring rest如果你想要的东西

标签: spring apache-camel spring-camel


【解决方案1】:

有人使用 Spring Resttemplate 实现 Camel Rest 消费者吗?

我不知道这一点,而且不太可能在那个方向上找到一些东西,因为 Camel 已经有 consume rest endpoints 的内置组件。

互联网上的一些示例使用码头服务器来使用休息端点。为什么你需要一个简单的休息消费者的码头服务器?

我相信码头被用作消费者而不是生产者端点。所以你不需要“服务器”。或者您可能看到了一个使用码头作为服务器来服务 OAuth 端点的示例?

如果您原谅我的做法,我建议您继续使用 Camel HTTP/Rest 功能来使用 OAuth 使用 REST API。我找到了this example on Gist

from("direct:authService").tracing()
    .setHeader(Exchange.HTTP_PATH)
        .simple("<auth service context>/oauth2/token")
    .setHeader("CamelHttpMethod")
        .simple("POST")
    .setHeader("Content-Type")
        .simple("application/x-www-form-urlencoded")
    .setHeader("Accept")
        .simple("application/json")
    .setBody()
        .constant("grant_type=client_credentials&client_id=<client id>&client_secret=<client sec>")
    .to("https4://<remote auth service url>")
        .convertBodyTo(String.class)
    .log("response from API: " + body())
    .choice()
        .when().simple("${header.CamelHttpResponseCode} == 200")
            .unmarshal().json(JsonLibrary.Jackson, AccessResponseToken.class)
            .setHeader("jwt").simple("${body.access_token}")
            .to("direct:<some direct route>")
        .otherwise()
    .log("Not Authenticated!!!");

如果您想坚持使用OAuthRestTemplate,您可以实现Processorbean 来包装这些调用并将授权令牌返回到您的路由。

【讨论】:

  • 谢谢。我需要使用 oAuthTemplate,因为我的客户端密码是使用 Springoauth 预先配置/填充的。我现在正在使用 bean 来调用该类。很惊讶听到骆驼不支持 spring resttemplate。
猜你喜欢
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
相关资源
最近更新 更多