java身份认证_Java Web Service客户端基本身份验证
Java Web Service客户端基本身份验证我在Glassfish之上创建了一个JAX-WS Web服务,它需要基本的HTTP身份验证。现在,我想为该Web服务创建一个独立的Java应用程序客户端,但是我不知道如何传递用户名和密码。它可以与Eclipse的Web Service资源管理器一起使用,并检查连接线,我发现了这一点:POST /SnaProvisioning/SnaProvisio
Java Web Service客户端基本身份验证
我在Glassfish之上创建了一个JAX-WS Web服务,它需要基本的HTTP身份验证。
现在,我想为该Web服务创建一个独立的Java应用程序客户端,但是我不知道如何传递用户名和密码。
它可以与Eclipse的Web Service资源管理器一起使用,并检查连接线,我发现了这一点:
POST /SnaProvisioning/SnaProvisioningV1_0 HTTP/1.1
Host: localhost:8080
Content-Type: text/xml; charset=utf-8
Content-Length: 311
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: IBM Web Services Explorer
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: ""
Authorization: Basic Z2VybWFuOmdlcm1hbg==
Connection: close
如何使用Java代码在此“授权”标头中传递用户名和密码? 是散列还是类似的东西? 什么是算法?
没有涉及安全性,我有一个工作的独立Java客户端:
SnaProvisioning myPort = new SnaProvisioning_Service().getSnaProvisioningV10Port();
myPort.listServiceScripts();
8个解决方案
65 votes
基本身份验证的JAX-WS方法是
Service s = new Service();
Port port = s.getPort();
BindingProvider prov = (BindingProvider)port;
prov.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "myusername");
prov.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
port.call();
Jonathan Barbero answered 2020-07-03T23:36:53Z
45 votes
事实证明,有一种简单的标准方法可以实现我想要的目标:
import java.net.Authenticator;
import java.net.PasswordAuthentication;
Authenticator myAuth = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("german", "german".toCharArray());
}
};
Authenticator.setDefault(myAuth);
没有自定义的“ sun”类或外部依赖项,也没有手动编码任何内容。
我知道BASIC安全性不是很好,但是我们也在使用HTTPS。
German answered 2020-07-03T23:37:22Z
9 votes
对于Axis2客户端这可能会有所帮助
...
serviceStub = new TestBeanServiceStub(""); // Set your value
HttpTransportProperties.Authenticator basicAuthenticator = new HttpTransportProperties.Authenticator();
List authSchemes = new ArrayList();
authSchemes.add(Authenticator.BASIC);
basicAuthenticator.setAuthSchemes(authSchemes);
basicAuthenticator.setUsername(""); // Set your value
basicAuthenticator.setPassword(""); // Set your value
basicAuthenticator.setPreemptiveAuthentication(true);
serviceStub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, basicAuthenticator);
serviceStub._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, "false");
...
Avil answered 2020-07-03T23:37:42Z
6 votes
关于基本身份验证的一些附加上下文,它包含一个包含键/值对的标头:
授权:基本Z2VybWFuOmdlcm1hbg ==
其中“授权”是标题键,标头值包含一个字符串(“ Basic”一词加空格),并连接到“ Z2VybWFuOmdlcm1hbg ==“”,该用户名和密码在基数64中由双点连接
String name = "username";
String password = "secret";
String authString = name + ":" + password;
String authStringEnc = new BASE64Encoder().encode(authString.getBytes());
...
objectXXX.header("Authorization", "Basic " + authStringEnc);
José Luis González Vergara answered 2020-07-03T23:38:11Z
5 votes
如果您正在为客户端使用JAX-WS实现(例如Metro Web Services),则以下代码显示如何在HTTP标头中传递用户名和密码:
MyService port = new MyService();
MyServiceWS service = port.getMyServicePort();
Map> credentials = new HashMap>();
credentials.put("username", Collections.singletonList("username"));
credentials.put("password", Collections.singletonList("password"));
((BindingProvider)service).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, credentials);
然后,将对该服务的后续调用进行身份验证。 请注意,密码仅使用Base64进行编码,因此建议您使用其他附加机制(例如客户端证书)来提高安全性。
Gonzalo Garcia Lasurtegui answered 2020-07-03T23:38:36Z
2 votes
这对我有用:
BindingProvider bp = (BindingProvider) port;
Map map = bp.getRequestContext();
map.put(BindingProvider.USERNAME_PROPERTY, "aspbbo");
map.put(BindingProvider.PASSWORD_PROPERTY, "9FFFN6P");
Rolando F. answered 2020-07-03T23:38:56Z
1 votes
为了简化您的生活,您可能需要考虑使用JAX-WS框架,例如Apache CXF或Apache Axis2。
这是描述如何为Apache CXF设置WS-Security的链接-> [http://cxf.apache.org/docs/ws-security.html]
编辑顺便说一下,Authorization字段仅使用简单的Base64编码。据此([http://www.motobit.com/util/base64-decoder-encoder.asp]),解码后的值为german:german。
Alexander Pogrebnyak answered 2020-07-03T23:39:25Z
0 votes
如果您使用JAX-WS,以下对我有用:
//Get Web service Port
WSTestService wsService = new WSTestService();
WSTest wsPort = wsService.getWSTestPort();
// Add username and password for Basic Authentication
Map reqContext = ((BindingProvider)
wsPort).getRequestContext();
reqContext.put(BindingProvider.USERNAME_PROPERTY, "username");
reqContext.put(BindingProvider.PASSWORD_PROPERTY, "password");
Winter MC answered 2020-07-03T23:39:44Z
更多推荐
所有评论(0)