Java SAAJ 基本认证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17042259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:47:44  来源:igfitidea点击:

Java SAAJ Basic Authentication

javaweb-servicesauthenticationsoapsaaj

提问by Ben

I have a basic SOAP service endpoint, actually SAP ECC, presenting a service. I have tested the service using SOAPUI 4.5, and it works ok using HTTP Auth, preemptive by the looks of things. I see an outbound "Authorization:Basic BASE64" and the service responds appropriately.

我有一个基本的 SOAP 服务端点,实际上是 SAP ECC,提供了一个服务。我已经使用 SOAPUI 4.5 测试了该服务,它使用 HTTP 身份验证可以正常工作,从外观上看是先发制人的。我看到一个出站的“授权:基本 BASE64”并且服务做出了适当的响应。

I am now trying to roll this into Java. I thought that I would take a SAAJ approach with:

我现在正试图将其融入 Java。我认为我会采用 SAAJ 方法:

 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
 SOAPConnection soapConnection = soapConnectionFactory.createConnection();
 String url = "http://SAPSERVER:8006/sap/bc/srt/rfc/sap/z_lookup_generic_prototype/300/z_user/z_user_binding";
 SOAPMessage message = messageFactory.createMessage();
 SOAPMessage response = connection.call(message, url);

But I cannot find a way to add the HTTP authentication in. I believe that SAAJ provides the means to control the SOAP message, but how do I add authentication in? Are there any alternatives worth considering?

但是我找不到添加 HTTP 身份验证的方法。我相信 SAAJ 提供了控制 SOAP 消息的方法,但是我如何添加身份验证?有没有值得考虑的替代方案?

回答by Floww

From this page : https://www.coderanch.com/how-to/java/WebServicesHowTo

从这个页面:https: //www.coderanch.com/how-to/java/WebServicesHowTo

SOAPMessage message = ...
String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);