如何使用Apache Camel授权? [英] How to authorize using Apache Camel?
问题描述
我必须发出POST请求
I have to make a POST request
curl -X POST --data-binary @auth.json http://somehost.com/auth
{
"response": {
"status": "OK",
"token": "622cee5f8c99c81e87614e9efc63eddb"
}
}
,这将返回带有令牌的JSON响应.auth.json是带有登录名和密码的JSON文件.然后,我有两个选择:将令牌放在将来的请求中作为"Authorization:TOKEN"的标头中,或者将其放在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.com/auth");
}
});
context.start();
Thread.sleep(10000);
context.stop();
}而且我在./data/inbox
} 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.com/auth");
}
这篇关于如何使用Apache Camel授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!