美文网首页
Http Soap Basic Authentication

Http Soap Basic Authentication

作者: 那些真真实实 | 来源:发表于2020-05-21 00:19 被阅读0次

问题

测试接口用的工具是SoapUI,可以在Request Properties中填入Username和Password用于请求验证,但在代码中使用的是基于Hutool的SoapClient工具。
看了SoapClient内部实现,没有找到可以在哪里设置Basic Authentication验证。


Request Properties.png

解决

在网上查看相关资料发现2种方式

  • 在Http请求头上添加Basic Authentication认证
  • 使用Authenticator重写getPasswordAuthentication()

在Http请求头上添加Basic Authentication认证

// httpConn 方式
httpConn.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64String("username:password".getBytes()));
// httpPost 方式
httpPost.addHeader("Authorization", "Basic " + Base64.encodeBase64String("username:password".getBytes()));

使用Authenticator重写getPasswordAuthentication()

在 SoapClient 中找了很长时间也没发现可以在哪里设置请求头,只好使用此方式!

        // http 证授权方式
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 过滤指定域名授权
                if (this.getRequestingURL().toString().indexOf("www.jianshu.com") > 0) {
                    // basic 授权
                    return new PasswordAuthentication(username, password.toCharArray());
                }
                return null;
            }
        });
        // 初始接口客户端-hutool工具
        SoapClient soapClient = this.create(url,
                "urn:ZzzzzzList", "urn:sap-com:document:sap:soap:functions:mc-style"
        );

进展

  • 2020-05-21 已经在Gitee上向Hutool提交Issues
  • 2020-06-27 在新版本5.3.6增加支持

参考

相关文章

网友评论

      本文标题:Http Soap Basic Authentication

      本文链接:https://www.haomeiwen.com/subject/klooohtx.html