2014-11-06 73 views
0

我创建了两个Grails项目,一个用于服务器端CXF Web服务等为CXF客户端调用Web服务..如何添加安全性(用户名,密码)的Grails

一切正常CXF Web服务。

我可以从客户端代码调用Web服务并获得结果。

现在我想添加安全性,那么服务器和客户端grails代码会发生什么变化?

我试过应用安全性,如Christian Oestreich在他的帖子中所说的那样。

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 
得到错误

和客户端代码

回答

0

而不是上面提到的客户端代码,使用下面的代码来使它工作。

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