Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: No WebContext for Security filter

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Quote 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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Basically that are the steps to be taken.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7

    Default

    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>

  8. #8

    Default

    Solved double web.xml declaration in pom.xml

  9. #9
    Join Date
    Jan 2008
    Posts
    1,826

    Default

    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.
    Rob Winch - @rob_winch
    Spring Security Lead
    Pivotal

  10. #10

    Default

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •