Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Http基本身份验证不适用于Spring WS和WebServiceTemplate凭据_Java_Spring_Web Services_Soap_Spring Ws - Fatal编程技术网

Java Http基本身份验证不适用于Spring WS和WebServiceTemplate凭据

Java Http基本身份验证不适用于Spring WS和WebServiceTemplate凭据,java,spring,web-services,soap,spring-ws,Java,Spring,Web Services,Soap,Spring Ws,我尝试使用Spring(-WS)将HTTP基本身份验证凭据添加到SOAP请求中。请求本身可以工作,但不提交任何凭据。HTTP头应该如下所示: [...] Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1.1 (java 1.5) Authorization: Basic mybase64encodedtopsecretcredentials= 但最后一行并没有丢失。在MyConfig.java中,我配置Bean(无XML): 如果

我尝试使用Spring(-WS)将HTTP基本身份验证凭据添加到SOAP请求中。请求本身可以工作,但不提交任何凭据。HTTP头应该如下所示:

[...]
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Authorization: Basic mybase64encodedtopsecretcredentials=
但最后一行并没有丢失。在MyConfig.java中,我配置Bean(无XML):


如果您知道授权行丢失的原因,请告诉我。:)提前非常感谢

我也遇到了类似的问题,本文帮助了我:

如(Thank you@Milos)所述,您需要创建一个类:

public class WebServiceMessageSenderWithAuth extends HttpUrlConnectionMessageSender{

    @Override
    protected void prepareConnection(HttpURLConnection connection)
            throws IOException {

        Base64.Encoder enc = Base64.getEncoder();
        String userpassword = "login:password"; // change to a real user and password
        String encodedAuthorization = enc.encodeToString(userpassword.getBytes());
        connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);

        super.prepareConnection(connection);
    }
}
并传递给消息发送者方法:

setMessageSender(new WebServiceMessageSenderWithAuth());
setMessageSender(new WebServiceMessageSenderWithAuth());