【问题标题】:SOAP-WS security header authenticationSOAP-WS 安全头认证
【发布时间】:2015-10-02 10:41:04
【问题描述】:

我使用 spring+ XSD+ Payload 开发了一个 web 服务。我需要使用 SOAP 请求标头中的用户名和密码对请求标头进行身份验证,这是我使用 SOAPUI 实现的

我可以在请求中生成以下标头

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

现在我想验证用户名和密码的标题元素。

例如:

案例 1: userName=User and Password=test //认证通过并给出响应Success

案例 2: userName=User1 and Password=test1 //认证失败并给出响应Failure

请帮我提供合适的样品来达到同样的效果。

【问题讨论】:

    标签: java soap soapui ws-security soapheader


    【解决方案1】:

    SOAP web 服务中的处理程序(类似于拦截器/过滤器)可用于服务器端的身份验证目的,然后进一步链接请求。 请查看 SOAPHandler 以解析来自有效负载的标头信息并验证用户名/密码。 SOAP Handler at Server Side

    【讨论】:

      【解决方案2】:

      以下是执行此操作的一些步骤:

      1. 通过编写自定义handleMessage 方法来实现SOAPHandler 类。
      2. 在handleMessage 方法中,评估上下文的MESSAGE_OUTBOUND_PROPERTY。如果它是假的(意味着它是一个入站消息),那么编写内省context.getMessage() 的代码。在那里,您可以评估 MIME 标头、安全标头和令牌以及正文,以确定您是否需要拒绝身份验证凭据。如果这样做,请在方法结束时返回 false。
      3. 将您创建的SoapHander 添加到服务的处理程序链中。

      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 添加到您的类路径中:

      示例.webservices.handler.Handler1 示例.webservices.handler.Handler2

      如需完整指南,请参阅Oracle's guide to creating SOAPHandlers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-28
        • 1970-01-01
        • 2016-04-13
        • 2020-09-24
        • 2011-03-01
        • 2011-01-20
        • 1970-01-01
        • 2023-03-25
        相关资源
        最近更新 更多