Announcement Announcement Module
Collapse
No announcement yet.
No Hibernate Session when I use Spring Security Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • No Hibernate Session when I use Spring Security

    Hi, when I use Spring Security with MVC and Hibernate I obtain: "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here".

    The authentication works well but when I send to a jsp page, It has some data from Hibernate I get the exception. The following my configuration:

    web.xml

    HTML Code:
    	<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>
    	
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    		/WEB-INF/spring/spring-security.xml
    		/WEB-INF/spring/applicationContext.xml
    		<!-- /WEB-INF/spring/servlet-context.xml  -->
    		</param-value>
    	</context-param>
    	
    	<!-- Processes application requests -->
    	<servlet>
    		<servlet-name>spring</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    		</init-param>
    		<load-on-startup>3</load-on-startup>
    	</servlet>
    	
    	<servlet-mapping>
    		<servlet-name>spring</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	
    </web-app>
    applicationContext.xml

    HTML Code:
    	
    	<!-- Activates various annotations to be detected in bean classes -->
    	<context:annotation-config />
    	
    	<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
    	 For example @Controller and @Service. Make sure to set the correct base-package-->
    	<context:component-scan base-package="it.obiectivo.ecoss" />
    	
    	<!-- Configures the annotation-driven Spring MVC Controller programming model.
    	Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
    	<mvc:annotation-driven /> 
    	
    	<!-- Load Hibernate related configuration -->
    	<import resource="hibernate-config/hibernate-context.xml" />
    	
    </beans>

    servlet-context.xml

    HTML Code:
    	<!-- Scans the classpath of this application for @Components to deploy as beans -->
    	<context:component-scan base-package="it.obiectivo.ecoss" />
    	
    	<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
    	 For example @Controller and @Service. Make sure to set the correct base-package-->
    	<!--  <context:component-scan base-package="org.springframework.ecoss" /> -->
    
    	<!-- Configures the @Controller programming model -->
    	<mvc:annotation-driven />
    
    	<!-- Forwards requests to the "/" resource to the "welcome" view -->
    	<mvc:view-controller path="/" view-name="welcome"/>
    
    	<!-- Configures Handler Interceptors -->	
    	<mvc:interceptors>
    		<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
    		<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    	</mvc:interceptors>
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    	<mvc:resources mapping="/resources/**" location="/resources/" />
    	
    	<!-- Load Hibernate related configuration --> 
    	<!-- <import resource="classpath:*hibernate-context.xml" />  -->
    
    	<!-- Saves a locale change using a cookie -->
    	<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
    
    	<!-- Application Message Bundle -->
    	<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<property name="basename" value="/WEB-INF/messages/messages" />
    		<property name="cacheSeconds" value="0" />
    	</bean>
    
    	<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/views/"/>
    		<property name="suffix" value=".jsp"/>
    	</bean>
    
    </beans>

    spring-security.xml

    HTML Code:
    	<!-- This is where we configure Spring-Security  -->
    	<security:http auto-config="true" use-expressions="true" access-denied-page="/auth/denied" >
    	
    		<security:intercept-url pattern="/auth/login" access="permitAll"/>
    		<security:intercept-url pattern="/utente/detail" access="hasRole('Utente')"/>
    		<!-- <security:intercept-url pattern="/utente/Detail" access="hasRole('ROLE_USER')"/> -->
    		
    		<security:form-login
    				login-page="/auth/login" 
    				authentication-failure-url="/auth/login?error=true" 
    				default-target-url="/utente/index"/>
    			
    		<security:logout 
    				invalidate-session="true" 
    				logout-success-url="/auth/login" 
    				logout-url="/auth/logout"/>
    	
    	</security:http>
    	
    	<!-- Declare an authentication-manager to use a custom userDetailsService -->
    	<security:authentication-manager>
    	        <security:authentication-provider user-service-ref="customUserDetailsService">
    	        		<!-- <security:password-encoder ref="passwordEncoder"/> -->
    	        </security:authentication-provider>
    	</security:authentication-manager>
    	
    	<!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
    	<bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>
    
    	<!-- A custom service where Spring will retrieve users and their corresponding access levels  -->
    	<bean id="customUserDetailsService" class="it.obiectivo.ecoss.service.CustomUserDetailsService"/>
    	
    </beans>

    ended my hibernate-context.xml

    HTML Code:
    	<context:property-placeholder location="/WEB-INF/spring/hibernate-config/spring.properties" />
        
        <!-- Enable annotation style of managing transactions -->
    	<tx:annotation-driven transaction-manager="transactionManager" />	
       
        <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
        <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html --> 							
    	<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
    	<!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    				 p:dataSource-ref="dataSource"
    				 p:configLocation="${hibernate.config}"
    				 p:packagesToScan="it.obiectivo.ecoss"/>
    	
    	<!-- Declare a datasource that has pooling capabilities-->	 
    	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    				destroy-method="close"
    				p:driverClass="${app.jdbc.driverClassName}"
    				p:jdbcUrl="${app.jdbc.url}"
    				p:user="${app.jdbc.username}"
    				p:password="${app.jdbc.password}"
    				p:acquireIncrement="5"
    				p:idleConnectionTestPeriod="60"
    				p:maxPoolSize="100"
    				p:maxStatements="50"
    				p:minPoolSize="10" />
    
    	<!-- Declare a transaction manager-->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
           			p:sessionFactory-ref="sessionFactory" />
      
    </beans>

    hibernate.cfg.xml

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    		
    <hibernate-configuration>
      <session-factory>
      	<!-- We're using MySQL database so the dialect needs to MySQL as well-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <!-- Enable this to see the SQL statements in the logs-->
        <property name="show_sql">false</property>
        <!-- This will drop our existing database and re-create a new one.
        		Existing data will be deleted! -->
        <property name="hbm2ddl.auto">update</property>
      </session-factory>
    </hibernate-configuration>

    and spring.propertis

    HTML Code:
    # database properties
    app.jdbc.driverClassName=com.mysql.jdbc.Driver
    app.jdbc.url=jdbc:mysql://localhost/ecoss
    app.jdbc.username=root
    app.jdbc.password=admin
    
    #hibernate properties
    hibernate.config=/WEB-INF/spring/hibernate-config/hibernate.cfg.xml


    I hope that sameone get help me! Bye!

  • #2
    Where's the MVC part that throws the error? Are you referring to a Controller or Service class?

    I'm only seeing configurations for MVC and Spring Security.

    Comment

    Working...
    X