java 如何使用 JAX-WS / SOAP 对 Web 服务客户端进行身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14220792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:30:31  来源:igfitidea点击:

How to authenticate a Web Service Client with JAX-WS / SOAP

javaweb-servicesjakarta-eeservlets

提问by Chris

I am new to web service and tried some tutorials. But I stumble on patrol when I try to access a HTTPS secure SOAP web service. I can access the WSDLwith my browser and Netbeans with a login/password that I have revived from the provider.

我是 Web 服务的新手,并尝试了一些教程。但是当我尝试访问 HTTPS 安全 SOAP Web 服务时,我偶然发现了巡逻。我可以使用WSDL我从提供程序恢复的登录名/密码使用我的浏览器和 Netbeans访问。

Netbeans generate service/schema classes (via authentication) and I run my client on Glassfish 3.1.2. I have added a Servlet to access the Service. But I get a HTTP response code: 401 (=Unauthorized) when trying to access the service and WSDL. The service do connects to HTTPS, but the error tells HTTP.

Netbeans 生成服务/模式类(通过身份验证),我在 Glassfish 3.1.2 上运行我的客户端。我添加了一个 Servlet 来访问服务。但是我在尝试访问服务和WSDL. 该服务确实连接到HTTPS,但错误告诉HTTP

What is the correct way to add the username/password to access a Web Service ?

添加用户名/密码以访问 Web 服务的正确方法是什么?

Service provider for the Servlet

Servlet 的服务提供者

private static LogicalModel pullpullEnterpriseService() {
    pullEnterpriseService service = new pullEnterpriseService();        
    ClientPullSoapBinding port = service.getClientPullSoapBinding();        

    BindingProvider prov = ((BindingProvider)port);
    prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myuser");
    prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");

    return port.getDataModel();
}

I tried with providing user/password in the SOAP header to, but with the same result

我尝试在 SOAP 标头中提供用户/密码,但结果相同

    ...
    Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();        

    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    headers.put("Username", Collections.singletonList("myuser"));
    headers.put("Password", Collections.singletonList("mypassword"));
    req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    ...

I have looked at these post, but they do not solve my problem:

我看过这些帖子,但它们并没有解决我的问题:

How do I add a SOAP Header using Java JAX-WS

如何使用 Java JAX-WS 添加 SOAP 标头

http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/

http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/

回答by Paul Vargas

You can try Storing the WSDL Locally in your project.

您可以尝试在您的项目中本地存储 WSDL。

See an example in Consuming a Web Service with Java 6 and JAX-WS - Wiki - Confluence.

请参阅使用 Java 6 和 JAX-WS-Wiki-Confluence 使用 Web 服务中的示例。

Then you can use the way in the Specifying the Endpoint Address and HTTP Basic Authorizationsection, like your first code in your question.

然后您可以使用指定端点地址和 HTTP 基本授权部分中的方式,就像您问题中的第一个代码一样。

I hope this help you.

我希望这对你有帮助。