Announcement Announcement Module
Collapse
No announcement yet.
Application Context and Bean Loading Issues Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Application Context and Bean Loading Issues

    Hi Guys,

    I'm trying to set up spring security in my flex app and am having a bit of a headache trying to load my beans in the container. My web.xml looks like this

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    
        <display-name>Custom App</display-name>
        <description>Custom CRM Application</description>
    
             <context-param>
    	   <param-name>contextConfigLocation</param-name>
    	   <param-value>/WEB-INF/securityContext.xml</param-value>
    	 </context-param>
    
    	<filter>
    		<filter-name>springSecurityFilterChain</filter-name>
    		<filter-class>
    			org.springframework.web.filter.DelegatingFilterProxy
    		</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>springSecurityFilterChain</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	
    
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    
        <!-- Http Flex Session attribute and binding listener support -->
        <listener>
            <listener-class>flex.messaging.HttpFlexSession</listener-class>
        </listener>
    
        <servlet>
    	    <servlet-name>MessageBrokerServlet</servlet-name>
    	    <display-name>MessageBrokerServlet</display-name>
    	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	    <init-param>
    	        <param-name>contextConfigLocation</param-name>
    	        <param-value>
    	        	/WEB-INF/web-application-config.xml
    	        </param-value>
    	    </init-param>
    	    <load-on-startup>1</load-on-startup>
    	</servlet>
    
    
        <servlet-mapping>
            <servlet-name>MessageBrokerServlet</servlet-name>
            <url-pattern>/messagebroker/*</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
        </welcome-file-list>
    
    
    
    </web-app>

    I have two config files; web-application-config.xml and securityContext.xml. My web-application-config.xml looks like this;



    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:flex="http://www.springframework.org/schema/flex"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:security="http://www.springframework.org/schema/security"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/flex 
               http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
               http://www.springframework.org/schema/security
    		   http://www.springframework.org/schema/security/spring-security-3.0.xsd">
    
    	<security:global-method-security secured-annotations="enabled" jsr250-annotations="enabled"/>
    
    	<flex:message-broker>
    		<flex:secured/>
    	</flex:message-broker>
    	
    	<context:component-scan base-package="org.uk.pastiche.pasticheCRM"/>
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    		<property name="url" value="jdbc:mysql://localhost/pasticheCRMDB"/>
    		<property name="username" value="root"/>
    		<property name="password" value="root"/>
    	</bean>
    	
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="packagesToScan" value="org.uk.pastiche.pasticheCRM.domain" />
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    	
    	<tx:annotation-driven />
    	
    </beans>
    and my securityContext.xml looks like this;

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:flex="http://www.springframework.org/schema/flex"
           xmlns:security="http://www.springframework.org/schema/security"
           xsi:schemaLocation="
    	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    	   http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd
    	   http://www.springframework.org/schema/security
    	   http://www.springframework.org/schema/security/spring-security-3.0.xsd">
     
    
    	<bean id="customUserDetailsService" class="org.uk.pastiche.pasticheCRM.auth.CustomUserDetailsService">
    	</bean>
     		
    	<security:http entry-point-ref="preAuthenticatedEntryPoint">
    		<security:anonymous enabled="false"/>
    	</security:http>
    	
    	<bean id="preAuthenticatedEntryPoint" class="org.springframework.flex.security3.FlexAuthenticationEntryPoint"/>
    
    	<security:authentication-manager>
    		<security:authentication-provider user-service-ref="customUserDetailsService">
    		</security:authentication-provider>
    	</security:authentication-manager>
    	
    </beans>
    Basically my problem is that I am trying to implement a custom user service and at first I tried to annotate CustomUserDetailsService, however the spring container couldn't find it. So i specified the bean in xml as shown above. CustomUserDetailsService uses other beans that are autowired into it, specifically a UserDAO object. This class is annotated with the @Repository tag. However again, the spring container cannot find UserDAO. Other beans are able to find this object no problem, but for some reason the CustomUserDetailsService cannot!?

    Why is this? Does it have something to do with the way I have loaded the different config files?

    I tried to incorporate the security bean configuration into a single config file (web-application-config.xml), but the container threw the following error;

    Code:
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    Im at a deadend. Any help would be much appreciated.

    Cheers

  • #2
    I suggest a read of the web chapter especially about the relation between the different application contexts.

    In short the DispatcherServlet can see the beans defined in the root context (ContextLoaderListener) but the ContextLoaderListener cannot see beans in child contexts (The DispatcherServlet). In general it i a best practice to put your application beans (datasources, services, daos etc.) in the root context and only the web specific stuff (controllers, views etc.) in the DispatcherServlet.

    So I suggest moving the component-scan, tx:annotation-driven, datasource, session factory and global security stuff to the root context. 1 warning when moving the component scan make sure that the ContextLoaderListener is scanning for everything BUT your web stuff (I'm not certain how that relates to the flex stuff) and you also need a component-scan in your child context to scan ONLY for your web stuff.

    Comment


    • #3
      Hi Martin,

      Thanks for the reply. I got it working in the end by removing the following from the web.xml file;

      Code:
               <context-param>
      	   <param-name>contextConfigLocation</param-name>
      	   <param-value>/WEB-INF/securityContext.xml</param-value>
      	 </context-param>
      
      	<filter>
      		<filter-name>springSecurityFilterChain</filter-name>
      		<filter-class>
      			org.springframework.web.filter.DelegatingFilterProxy
      		</filter-class>
      	</filter>
      	<filter-mapping>
      		<filter-name>springSecurityFilterChain</filter-name>
      		<url-pattern>/*</url-pattern>
      	</filter-mapping>
      	
      
      	<listener>
      		<listener-class>
      			org.springframework.web.context.ContextLoaderListener
      		</listener-class>
      	</listener>
      and defining all the security configuration in the DisptacherServlet context. I guess given what you've said, I don't have a root context only a DispatcherServlet context. I'm guessing this isn't good practice?

      I've been trying to look for specific information on this in the spring documentation but all i could find were some references in the following link;

      http://static.springsource.org/sprin.../html/mvc.html

      am i looking in the right place?

      Comment


      • #4
        Hii Aegis,

        I had just recently discussion with Marten with the root ApplicationContext and Dispacher Servlet. Marten must remember it .

        Back to Aegis, You having a DispacherServlet and root ApplicationContext too. The xml files define using ContextConfigLocation are consider as root ApplicationContext and that files having application level components where as DispacherServlet having Controller, handlermapping, ViewResolver etc

        Comment

        Working...
        X