Java Web 服务客户端基本身份验证
我在 Glassfish 之上创建了一个 JAX-WS Web 服务,它需要基本的 HTTP 身份验证。
现在我想为该 Web 服务创建一个独立的 java 应用程序客户端,但我不知道如何传递用户名和密码。
它与 Eclipse 的 Web 服务资源管理器配合使用,并检查了我发现的线路:
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>
How do I pass the username and password in this "Authorization" header using java code?它是散列的还是类似的东西?算法是什么?
如果不涉及安全性,我有一个可以工作的独立 java 客户端:
SnaProvisioning myPort = new SnaProvisioning_Service().getSnaProvisioningV10Port();
myPort.listServiceScripts();
I have created a JAX-WS Web Service on top of Glassfish which requires basic HTTP authentication.
Now I want to create a standalone java application client for that Web Service but I don't have a clue of how to pass the username and password.
It works with Eclipse's Web Service explorer, and examining the wire I found this:
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>
How do I pass the username and password in this "Authorization" header using java code? Is it hashed or something like that? What is the algorithm?
Without security involved I have a working standalone java client:
SnaProvisioning myPort = new SnaProvisioning_Service().getSnaProvisioningV10Port();
myPort.listServiceScripts();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
为了让您的生活变得更简单,您可能需要考虑使用 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
。To make your life simpler, you may want to consider using JAX-WS framework such as Apache CXF or Apache Axis2.
Here is the link that describes how to setup WS-Security for Apache CXF -> http://cxf.apache.org/docs/ws-security.html
EDIT
By the way, the
Authorization
field just uses simple Base64 encoding.According to this ( http://www.motobit.com/util/base64-decoder-encoder.asp ), the decoded value is
german:german
.如果您使用 JAX-WS,以下内容对我有用:
If you use JAX-WS, the following works for me:
要实现此功能,最简单的选择是在请求下包含用户名和密码。请参阅下面的示例。
The easiest option to get this working is to include Username and Password under of request. See sample below.
对我来说,下面的代码有效
For me below code works
用于基本身份验证的 JAX-WS 方式是
The JAX-WS way for basic authentication is
事实证明,有一种简单、标准的方法可以实现我想要的:
没有自定义的“sun”类或外部依赖项,也没有手动编码任何内容。
我知道 BASIC 安全性并不安全,但我们也在使用 HTTPS。
It turned out that there's a simple, standard way to achieve what I wanted:
No custom "sun" classes or external dependencies, and no manually encode anything.
I'm aware that BASIC security is not, well, secure, but we are also using HTTPS.
对于 Axis2 客户端这可能会有所帮助
for
Axis2
client this may be helpful有关基本身份验证的一些附加上下文,它包含在包含键/值对的标头中:
授权:基本 Z2VybWFuOmdlcm1hbg==
其中“授权< /em>”是标题键,
标题值有一个字符串(“Basic”单词加空格)连接到“ Z2VybWFuOmdlcm1hbg==”,即用户名和密码以双点连接的base 64
Some context additional about basic authentication, it consists in a header which contains the key/value pair:
Authorization: Basic Z2VybWFuOmdlcm1hbg==
where "Authorization" is the headers key,
and the headers value has a string ( "Basic" word plus blank space) concatenated to "Z2VybWFuOmdlcm1hbg==", which are the user and password in base 64 joint by double dot
如果您的客户端使用 JAX-WS 实现(例如 Metro Web Services),则以下代码显示如何在 HTTP 标头中传递用户名和密码:
然后将对服务的后续调用进行身份验证。请注意,密码仅使用 Base64 进行编码,因此我鼓励您使用其他附加机制(例如客户端证书)来提高安全性。
If you are using a JAX-WS implementation for your client, such as Metro Web Services, the following code shows how to pass username and password in the HTTP headers:
Then subsequent calls to the service will be authenticated. Beware that the password is only encoded using Base64, so I encourage you to use other additional mechanism like client certificates to increase security.
这对我有用:
This worked for me: