Grails + CXF + secureServiceFactory

时间:2013-08-25 20:49:49

标签: grails cxf ws-security

当我尝试运行此脚本以保护我在Grails / CXF客户端上的Web服务时,我得到了

“无法在nullService上调用方法getInInterceptors()”on secureServiceFactory

是否需要在其他地方设置 secureServiceFactory

任何想法:

代码:

class BootStrap {

def secureServiceFactory

def init = { servletContext ->

    Map<String, Object> inProps = [:]
    inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
    Map<QName, Validator> validatorMap = new HashMap<QName, Validator>();
    validatorMap.put(WSSecurityEngine.USERNAME_TOKEN, new UsernameTokenValidator() {

        @Override
        protected void verifyPlaintextPassword(org.apache.ws.security.message.token.UsernameToken usernameToken, org.apache.ws.security.handler.RequestData data)
            throws org.apache.ws.security.WSSecurityException {
            if(data.username == "wsuser" && usernameToken.password == "secret") {
      println "username and password are correct!"
  } else {
      println "username and password are NOT correct..."
                throw new WSSecurityException("user and/or password mismatch")
            }
        }
    });
    inProps.put(WSS4JInInterceptor.VALIDATOR_MAP, validatorMap);
    secureServiceFactory.getInInterceptors().add(new WSS4JInInterceptor(inProps))
}

1 个答案:

答案 0 :(得分:0)

不确定这是一个完整的答案,但是,我收到了同样的错误,我知道cxf插件的目的是连接与您公开的服务名称相匹配的服务工厂。我已经验证了开箱即用,使用grails run-app运行grails-cxf插件应用程序正常运行。但是,通过在项目上执行grails war创建一个战争,当部署到tc服务器[vfabric-tc-server-developer-2.9.4.RELEASE] tomcat 7 [tomcat-7.0.47.A.RELEASE]时,发生此错误。

注意开箱即用,如插件作者在其他参考文献[http://www.christianoestreich.com/2012/04/grails-cxf-interceptor-injection/]中所述,除非您将test('org.apache.ws.security:wss4j:1.6.7')更改为compile('org.apache.ws.security:wss4j:1.6.7'),否则生成的战争将无效我注意到我无法完成这项工作,我不得不使用compile('org.apache.ws.security:wss4j:1.6.9')

不幸的是,在超越这个之后,我在部署grails run-app中没有发生的战争时遇到了第三个错误:

22-Aug-2014 11:46:05.062 SEVERE [tomcat-http--1] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet CxfServlet
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cxf' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:273)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:979)
    at org.apache.cxf.transport.servlet.CXFServlet.loadBus(CXFServlet.java:75)

我会继续关注它,但也许这场战争并不是为了真正部署,而是更多意味着只是为了开发插件本身。但是,如果是这样的话,在TC工作仍然会更好,因为我们可以放心地利用我们自己项目中的代码。