CXF 生成的 WSDL 不包含 WS-SecurityPolicy 定义
我想使用 WS-Security 来保护我的 Web 服务。我使用 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();
I would like to use WS-Security to protect my web service. I'm using CXF to expose my endpoint, and use WSDL generation from Java code (aka CXF code first service).
This tutorial explain how to use WS-Security with CXF when the WSDL is manually managed: http://www.ibm.com/developerworks/java/library/j-jws13/index.html
However, I'm using CXF to automatically generate the WSDL. The generated WSDL doesn't indicate that the client should use WS-Security. I would expect something similar to this in the 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>
I'm not using Spring, but I use an embedded Jetty. Here is how I wire everything:
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前不支持。
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)
It's not supported right now.
http://cxf.apache.org/docs/ws-securitypolicy.html
Someone explains the same problem here, and expose a solution with @Policy. However, the solution is boggy with CXF <=2.4.1 (the policy is added twice in the WSDL).
http://cxf.547215.n5.nabble.com/WS-Security-policy-in-wsdl-for-java-first-approach-td569052.html
The duplication problem is now fixed and will be release in 2.4.2 (see https://issues.apache.org/jira/browse/CXF-3668)