如何为grails cxf web服务添加安全性(用户名,密码)

时间:2014-11-06 13:22:57

标签: web-services security grails cxf

我创建了两个grails项目,一个用于服务器端cxf web服务,另一个用于cxf客户端用于调用Web服务..

一切正常。

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

现在我想添加安全性,然后对服务器和客户端grails代码的更改是什么?

我尝试用Christian Oestreich在他的帖子中所说的安全性。

http://www.christianoestreich.com/2012/04/grails-cxf-interceptor-injection/ (Grails Cxf Interceptor Injection 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

1 个答案:

答案 0 :(得分: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);