gpt4 book ai didi

java - 使用 Spring WS 获取 SOAP header 的内容

转载 作者:行者123 更新时间:2023-12-02 08:42:04 26 4
gpt4 key购买 nike

我正在尝试构建一个将从客户端接收 SOAP 消息的端点。我收到的消息在 SOAP 头中包含用户名和密码...

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://www.company.com/Application">
<soapenv:Header xmlns:wsse="http://__________.xsd">
<wsse:Security >
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>

</soapenv:Header>
<soapenv:Body>

我正在使用 Spring WS - 明显的解决方案是在 web.xml 中创建一个过滤器,它将完全绕过 Spring WS,解析 SOAP 消息,提取用户名和密码,然后继续Spring WS 将再次解析 SOAP。

有没有办法在不绕过 Spring WS 的情况下获取 header 的内容?

我尝试在 sws:interceptors 中添加一个 bean:

<sws:interceptors>

<!-- extract Security details from Header -->
<bean class="com.company.application.service.SecurityInterceptorService" />

<!-- log full Body of request -->
<bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor"/>

<!-- validate Request against XSD to make sure it's a valid request -->
<bean id="CompanyApplication" class="com.company.application.interceptor.ValidatingInterceptor">
<property name="schema" value="/WEB-INF/_______________.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="true" />
</bean>

</sws:interceptors>

然后实现该类:

public class SecurityInterceptorService implements EndpointInterceptor {


@Override
public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {

System.out.println("---------------");
System.out.println("handleRequest") ;
System.out.println("---------------");


return true;
}

@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {

System.out.println("---------------");
System.out.println("handleResponse");
System.out.println("---------------");



return true;
}

@Override
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {

System.out.println("---------------");
System.out.println("handleFault");
System.out.println("---------------");


return true;
}

@Override
public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception {

System.out.println("---------------");
System.out.println("afterCompletion");
System.out.println("---------------");

}



}

endpoint 仅包含有关 handleRequest 内端点的数据,在 Debug模式下遍历 messageContext 内的许多层后,我可以似乎没有发现标题的内容。

我要查找的内容是否在 messageContext 中?如果是,我该如何访问它?

最佳答案

来自messageContext对象,您可以检索请求或响应(在您的情况下,我猜您需要该请求)。

请求/响应基本上是 WebServiceMessage 。如果您检查 webServiceMessage,您将看到该对象可以转换为 SoapMessage 。现在,您可以从soap消息中获取soap header 。

WebServiceMessage webServiceMessageRequest = messageContext_.getRequest();
SoapMessage soapMessage = (SoapMessage) webServiceMessageRequest;
SoapHeader soapHeader = soapMessage.getSoapHeader()

之后,您可能想要获取 source对象并将其转换为 DOMSource 对象,然后获取 Node对象,使信息检索变得更加容易。

Source bodySource = soapHeader .getSource();
DOMSource bodyDomSource = (DOMSource) bodySource;
Node bodyNode = _bodyDomSource.getNode();

关于java - 使用 Spring WS 获取 SOAP header 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35412063/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com