Java SOAP-WS安全标头身份验证
我已经使用spring+XSD+负载开发了一个webservices。我需要使用SOAP请求头中的用户名和密码对请求头进行身份验证,这是我使用SOAPUI实现的 我能够在请求中生成下面的标题Java SOAP-WS安全标头身份验证,java,soap,soapui,ws-security,soapheader,Java,Soap,Soapui,Ws Security,Soapheader,我已经使用spring+XSD+负载开发了一个webservices。我需要使用SOAP请求头中的用户名和密码对请求头进行身份验证,这是我使用SOAPUI实现的 我能够在请求中生成下面的标题 <soapenv:Envelope xmlns:jaxb="http://jaxb.miws.sg.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> &
<soapenv:Envelope xmlns:jaxb="http://jaxb.miws.sg.com/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis- 200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-C3092BFBAE5B212E93144378035575013">
<wsse:Username>User</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">test</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">CT1Fyo/g2WMaadE52bsnkg== </wsse:Nonce>
<wsu:Created>2015-10-02T10:05:55.750Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
使用者
测试
CT1Fyo/g2WMaadE52bsnkg==
2015-10-02T10:05:55.750Z
现在我想验证用户名和密码的标题元素
例:
案例1:
用户名=用户和密码=测试//身份验证通过并给出响应成功
案例2:
userName=User1和Password=test1//身份验证失败并给出响应失败
请帮助我提供适当的示例以实现相同的目的。SOAP Web服务中的处理程序(类似于拦截器/过滤器)可用于服务器端的身份验证目的,然后进一步链接请求。 请查看SOAPHandler,以解析有效负载中的头信息并验证用户名/密码。
SOAP Web服务中的处理程序(类似于拦截器/过滤器)可用于服务器端的身份验证目的,然后进一步链接请求。 请查看SOAPHandler,以解析有效负载中的头信息并验证用户名/密码。
以下是一些步骤:
handleMessage
方法,实现一个SOAPHandler
类handleMessage
方法中,评估上下文的MESSAGE\u OUTBOUND\u属性
。如果为false(意味着它是一条入站消息),则编写代码来内省context.getMessage()
。在那里,您可以评估MIME头、安全头和令牌以及主体,以确定是否需要拒绝身份验证凭据。如果是,请在方法末尾返回false
SoapHander
添加到服务的处理程序链中处理程序链.xml
添加到类路径:
示例.webservices.handler.Handler1
示例.webservices.handler.Handler2
有关完整的指南,请参见以下步骤:
handleMessage
方法,实现一个SOAPHandler
类handleMessage
方法中,评估上下文的MESSAGE\u OUTBOUND\u属性
。如果为false(意味着它是一条入站消息),则编写代码来内省context.getMessage()
。在那里,您可以评估MIME头、安全头和令牌以及主体,以确定是否需要拒绝身份验证凭据。如果是,请在方法末尾返回false
SoapHander
添加到服务的处理程序链中处理程序链.xml
添加到类路径:
示例.webservices.handler.Handler1
示例.webservices.handler.Handler2
有关完整的指南,请参阅
public class MyCustomSoapHandler implements SOAPHandler<SOAPMessageContext>
{
public Set<QName> getHeaders()
{
return Collections.emptySet();
}
public boolean handleMessage(SOAPMessageContext messageContext)
{
Boolean outboundProperty = (Boolean)
messageContext.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
//This is for handling messages going out of the conduit
} else {
//Here is where you want to authenticate
}
return true; //return false if do not want to proceed to the next handler in the chain
}
public boolean handleFault(SOAPMessageContext messageContext)
{
return true;
}
public void close(MessageContext messageContext)
{
}
@WebService(name = "Handler", targetNamespace = "http://example.org")
@HandlerChain(file="handler-chain.xml")
public class HandlerWS
{
@Resource
WebServiceContext ctx;
@WebMethod()
public String getProperty(String propertyName)
{
return (String) ctx.getMessageContext().get(propertyName);
}
}