복붙노트

[SPRING] 봄에 임베디드 부두에서 스프링 보안 설정하기

SPRING

봄에 임베디드 부두에서 스프링 보안 설정하기

나는 아래와 같이 스프링 빈 정의 파일을 가지고있다.

<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를 살펴보면

컨텍스트 핸들러의 (하위) 속성으로 필터를 만들려고하기 때문에 컨텍스트가 아직 초기화되지 않았으므로 Spring WAC가 없습니다.

제가 알고 싶은 것은 Spring 자체가 조합되어있는 임베디드 Jetty 컨테이너에서 Spring Security를 ​​어떻게 구성 할 수 있는가하는 것입니다.

늦은 초기화가 필요한 캐치 22 시나리오가있는 것처럼 보이지만 회전 할 적절한 플래그를 찾을 수 없습니다. 나는 lazy-init = "true"를 필터 빈에 설정하려고 시도했지만,별로 놀랍지 않게 달성하지 못했다.

관련 항목 : Jetty를 Spring에 임베딩하고 그것이 포함 된 AppContext와 동일한 것을 사용하는 방법?

해결법

  1. ==============================

    1.Jetty 6.1.4와 Spring 3.0.5를 실행 중이지만 WEB-INF / web.xml을 사용하여 구식 방식으로 수행하고 있습니다.

    Jetty 6.1.4와 Spring 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();
    }
    
  2. from https://stackoverflow.com/questions/6653495/configure-spring-security-on-embedded-jetty-in-spring by cc-by-sa and MIT license