[英]SOAP-WS security header authentication
我已經使用spring + XSD + Payload開發了一個Web服務。 我需要使用我通過SOAPUI實現的SOAP請求標頭中的用戶名和密碼對請求標頭進行身份驗證
我可以在請求中生成以下標頭
<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>
現在,我想驗證userName和Password的標題元素。
例如:
情況1:userName = User和Password = test //通過身份驗證並給出響應成功
情況2:userName = User1和Password = test1 //驗證失敗並給出響應失敗
請幫助我提供合適的樣本以達到相同的效果。
SOAP Web服務中的處理程序(類似於攔截器/過濾器)可以用於服務器端的身份驗證,然后進一步鏈接請求。 請看一下SOAPHandler來解析有效負載中的頭信息並驗證用戶名/密碼。 服務器端的SOAP處理程序
以下是一些步驟:
handleMessage
方法來實現SOAPHandler
類。 handleMessage
方法中,評估上下文的MESSAGE_OUTBOUND_PROPERTY
。 如果它為假(表示它是入站消息),則編寫內省context.getMessage()
代碼。 您可以在此處評估MIME標頭,安全標頭和令牌以及正文,以確定是否需要拒絕身份驗證憑據。 如果這樣做,則在方法末尾返回false
。 SoapHander
添加到服務的Handler鏈中。 SOAPHandler的示例:
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)
{
}
這是您需要添加到服務的handlerChain的SOAPHandler的入門模板:
@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);
}
}
您還需要將handler-chain.xml
添加到您的類路徑中:
examples.webservices.handler.Handler1 examples.webservices.handler.Handler2
有關完整的指南,請參見Oracle的SOAPHandlers
創建SOAPHandlers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.