在 Apache Camel 路由中调用通过 Bearer 令牌身份验证保护的其余 api

问题描述 投票:0回答:1

我需要从我的 Apache Camel 路由调用 Rest API。 Rest API 使用可以在其他 API 上获取的令牌来保护。

我的演示路线如下所示:

    from("direct:iot")
        .id("iot")
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader(Exchange.CONTENT_TYPE, constant("application/x-www-form-urlencoded"))
        .setBody(simple(STR."username=\{USERNAME}&password=\{PASSWORD}&grant_type=password&client_id=iot-api-client&client_secret=\{CLIENT_SECRET}"))
        .to(TOKEN_URL)
        .unmarshal().json(JsonLibrary.Gson)
        .process(e -> {
            Map body = e.getIn().getBody(Map.class);
            e.getIn().setHeader("Authorization",
                STR."Bearer \{body.get("access_token")}");
        })
        .removeHeader("*")
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .to(STR."\{BASE_URL}\{RESOURCE_URI}")
        .log("${body}");

它可以工作,但对我来说似乎相当复杂(我还应该添加一些错误处理、令牌缓存等)。我想知道是否可以以某种方式简化它。

APACHE CAMEL 4.2 WHAT'S NEW 中,他们写道:

“camel-http 组件现在支持 OAuth 2.0 客户端 身份验证。”

不幸的是我找不到任何细节。

java authentication apache-camel spring-camel
1个回答
0
投票

您可以看到这个单元测试

HttpOAuth2AuthenticationTest
,还有一些文档,因为 http 组件上有新的 oauth 选项。

© www.soinside.com 2019 - 2024. All rights reserved.