首页 > 解决方案 > 如何在 apache camel 中使用基本身份验证进行 RESTful 调用?

问题描述

我有一个需要将日志文件发送到端点的 apache camel 应用程序,这需要基本身份验证。我能够将 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();

请帮助审查和进一步建议

标签: restauthenticationhttpsapache-camel

解决方案


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

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

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


推荐阅读