在 HTTP 请求标头中插入用户凭据

发布于 2024-09-10 04:03:18 字数 788 浏览 1 评论 0原文

我试图将用户凭据插入到 HTTP 请求标头中,然后通过 https 发送到 Web 服务,Web 服务又读取它们以进行授权...

客户端和服务都是用 Java 编写的。

在客户端,我执行以下操作:

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());

如果我在添加后以编程方式转储 reqContext,我会看到添加的标头。但是通过 tcpmon,我可以看到,它们没有发送到 Web 服务...当然我也无法在 Web 服务中的任何地方找到它们。

知道我做错了什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还给你自由 2024-09-17 04:03:18

我相信您正在使用 jax-ws 进行 Web 服务消费,所以我的答案将面向它

您可以像这样设置用户名和密码属性

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

如果这不起作用,您可能需要一个身份验证器(不适用于 Axis2 )

此处复制

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());
    }
}

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文