Hi,
i am using spring security 3.1.1 with hibernate-entitymanager 3.6.0.
What i want is my own UserDetailsService within my application so i can use my own user class.
For this i implemented the UserDetailsService interface which looks like this:
But everytime i try to login using the default spring security login form the entityManager is null.Code:import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("userRepositoryImpl") public class UserRepositoryImpl implements UserDetailsService { @PersistenceContext private EntityManager entityManager; @Override @Transactional(readOnly = true) public User loadUserByUsername(String username) throws UsernameNotFoundException { User user = entityManager.find(User.class, username); if (user == null) throw new UsernameNotFoundException("Username not found: " + username); return user; } }
The same happens if i try to autowire my userDao, it is null in the context of the UserDetailsService.
However, using the entityManager in my generic dao to retrieve and save data to database it works fine:
This is my security.xml:Code:@Transactional public abstract class DaoImpl<T> implements Dao<T> { private Class<T> type; @PersistenceContext protected EntityManager em; public DaoImpl() { Type t = getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) t; type = (Class) pt.getActualTypeArguments()[0]; } ... }
and this is my db.xml including the entityManagerFactory definition:Code:<http use-expressions="true" auto-config="true"> <intercept-url pattern="/task/" access="permitAll" /> <intercept-url pattern="/task/**" access="isAuthenticated()" /> <!-- <intercept-url pattern="/**" access="denyAll" /> --> <form-login /> </http> <beans:bean id="userRepositoryImpl" class="de.sveri.jeiwomisa.model.UserRepositoryImpl" autowire="byType"> </beans:bean> <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder"> </beans:bean> <authentication-manager> <authentication-provider user-service-ref="userRepositoryImpl"> <password-encoder ref="passwordEncoder" /> </authentication-provider> </authentication-manager>
As i am fairly new to the spring framework i think that i am missing something fundamentally here, maybe someone can pinpoint me into the right direction?Code:<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:db.properties" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true" /> <property name="generateDdl" value="true" /> <property name="databasePlatform" value="${db.dialect}" /> </bean> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" /> </beans>
Regards,
Sven


Reply With Quote
