如何授权使用Apache Camel?

编程入门 行业动态 更新时间:2024-10-26 21:30:12
本文介绍了如何授权使用Apache Camel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我必须发出 POST 请求

I have to make a POST request

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

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

, and this will return a JSON response with the token. auth.json is a JSON file with login and password. I then have two options: put the token in the header in future requests as "Authorization: TOKEN", or put it in a cookie and make other requests. How can I do it with Apache Camel? How can I receive HTTP response? Where do I put the token? Now I have:

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/auth");
        }
    });
    context.start();
    Thread.sleep(10000);
    context.stop(); 

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

} and I have the auth.json file in ./data/inbox

推荐答案

由于您在 Apache Camel 邮件列表上发布了相同的问题,因此我提供了 一个答案.

Since you posted the same question on the Apache Camel mailing list I've provided an answer there.

总结:在发送http请求之前,只需在路由中调用 setHeader("Authorization", constant("622cee5f8c99c81e87614e9efc63eddb")) 即可.Camel 会自动将此标头转换为特定于传输的(在本例中为 HTTP)标头.当然,您不需要在路由中提供常量令牌,您可以使用 Camel 表达式 或处理器.

To summarize: Just call setHeader("Authorization", constant("622cee5f8c99c81e87614e9efc63eddb")) in your route before sending the http request. Camel will automatically translate this header to a transport specific (in this case HTTP) header. Of course you don't need to provide a constant token in your route, you can dynamically calculate or lookup the token by using a Camel expression or processor.

您的完整路线如下所示:

Your complete route will look something like:

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

这篇关于如何授权使用Apache Camel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-19 22:34:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/971335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Apache   Camel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!