0

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 Answer 1

2

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.