Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Basic Authentication not working with Spring WS and WebServiceTemplate credentials

I try to add HTTP Basic Auth credentials to my SOAP-Request using Spring(-WS). The Request itself works, but no credentials are submitted. The HTTP header should look like:

[...]
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
Authorization: Basic mybase64encodedtopsecretcredentials=

But the last row is not missing. In MyConfig.java, I configure the Bean (no XML):

@Bean
public WebServiceTemplate webServiceTemplate() {
    WebServiceTemplate template = new WebServiceTemplate();
    try {
        template.setMarshaller(marshaller());  //Jaxb2Marshaller
        template.setUnmarshaller(marshaller());

        // proxy for tcpmon inspection
        template.setDefaultUri("http://127.0.0.1:29080/target/webservice.php");
        String username = environment.getProperty("config.username");
        String password = environment.getProperty("config.password");
        Credentials credentials = new UsernamePasswordCredentials(username, password);
        HttpComponentsMessageSender sender = new HttpComponentsMessageSender();
        sender.setCredentials(credentials);
        sender.afterPropertiesSet();
        template.setMessageSender(sender);
    } catch (Exception e) {
        // @todo: handle me
    }
    return template;
}

If you know the reason why the Authorization line is missing, please let me know. :) Thank you a lot in advance

like image 846
dexBerlin Avatar asked Sep 03 '25 04:09

dexBerlin


1 Answers

I had similar problem and this article helped me: https://looksok.wordpress.com/2014/09/06/spring-4-soap-request-with-http-basic-authentication/

like image 61
Milos Avatar answered Sep 04 '25 19:09

Milos