Announcement Announcement Module
Collapse
No announcement yet.
No WebContext for Security filter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No WebContext for Security filter

    Hi, I am stucked with implementing spring security to my webproject.
    I created project using spring 3.0.5 without ContextLoaderListener.
    Everything was working fine, but then i add Spring security filter and i get error

    Code:
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:159)
    	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    	at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:350)
    	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
    	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
    	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    	at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    	at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    	at org.mortbay.jetty.Server.handle(Server.java:326)
    	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    	at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
    	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
    web.xml looks like this:

    Code:
    <!-- Handles all requests into the application -->
    	<servlet>
    		<servlet-name>EliteCafeServer</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>
    				/WEB-INF/spring/app-config.xml
    				/WEB-INF/spring/security-context.xml
    			</param-value>
    		</init-param>
    		<!-- Creates the Spring Container -->   
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>EliteCafeServer</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	
    	<filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>
                org.springframework.web.filter.DelegatingFilterProxy
            </filter-class>
        </filter>
    
        <filter>
            <filter-name>encoding-filter</filter-name>
            <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
            </filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>encoding-filter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
        <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    Any suggestions? I already tried to add Listener, but then I get error of multiple Contexts

  • #2
    Spring security needs to be loaded in the root context (at least for the filtering etc). So you really need to ContextLoaderListener.

    Also make sure that you load the security config only once (not both the servlet and contextloaderlistener must load this configuration). In general the ContextLoaderListener should load the general components (infrastructure, services, daos'etc.) and the servlet only the specifics (views, viewresolvers, handlers etc.).

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Spring security needs to be loaded in the root context (at least for the filtering etc). So you really need to ContextLoaderListener.

      Also make sure that you load the security config only once (not both the servlet and contextloaderlistener must load this configuration). In general the ContextLoaderListener should load the general components (infrastructure, services, daos'etc.) and the servlet only the specifics (views, viewresolvers, handlers etc.).
      So for me it means that I have to:
      1) Add ContextLoaderListener into web.xml
      2) Make plain DispatcherServlet entry in web.xml
      3) Define Dispatcher-Servlet.xml configuration file with servlet specifics
      4) Rest of the configurations leave in applicationContext

      right?

      Comment


      • #4
        Basically that are the steps to be taken.

        Comment


        • #5
          My web.xml looks like this now
          Code:
          <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/javaee"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd">
          
          
          	<listener>
          		<listener-class>org.springframework.web.context.ContextLoaderListener
          		</listener-class>
          	</listener> 
          
          	<listener>
          		<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher
          		</listener-class>
          	</listener>
          
          
          	<servlet>
          		<servlet-name>server</servlet-name>
          		<servlet-class>org.springframework.web.servlet.DispatcherServlet
          		</servlet-class>
          		<load-on-startup>2</load-on-startup>
          	</servlet>
          	<servlet-mapping>
          		<servlet-name>server</servlet-name>
          		<url-pattern>/</url-pattern>
          	</servlet-mapping>
           
          
          
          </web-app>
          but now I am getting this error:

          Code:
          java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
          I am really lost now.

          Comment


          • #6
            Then there is something else also registering a root ApplicationContext. Do you have some beans which put the loaded context into the ServletContext? There must be something else loading it...

            Comment


            • #7
              I tottaly stripped application configuration to the minimal core and error still persists.
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              
              <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
              	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
              	
              	
              	<context-param>
              	<param-name>contextConfigLocation</param-name>
              	<param-value>/WEB-INF/spring/app-config.xml</param-value>
              	</context-param>	
              
              	 <listener> 
              	 	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
              	</listener>  
              	
              	<servlet>
              		<servlet-name>EliteCafeServer</servlet-name>
              		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              		<load-on-startup>2</load-on-startup>
              	</servlet>
              	<servlet-mapping>
              		<servlet-name>EliteCafeServer</servlet-name>
              		<url-pattern>/</url-pattern>
              	</servlet-mapping>
              	
              	
              	
              </web-app>

              Code:
              <bean id="dataSource"
              		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              		<property name="driverClassName">
              			<value>org.hsqldb.jdbcDriver</value>
              		</property>
              		<property name="url">
              			<value>jdbc:hsqldb:file:./db</value>
              		</property>
              		<property name="username">
              			<value>sa</value>
              		</property>
              		<property name="password">
              			<value></value>
              		</property>
              	</bean>
              
              	<bean id="sessionFactory"
              		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              		<property name="dataSource" ref="dataSource" />
              </bean>
              	 
              	 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
              		<property name="sessionFactory" ref="sessionFactory" />
              	</bean>
              	
               	<bean id="genericDao" class="cz.elitechoice.elitecafeserver.data.dao.GenericDaoImpl">
               	<property name="hibernateTemplate" ref="hibernateTemplate" />
               	</bean>
               	
              	<bean id="appBean" class="cz.elitechoice.elitecafeserver.commons.ApplicationBean" />
               	
              	
               	
               	<bean id="clientService"
               	class="cz.elitechoice.elitecafeserver.clientservice.ClientServiceImpl" /> 
              
              
              </beans>

              Comment


              • #8
                Solved double web.xml declaration in pom.xml

                Comment


                • #9
                  Can you post the entire stack trace? Just to make sure...Is that your entire web.xml that you are using? You might try enabling logging so you can see anything else that is loading the ApplicationContext.

                  Comment


                  • #10
                    I figured it out, it was maven configuration error, not spring.
                    Thanky you for trying to help

                    Comment


                    • #11
                      I've run into the same problem described above. Shouldn't the DispatchServlet register the WebApplicationContext.ROOT attribute with the ServletContext? It seems like a bug to me that DispatchServlet doesn't register this attribute especially if Spring Security DelegatingFilterProxy requires it.

                      Comment


                      • #12
                        No it shouldn't... The ROOT context (as also explained by the reference guide) is loaded by the ContextLoaderListener and NOT the DispatcherServlet. Imagine you have multiple DispatcherServlets, which servlet is the root context (trust me this isn't far fetched it is actually a situation I worked with next to some MessageDispatcherServlets).

                        Comment


                        • #13
                          Thanks. I followed the steps you laid out for Aahzmandius and it worked. It was my mistake using DispatchServlet to load the full application. Correcting this also solved some other issues we had with multiple application contexts...

                          For other readers, steps followed:
                          1. Move application beans and config to service-context.xml
                          2. Setup ContextLoaderListener, in web.xml, with service-context.xml (the WebApplicationContext.ROOT)
                          3. Left Spring Web MVC config, including view config, in servlet-config.xml
                          4. Setup DispatchServlet, in web.xml, with servlet-config.xml

                          Comment

                          Working...
                          X