Java 当用户名和密码在数据库中时,JAX-WS和基本身份验证
我是JAX-WS新手,有一件事我不明白 关于如何设置JAX-WS安全性,有很多教程,但在几乎所有情况下,BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性都存储在一些.xml文件中(取决于我相信的容器),它们是“硬编码”的。这就是我不明白的。如何通过将BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性与数据库中的用户名和密码进行比较来验证web服务客户端?我尝试在客户端设置BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性,如下所示:Java 当用户名和密码在数据库中时,JAX-WS和基本身份验证,java,jax-ws,tomcat6,basic-authentication,Java,Jax Ws,Tomcat6,Basic Authentication,我是JAX-WS新手,有一件事我不明白 关于如何设置JAX-WS安全性,有很多教程,但在几乎所有情况下,BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性都存储在一些.xml文件中(取决于我相信的容器),它们是“硬编码”的。这就是我不明白的。如何通过将BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性与数据库中的用户名和密码进行比较来验证web服务客户端?我尝试在客户端
ShopingCartService scs = new ShopingCartService(wsdlURL, name);
ShopingCart sc = scs.getShopingCartPort();
Map<String, Object> requestContext = ((BindingProvider)sc).getRequestContext();
requestContext.put(BindingProvider.USERNAME_PROPERTY, userName);
requestContext.put(BindingProvider.PASSWORD_PROPERTY, password);
sc.someFunctionCall();
@Resource
WebServiceContext wsContext;
@WebMethod
public void someFunctionCall() {
MessageContext mc = wsContext.getMessageContext();
mc.get(BindingProvider.USERNAME_PROPERTY);
mc.get(BindingProvider.PASSWORD_PROPERTY);
}
但我总是得到空值,我没有在xml中设置任何内容,web服务工作得很好,只是我无法获得这些变量:(
我同时运行Java1.6、Tomcat6和
非常感谢使用数据库中的密码对用户进行身份验证的任何帮助,
谢谢。BindingProvider.USERNAME\u属性和BindingProvider.PASSWORD\u属性与HTTP基本身份验证机制匹配,可在HTTP级别而非应用程序或servlet级别启用身份验证过程 基本上,只有HTTP服务器知道用户名和密码(以及最终根据HTTP/ApplicationServer规范的应用程序,如Apache/PHP)。 使用Tomcat/Java,在web.xml中添加一个登录配置BASIC和相应的安全约束/安全角色(稍后将与用户/真实用户组关联的角色)
我认为您正在寻找应用程序级别的JAX-WS身份验证,而不是服务器级别的HTTP basic。请参阅以下完整示例:
在web服务客户端站点上,只需将您的“用户名”和“密码”放入请求头中
Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("someUser"));
headers.put("Password", Collections.singletonList("somePass"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
我也遇到了同样的问题,并在这里找到了解决方案:
祝您好运对于同时使用应用程序级身份验证和HTTP基本身份验证的示例,请参见我的一个。我遇到了类似的情况,我需要向WS提供用户名、密码和WSS密码类型
我最初使用的是“Http基本身份验证”(如@ahoge),我也尝试使用@Philipp Dev的ref。我没有得到一个成功的解决方案
在谷歌进行了一番深入搜索后,我发现了以下帖子:
还有我的问题解决方案
我希望这能帮助其他人,就像帮助我一样
Rgds,
iVieL在客户端SOAP处理程序中,您需要设置javax.xml.ws.security.auth.username和javax.xml.ws.security.auth.password属性,如下所示:
public class ClientHandler implements SOAPHandler<SOAPMessageContext>{
public boolean handleMessage(final SOAPMessageContext soapMessageContext)
{
final Boolean outInd = (Boolean)soapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outInd.booleanValue())
{
try
{
soapMessageContext.put("javax.xml.ws.security.auth.username", <ClientUserName>);
soapMessageContext.put("javax.xml.ws.security.auth.password", <ClientPassword>);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
return true;
}
}
public类ClientHandler实现SOAPHandler{
公共布尔handleMessage(最终SOAPMessageContext SOAPMessageContext)
{
最终布尔输出=(布尔)soapMessageContext.get(MessageContext.MESSAGE\u OUTBOUND\u属性);
if(outId.booleanValue())
{
尝试
{
soapMessageContext.put(“javax.xml.ws.security.auth.username”,);
soapMessageContext.put(“javax.xml.ws.security.auth.password”);
}
捕获(例外e)
{
e、 printStackTrace();
返回false;
}
}
返回true;
}
}
如果您以这种方式将客户端的用户名和密码放入请求中:
URL url = new URL("http://localhost:8080/myapplication?wsdl");
MyWebService webservice = new MyWebServiceImplService(url).getMyWebServiceImplPort();
Map<String, Object> requestContext = ((BindingProvider) webservice).getRequestContext();
requestContext.put(BindingProvider.USERNAME_PROPERTY, "myusername");
requestContext.put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
然后,您可以在服务器端读取这样的基本身份验证字符串(您必须添加一些检查并执行一些异常处理):
@Resource
WebServiceContext WebServiceContext;
public void someMethodAtMyWebservice(字符串参数){
MessageContext=webserviceContext.getMessageContext();
Map httpRequestHeaders=(Map)messageContext.get(messageContext.HTTP_请求_头);
List authorizationList=(List)httpRequestHeaders.get(“授权”);
if(authorizationList!=null&&!authorizationList.isEmpty()){
String basicString=(String)authorizationList.get(0);
String encodedBasicString=basicString.substring(“Basic”.length());
String decoded=新字符串(Base64.getDecoder().decode(encodedBasicString),StandardCharsets.UTF_8);
String[]splitter=decoded.split(“:”);
字符串usernameFromBasicAuth=splitter[0];
字符串密码FromBasicAuth=拆分器[1];
}
我还尝试在SOAPHandler中检查BindingProvider.USERNAME\u属性,仍然为空。请问您从哪里获得这些信息?我需要官方信息源,但找不到任何具有足够信息的官方信息源!谢谢(最好在此处复制/粘贴解决方案…:P)
public class ClientHandler implements SOAPHandler<SOAPMessageContext>{
public boolean handleMessage(final SOAPMessageContext soapMessageContext)
{
final Boolean outInd = (Boolean)soapMessageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outInd.booleanValue())
{
try
{
soapMessageContext.put("javax.xml.ws.security.auth.username", <ClientUserName>);
soapMessageContext.put("javax.xml.ws.security.auth.password", <ClientPassword>);
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
return true;
}
}
URL url = new URL("http://localhost:8080/myapplication?wsdl");
MyWebService webservice = new MyWebServiceImplService(url).getMyWebServiceImplPort();
Map<String, Object> requestContext = ((BindingProvider) webservice).getRequestContext();
requestContext.put(BindingProvider.USERNAME_PROPERTY, "myusername");
requestContext.put(BindingProvider.PASSWORD_PROPERTY, "mypassword");
String response = webservice.someMethodAtMyWebservice("test");
@Resource
WebServiceContext webserviceContext;
public void someMethodAtMyWebservice(String parameter) {
MessageContext messageContext = webserviceContext.getMessageContext();
Map<String, ?> httpRequestHeaders = (Map<String, ?>) messageContext.get(MessageContext.HTTP_REQUEST_HEADERS);
List<?> authorizationList = (List<?>) httpRequestHeaders.get("Authorization");
if (authorizationList != null && !authorizationList.isEmpty()) {
String basicString = (String) authorizationList.get(0);
String encodedBasicString = basicString.substring("Basic ".length());
String decoded = new String(Base64.getDecoder().decode(encodedBasicString), StandardCharsets.UTF_8);
String[] splitter = decoded.split(":");
String usernameFromBasicAuth = splitter[0];
String passwordFromBasicAuth = splitter[1];
}