Java Web服务客户端基本authentication

我在Glassfish上创build了一个JAX-WS Web服务,它需要基本的HTTPauthentication。

现在我想为该Web Service创build一个独立的Java应用程序客户端,但是我不知道如何传递用户名和密码。

它与Eclipse的Web服务浏览器一起工作,并检查我find的线路:

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 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ngin.ericsson.com/sna/types/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:listServiceScripts/> </soapenv:Body> </soapenv:Envelope> 

如何使用java代码在此“授权”头中传递用户名和密码? 它是散列还是类似的东西? algorithm是什么?

没有安全性,我有一个独立的java客户端:

 SnaProvisioning myPort = new SnaProvisioning_Service().getSnaProvisioningV10Port(); myPort.listServiceScripts(); 

用于基本authentication的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(); 

事实certificate,有一个简单,标准的方式来实现我想要的:

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

没有自定义的“太阳”类或外部依赖,并且没有手动编码任何东西。

我知道BASIC安全性不是很好,但我们也使用HTTPS。

对于Axis2客户端,这可能会有所帮助

 ... serviceStub = new TestBeanServiceStub("<WEB SERVICE URL>"); // Set your value HttpTransportProperties.Authenticator basicAuthenticator = new HttpTransportProperties.Authenticator(); List<String> authSchemes = new ArrayList<String>(); authSchemes.add(Authenticator.BASIC); basicAuthenticator.setAuthSchemes(authSchemes); basicAuthenticator.setUsername("<UserName>"); // Set your value basicAuthenticator.setPassword("<Password>"); // 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"); ... 

为了让您的生活更简单 ,您可以考虑使用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

如果您正在为客户端使用JAX-WS实现(如Metro Web Services),则以下代码显示如何在HTTP标头中传递用户名和密码:

  MyService port = new MyService(); MyServiceWS service = port.getMyServicePort(); Map<String, List<String>> credentials = new HashMap<String,List<String>>(); credentials.put("username", Collections.singletonList("username")); credentials.put("password", Collections.singletonList("password")); ((BindingProvider)service).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, credentials); 

随后对该服务的调用将被authentication。 注意密码只使用Base64编码,所以我鼓励你使用其他额外的机制,如客户端证书来提高安全性。

asíme funciono:

 BindingProvider bp = (BindingProvider) port; Map<String, Object> map = bp.getRequestContext(); map.put(BindingProvider.USERNAME_PROPERTY, "aspbbo"); map.put(BindingProvider.PASSWORD_PROPERTY, "9FFFN6P"); 

一些关于基本authentication的额外的上下文,它包含一个包含键/值对的头部:

授权:基本Z2VybWFuOmdlcm1hbg ==

其中“ 授权 ”是标题键,标题值有一个连接到“ Z2VybWFuOmdlcm1hbg == ”的string(“ 基本 ”字加空格 ),它是基址64中的用户和密码通过双点

 String name = "username"; String password = "secret"; String authString = name + ":" + password; String authStringEnc = new BASE64Encoder().encode(authString.getBytes()); ... objectXXX.header("Authorization", "Basic " + authStringEnc); 
Interesting Posts