views:

48

answers:

1

I'm trying to insert user credentials into an HTTP request header which is then sent via https to a web service, which in turn reads them for authorization purposes...

Client and Service are both written in Java.

On the client side I do the following:

ExampleImplService service = new ExampleImplService();
Example port = service.getExampleImplPort();

Map<String, Object> reqContext = ((BindingProvider) port).getRequestContext();
Map<String, List<String>> reqHeader = new HashMap<String, List<String>>();
reqHeader.put("Username", Collections.singletonList("user"));
reqHeader.put("Password", Collections.singletonList("password"));
reqContext.put(MessageContext.HTTP_REQUEST_HEADERS, reqHeader);

System.out.println(port.somemethod());

If I dump the reqContext after my additions programmatically I see the added headers. But via tcpmon, I can see, that they are not send to the web service... Naturally I can't find them anywhere in the web service either.

Any idea what I'm doing wrong?

+1  A: 

I believe you are using jax-ws for web service consumption so my answer will be oriented to it

You can set the username and password properties like this

reqContext.put( BindingProvider.USERNAME_PROPERTY, "user" );
reqContext.put( BindingProvider.PASSWORD_PROPERTY, "password" );

If that doesn't work, you may need an Authenticator ( Doesn't work with Axis2 )

copied from here

class MyAuthenticator extends Authenticator {
    private String username, password;

    public MyAuthenticator(String user, String pass) {
        username = user;
        password = pass;
        setDefault( this );
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        System.out.println("Requesting Host  : " + getRequestingHost());
        System.out.println("Requesting Port  : " + getRequestingPort());
        System.out.println("Requesting Prompt : " + getRequestingPrompt());
        System.out.println("Requesting Protocol: " + getRequestingProtocol());
        System.out.println("Requesting Scheme : " + getRequestingScheme());
        System.out.println("Requesting Site  : " + getRequestingSite());

        return new PasswordAuthentication(username, password.toCharArray());
    }
}
clavarreda