【问题标题】:How to authorize using Apache Camel?如何授权使用 Apache Camel?
【发布时间】:2011-07-13 12:50:40
【问题描述】:

我必须发出一个 POST 请求

curl -X POST --data-binary @auth.json http://somehost.com/auth
{
    "response": {
        "status": "OK",
        "token": "622cee5f8c99c81e87614e9efc63eddb"
    }
}

,这将返回带有令牌的 JSON 响应。 auth.json 是一个带有登录名和密码的 JSON 文件。然后我有两个选择:将令牌作为“授权:令牌”放入未来请求的标头中,或者将其放入 cookie 并发出其他请求。 我怎样才能用 Apache Camel 做到这一点?如何接收 HTTP 响应?我在哪里放置令牌? 现在我有:

public static void main(String args[]) throws Exception {
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
        public void configure() {
            from("file:data/inbox?noop=true")
            .to("http://somehost.com/auth");
        }
    });
    context.start();
    Thread.sleep(10000);
    context.stop(); 

} 我在 ./data/inbox 中有 auth.json 文件

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    由于您在 Apache Camel 邮件列表上发布了相同的问题,因此我在此处提供了 an answer。

    总结一下:在发送 http 请求之前,只需在您的路由中调用 setHeader("Authorization", constant("622cee5f8c99c81e87614e9efc63eddb"))。 Camel 会自动将此标头转换为特定于传输的(在本例中为 HTTP)标头。当然你不需要在你的路由中提供一个常量token,你可以使用Camelexpression或者处理器来动态计算或者查找token。

    您的完整路线如下所示:

    context.addRoutes(new RouteBuilder() { 
        public void configure() { 
                from("file:data/out?fileName=filename.json&noop=true") 
                .setHeader("Authorization", constant("mytoken")) 
                .to("http://somehost.com/auth"); 
     } 
    

    【讨论】:

    • 我已尝试按照您的说明设置标题。但我无法通过标题。我尝试使用 exchange.getOut.setHeaders(headers) 但只有 content-type 没有通过授权标头。有什么建议吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多