2011-07-11 138 views
4

我有一个Spring bean的定义文件,如下配置Spring Security嵌入码头春季

<bean id="jettyZk" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop"> 
    <!-- properties, threadPool, connectors --> 

    <property name="handler"> 
     <bean class="org.eclipse.jetty.servlet.ServletContextHandler"> 
      <property name="eventListeners"> 
       <list> 
        <!-- my.ContextLoaderListener 
        * An ApplicationContextAware ContextLoaderListener that allows for using the current ApplicationContext, 
        * as determined by ApplicationContextAware, as the parent for the Root WebApplicationContext. 
        * 
        * Also provides for specifying the contextConfigLocation of the Root WebApplicationContext, because 
        * Eclipse Jetty 7 ServletContextHandler does not expose a setInitParameters method. 
        --> 
        <bean class="my.ContextLoaderListener"> 
         <property name="contextConfigLocation" value="/META-INF/spring/applicationContext-securityZk.xml"/> 
        </bean> 
        <!-- not sure if this is needed, disabled for now --> 
        <!-- <bean class="org.springframework.web.context.request.RequestContextListener"/> --> 
       </list> 
      </property> 
      <property name="servletHandler"> 
       <bean class="org.eclipse.jetty.servlet.ServletHandler"> 
        <property name="filters"> 
         <list> 
          <bean class="org.eclipse.jetty.servlet.FilterHolder"> 
           <property name="name" value="springSecurityFilterChain"/> 
           <property name="filter"> 
            <bean class="org.springframework.web.filter.DelegatingFilterProxy"/> 
           </property> 
          </bean> 
         </list> 
        </property> 
        <!-- filterMappings, servlets, servletMappings --> 
       </bean> 
      </property> 
     </bean> 
    </property> 
</bean> 

当试图启动的背景下,我得到下面的异常

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: ServletContext must not be null 

Caused by: java.lang.IllegalArgumentException: ServletContext must not be null 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:109) 

这是使用Spring 3.0.5.RELEASE

这个异常是可以理解的,查看DelegatingFilterProxy#findWebApplicationContext的代码和JavaDocs,它说

WebApplicationContext是必须已加载并存储在 ServletContext中之前该过滤器被初始化(或调用)。

,因为我试图创建过滤器作为上下文句柄(子)属性,因此它似乎是合理的,上下文还没有被初始化,因此既没有春节WAC。

我想知道的是如何在Spring本身正在装配的嵌入式Jetty容器中配置Spring Security?

似乎有一个catch 22场景只需要一个较晚的初始化,但我找不到一个合适的标志来旋转。我已经尝试将lazy-init="true"设置到过滤器bean上,但这似乎并没有实现太多,不出所料。

相关: How to embed Jetty into Spring and make it use the same AppContext it was embedded into?

+0

作为试图解决这个问题,我一直在看春季JavaConfig的结果,有一个相关的问题:http://stackoverflow.com/questions/6683771/spring-configuration-and-contextcomponent-扫描 – ptomli

+0

你有没有想过办法做到这一点?我基本上有同样的问题。 – tdimmig

+0

我结束了基于@Configuration的设置 – ptomli

回答

0

我正在码头6.1.4和3.0.5春天,但我这样做的老式方法,与WEB-INF/web.xml文件。

使用这种方法,它导致零问题。

public class JettyServer { 

    public static void main(String[] args) throws Exception { 
    Properties props = new Properties(); 
    props.load(new FileInputStream("./etc/server.properties")); 

    Server server = new Server(); 

    final String configFile = props.getProperty("server.config"); 

    XmlConfiguration configuration = 
     new XmlConfiguration(new File(configFile).toURI().toURL()); 
    configuration.configure(server); 
    HandlerCollection handlers = new HandlerCollection(); 

    WebAppContext webapp = new WebAppContext(); 
    webapp.setContextPath(props.getProperty("context.path")); 
    webapp.setDefaultsDescriptor(props.getProperty("default.config")); 

    webapp.setWar(props.getProperty("war.path")); 

    NCSARequestLog requestLog = new NCSARequestLog(props.getProperty("log.file")); 
    requestLog.setExtended(true); 
    RequestLogHandler requestLogHandler = new RequestLogHandler(); 
    requestLogHandler.setRequestLog(requestLog); 

    handlers.setHandlers(
     new Handler[] { webapp, new DefaultHandler(), requestLogHandler }); 

    server.setHandler(handlers); 

    server.setStopAtShutdown(true); 
    server.setSendServerVersion(true); 

    server.start(); 
    server.join(); 
} 
+0

你有Jetty提供一个WAR文件,这不是我正在做的。 *思考*我花了几个小时左右的时间调试它的全部调用方式,并且我没有在运行时提供'org.eclipse.jetty.webapp'包,所以无论我在哪里得到Spring,实例化过滤器,afterPropertiesSet在我的ServletContext被初始化之前被调用。我确实注意到我在错误的容器上设置了“过滤器”,所以我会更新问题中的示例,但我仍然卡住。虽然谢谢:) – ptomli