Announcement Announcement Module
No announcement yet.
Hibernate problem with Spring security UserDetailsService Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate problem with Spring security UserDetailsService

    I'm trying to use Spring Security with a custom Hibernate UserDetailsService.

    Every time the user attempts to log in I get a "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" exception.

    I also use BlazeDS Integration but I don't think the problem is related to the Flex/BlazeDS part of the application.

    Here are my configuration files


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "">
    	<description>Flex Test</description>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    		<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="" xmlns:xsi="" xmlns:sec="" xmlns:context="" xmlns:p="" xmlns:tx="" xmlns:flex="" xsi:schemaLocation=" ">
    	<context:property-placeholder location="/WEB-INF/" />
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" 
    	p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/>
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
    	p:dataSource-ref="dataSource" p:mappingDirectoryLocations="classpath:com/flextest/domain/hibernate/hbm/">
    		<property name="hibernateProperties">
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.show_sql">true</prop>
    	<tx:annotation-driven transaction-manager="txManager"/>
    	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
    	<bean id="userRepository" class="com.flextest.domain.UserHome">
    		<property name="sessionFactory" ref="sessionFactory" />
    		<flex:secured per-client-authentication="true"/>
    	<bean id="userRegistrationService" class="">
    		<property name="userRepository" ref="userRepository" />
    		<flex:remoting-destination />
    			<sec:protect method="testMethod" access="ROLE_USER" />


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	xmlns:xsi="" xmlns:sec=""
    	<sec:http auto-config="true" session-fixation-protection="none"/>
    	<sec:authentication-provider user-service-ref="userRepository"/>

    The UserHome class implements UserDetailsService and related method

    import org.hibernate.SessionFactory;
    public class UserHome implements  UserDao, UserDetailsService{
    	private SessionFactory sessionFactory;
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	public UserDetails loadUserByUsername(String username)
    			throws UsernameNotFoundException, DataAccessException {
    		return findById(username);
    public User findById(java.lang.String id) {
    		try {
    			User instance = (User) sessionFactory.getCurrentSession().get("com.flextest.domain.User", id);
    			if (instance == null) {
    			} else {
    			return instance;
    		} catch (RuntimeException re) {
    			log.error("get failed", re);
    			throw re;
    Can anyone help me solve the problem?

    Thanks in advance

  • #2
    As an additional detail, if I don't use Spring security the exception remains but if I use transaction annotation on the service method (even @Transactional(propagation=Propagation.NEVER)) everything works fine.

    Adding such annotation while using Spring security doesn't solve the problem. Ithink there is a configuration broblem


    • #3
      Open Session in View

      By default there is no hibernate session bound to an HTTP request unless you make it happen.

      Luckily there's a handy interceptor provided by Spring that you can configure in your web.xml file, it will bind a new hibernate session to every HTTP request made to the path the filter is specified for. This is an easy way of achieving the 'Open Session in View' hibernate session pattern.

      For example:



      • #4
        I recently ran into the exact same sort of error, and beat my head against the wall trying to figure out what the problem was.

        In my case, I was using the `OpenSessionInViewFilter` configured in the `web.xml` file, along with the Spring Security configuration there as well.

        Since both Spring Security and `OpenSessionInViewFilter` are filters, the order in which they are applied depends on the order they appear in the `web.xml` file.

        In my case, I initially had the `OpenSessionInViewFilter` below the Spring Security filter. Once I swapped their positions (ie. put the `OpenSessionInViewFilter` above the Spring Security one), everything worked just fine!