【问题标题】:CXF RESTful Client - How to do basic http authentication without Spring?CXF RESTful 客户端 - 如何在没有 Spring 的情况下进行基本的 http 身份验证?
【发布时间】:2011-10-24 19:18:16
【问题描述】:

我熟悉使用 Jersey 来创建 RESTful Web 服务服务器和客户端,但由于 class loading issues,我正在尝试将 Jersey 客户端转换为 CXF。我相信我想使用以 HTTP 为中心的客户端,但我们不使用 Spring。我们需要使用基本的 HTTP 身份验证。 user guide 有这个例子:

WebClient client = WebClient.create("http:books", "username", "password", "classpath:/config/https.xml");

第一个参数不是 URI 字符串。它是Spring使用的格式吗?这个方法只能用于使用Spring创建WebClients吗?

显示的另一种身份验证方式是添加标题字符串:

String authorizationHeader = "Basic " + org.apache.cxf.common.util.Base64Utility.encode("user:password".getBytes());
webClient.header("Authorization", authorizationHeader);

我猜"user:password" 应该替换为实际值,但希望得到确认。

【问题讨论】:

    标签: java rest client cxf http-authentication


    【解决方案1】:

    此答案来自 CXF 用户邮件列表。

    上面引用的第一个示例中有一个错字。它已更新为:

    WebClient client = WebClient.create("http://books", "username", "password", "classpath:/config/https.xml");
    

    如果未使用 Spring 配置文件(因此 Spring)未使用,则第四个参数可以为 null。

    所以,这对我有用:

    private WebClient webClient;
    
    public RESTfulClient(String url, String username, String password)
            throws IllegalArgumentException
    {
        this.username = username;
        this.password = password;
        this.serviceURL = url;
    
        if (username == null || password == null || serviceURL == null)
        {
            String msg = "username, password and serviceURL MUST be defined.";
            log.error(msg);
            throw new IllegalArgumentException(msg);
        }
    
        webClient = WebClient.create(this.serviceURL,
                this.username,
                this.password,
                null); // Spring config file - we don't use this
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2018-03-19
      • 2020-02-12
      • 2018-10-06
      • 1970-01-01
      • 2018-07-03
      • 2015-07-09
      • 2011-10-18
      相关资源
      最近更新 更多