将通用身份验证标头添加到所有骆驼Restlet路线

时间:2018-10-15 06:42:26

标签: rest header apache-camel authorization restlet

我有很多路线

from("restlet://api1").to("dest1-rest-url");
from("restlet://api2").to("dest2-rest-url");
from("restlet://api3").to("dest3-rest-url");
..
from("restlet://api100").to("dest100-rest-url");

所有路由都分布在不同的文件中。

现在,所有目标Rest URL都需要设置AUTH标头。设置在每个地方都会很麻烦。

有没有一种方法,我可以设置/配置一个标头,该标头会在调用任何其他API之前附加。

2 个答案:

答案 0 :(得分:1)

恕我直言,一个可能而优雅的解决方案是使用骆驼拦截器。 在“ interceptSendToEndpoint”上使用通配符,您应该能够拦截对“ restlet *”(或“ http *”端点)的所有调用,以便在继续操作之前插入/添加“ Authorization” http标头路线。 更多信息在这里: http://camel.apache.org/intercept.html

答案 1 :(得分:0)

添加解决了部分问题的答案

interceptFrom("restlet*").process( e -> { e.getOut().setHeader("Authorization":"Basic <authkey>"); });

注意::您可能必须设置其他参数,例如“内容类型”,“ HTTP方法”,“其他标头”,“从传入交换到传出交换的正文”。