java - CXF 生成的 WSDL 不包含 WS-SecurityPolicy 定义

标签 java soap jax-ws cxf ws-security

我想使用 WS-Security 来保护我的网络服务。我正在使用 CXF 公开我的端点,并使用从 Java 代码生成的 WSDL(也称为 CXF 代码优先服务)。

本教程解释了在手动管理 WSDL 时如何将 WS-Security 与 CXF 结合使用:http://www.ibm.com/developerworks/java/library/j-jws13/index.html

但是,我使用 CXF 自动生成 WSDL。 生成的 WSDL 并不表示客户端应该使用 WS-Security。我希望 WSDL 中有类似的内容:

<wsp:Policy wsu:Id="UsernameToken" xmlns:wsu=
 "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
  xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
  xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702">
<wsp:ExactlyOne>
  <wsp:All>
    <sp:TransportBinding/>
    <sp:SupportingTokens>
      <wsp:Policy>
        <sp:UsernameToken sp:IncludeToken=".../IncludeToken/AlwaysToRecipient"/>
      </wsp:Policy>
    </sp:SupportingTokens>
  </wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>

我没有使用 Spring,但我使用的是嵌入式 Jetty。这是我连接所有内容的方式:

CXFNonSpringServlet cxfServlet = new CXFNonSpringServlet() {
  private static final long serialVersionUID = 1L;

  @Override
  protected void loadBus(ServletConfig sc) {
    super.loadBus(sc);

    Map<String, Object> inProps = new HashMap<String, Object>();
    inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
    inProps.put(WSHandlerConstants.PW_CALLBACK_REF, new TestCallback());

    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setBus(bus);
    factory.setServiceBean(new MyServiceEndpointImpl());
    factory.setAddress("/myservice");
    factory.getInInterceptors().add(new WSS4JInInterceptor(inProps));
    factory.create();
  }
};

Server server = new Server(8080);
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
ServletContextHandler rootContext = new ServletContextHandler(contexts, "/");
rootContext.addServlet(new ServletHolder(cxfServlet), "/soap/*");
server.start();

最佳答案

目前不支持。

Note: at this point, WS-SecurityPolicy support is ONLY available for "WSDL first" scenarios. The WS-SecurityPolicy fragments can only be pulled from WSDL. In the future, we plan to enable various code first scenarios as well, but at this time, only WSDL first is available.

http://cxf.apache.org/docs/ws-securitypolicy.html

有人在这里解释了同样的问题,并用@Policy公开了一个解决方案。但是,CXF <=2.4.1 的解决方案很麻烦(该策略在 WSDL 中添加了两次)。

http://cxf.547215.n5.nabble.com/WS-Security-policy-in-wsdl-for-java-first-approach-td569052.html

重复问题现已修复,将在 2.4.2 中发布(参见 https://issues.apache.org/jira/browse/CXF-3668)

关于java - CXF 生成的 WSDL 不包含 WS-SecurityPolicy 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6832849/

相关文章:

java - Java中ArrayList中每个组的最小值

java - 如何使用 arraylist 更改布局和类

javascript - 使用 AngularJS 从 SSRS 请求时出现 SOAP 故障错误

java - 模拟是单元测试的重要组成部分吗?

java - 如何在 vaadin 中设置网格单元格样式

java - Jaxb Unmarshall 找到预期标签并失败,因为它是意外的

multithreading - 无法销毁线程中的 THTTPReqResp 组件

java - 在 Axis2/Rampart 中,如何忽略接收方/响应方的 SignatureToken?

java - RMI 和 Web 服务

get - JAX-RS 2 和 Jersey 2 : how to pass an (hash)map on a GET request