Announcement Announcement Module
Collapse
No announcement yet.
Spring Security and EntityManager Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Security and EntityManager

    Good Afternoon Folks.

    I've a lil problem when trying to login using Spring Security and EntityManager.

    Well, the trouble is, my EntityManager can't make a connection and execute SQL in the database.

    ps: I've spelled a big explanation below plz read it...

    my config is like that below:

    applicationContext-security:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/security"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    		http://www.springframework.org/schema/security 
    		http://www.springframework.org/schema/security/spring-security-3.1.xsd
    		http://www.springframework.org/schema/context 
    		http://www.springframework.org/schema/context/spring-context-3.1.xsd">		
    
    	<http auto-config="true" use-expressions="true">
    		<intercept-url pattern="/*"					access="permitAll()" />
    		<intercept-url pattern="/login*"			access="permitAll()" />
    		<intercept-url pattern="/css/**"			access="permitAll()" />
    		<intercept-url pattern="/images/**"			access="permitAll()" />
    		<intercept-url pattern="/javascript/**"		access="permitAll()" />
    		<intercept-url pattern="/js/*"				access="permitAll()" />
    		<intercept-url pattern="/logout"			access="permitAll()" />
    		<intercept-url pattern="/diretor/*"			access="hasAnyRole('Diretor')" />
    		<intercept-url pattern="/aluno/*"			access="hasAnyRole('Aluno')" />
    		<intercept-url pattern="/professor/*"		access="hasAnyRole('Professor')" />
    
    		<form-login login-page="/" 
    			login-processing-url="/j_login" 
    			default-target-url="/" 
    			authentication-success-handler-ref="loginSuccessHandler"
    			authentication-failure-handler-ref="loginFailureHandler"/>
    
    		<logout invalidate-session="true" 
    			logout-success-url="/"
    			logout-url="/logout" />
    
    		<session-management session-fixation-protection="newSession">
    			<concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
    		</session-management>
    	</http>
    	
    	<context:annotation-config/>
    
    	<authentication-manager>
    		<authentication-provider user-service-ref="usuarioService">
    			<password-encoder hash="md5" />
    		</authentication-provider>
    	</authentication-manager>
    
    	<beans:bean id="loginSuccessHandler" class="br.com.academico.security.LoginSuccessHandler" />
    	<beans:bean id="loginFailureHandler" class="br.com.academico.security.LoginFailureHandler" />
    </beans:beans>
    my service that searchs the UserByUsername

    UsuarioService.java
    Code:
    @Component
    @Transactional
    public class UsuarioService implements UserDetailsService{
    
    	@Autowired
    	private UsuarioPersistence service;
    		
    	public UserDetails loadUserByUsername(String username)
    			throws UsernameNotFoundException {
    		
    		Usuario user = service.loadByUsername(username);
    	    
    	    if(user == null) {
    	      throw new UsernameNotFoundException("Usuario nao existe ou senha incorreta.");
    	    }
    	    
    	    user.setUserRoles(service.loadRoles(user));
    	    return user;
    	}
    
    }
    so when it tries to load it from the database EntityManager can't find a connection (i don't know if it is true) but my console is just like that:

    16:57:13,804 DEBUG [JpaTransactionManager] Creating new transaction with name [br.com.academico.security.UsuarioService.loadUserB yUsername]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    16:57:13,804 DEBUG [JpaTransactionManager] Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@1963c81] for JPA transaction
    16:57:13,805 DEBUG [JpaTransactionManager] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDia lect$HibernateConnectionHandle@191e365]
    16:57:13,805 DEBUG [AnnotationTransactionAttributeSource] Adding transactional method 'carregarPrimeira' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    16:57:13,805 DEBUG [AnnotationTransactionAttributeSource] Adding transactional method 'carregarPrimeira' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    16:57:13,805 DEBUG [DefaultListableBeanFactory] Returning cached instance of singleton bean 'transactionManager'
    16:57:13,805 DEBUG [JpaTransactionManager] Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@1963c81] for JPA transaction
    16:57:13,805 DEBUG [JpaTransactionManager] Participating in existing transaction
    16:57:13,805 DEBUG [EntityManagerFactoryUtils] Opening JPA EntityManager
    16:57:13,807 DEBUG [EntityManagerFactoryUtils] Registering transaction synchronization for JPA EntityManager

    and then it STOPS in the last line.. and the Login Failures...

    thanks to everyone

  • #2
    plz somebody?

    Comment


    • #3
      Your post doesn't really give that many details. What do you mean it stops? Does the server crash? How does log in fail? Did you enable debug logging for Spring Security? What are the logs for it? Did you test your code outside of spring security to ensure it works? For example can you use your UsuarioService within a Filter that is not Spring Security related?

      Comment

      Working...
      X