如何在Apache骆驼中使用基本身份验证进行RESTful呼叫?

时间:2020-02-18 13:47:53

标签: rest authentication https apache-camel

我有一个Apache骆驼应用程序,该应用程序需要将日志文件发送到端点,这需要基本身份验证。我能够按照骆驼文档中的指定将authMethod,authusername和authPassword传递给url,但是我遇到的挑战是,启动应用程序后,我始终从端点获得空响应。 但是,同一端点使用邮递员返回响应代码和响应正文。

下面是我的代码:

from("{{routes.feeds.working.directory}}?idempotent=true")
                .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
                    multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                    String fileName = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
                    File file = exchange.getIn().getBody(File.class);
                    multipartEntityBuilder.addPart("file",
                            new FileBody(file, ContentType.MULTIPART_FORM_DATA, fileName));
                    exchange.getOut().setBody(multipartEntityBuilder.build());
                    Message out = exchange.getOut();
                    int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
                    log.info("response code "+responseCode);

            }
        })
                .setHeader(Exchange.HTTP_QUERY, 
            constant("authMethod=Basic&authUsername="+username+"&authPassword="+password+""))
                .to(TARGET_WITH_AUTH +"/"+uuid+"/files")
                .log(LoggingLevel.DEBUG, "response code >>>>"+Exchange.HTTP_RESPONSE_CODE)
                .log(LoggingLevel.INFO, "RESPONSE BODY ${body}")
                .end();

请帮助审查并提供进一步建议

2 个答案:

答案 0 :(得分:0)

对于HTTP基本身份验证,我在发送请求之前先使用它

<setHeader headerName="Authorization">
    <constant>Basic cm9vdDpyb290</constant>
</setHeader>

cm9vdDpyb290-编码的Base64 root:root(用户名和密码)字符串

答案 1 :(得分:0)

使用httpClient通过基本身份验证发送我的请求已解决此问题。显然,Apache骆驼中的authMethod不会将凭据与“发帖请求”一起发送,这就是为什么我要获得初始401响应代码的原因。 谢谢大家的贡献。