我创建了两个grails项目,一个用于服务器端cxf web服务,另一个用于调用web服务的cxf客户端。。
一切都很好。
我可以从客户端代码调用web服务并得到结果。
现在我想添加安全性,那么服务器和客户端grails代码会有什么变化?
正如克里斯蒂安·奥斯特里奇在他的帖子中所说,我试着申请安全。
http://www.christianoestreich.com/2012/04/grails-cxf-interceptor-injection/(Grails Cxf拦截器注入2.4.x-2.5.x)
应用安全性的客户端代码如下
ExampleService exampleService = new ExampleService()
def port = exampleService.exampleServicePort
Map ctx = ((BindingProvider)port).getRequestContext();
ctx.put("ws-security.username", "pankaj");
ctx.put("ws-security.password", "pankaj");
println ".......... " + port.sayHello("pankaj")
但我得到的错误如下
Error |
2014-11-06 18:33:15,411 [http-bio-8088-exec-4] ERROR errors.GrailsExceptionResolver - SoapFault occurred when processing request: [GET] /WSDLDemoClient/wsdldemo/index
An error was discovered processing the <wsse:Security> header.. Stacktrace follows:
Message: An error was discovered processing the <wsse:Security> header.
Line | Method
->> 75 | unmarshalFault in org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor
使用以下代码使其工作,而不是上面提到的客户端代码。
Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);
Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("pankaj"));
headers.put("Password", Collections.singletonList("pankaj"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);