Boa tarde galera…
tou passando por um problema bem estranho.
estou configurando o meu projeto com spring security…
só q quando eu peço para ele fazer Login, ele não consegue pesquisar no banco, não consegue registrar a EntityManager…
fica parado nesta linha no Console
14:07:04,217 DEBUG [EntityManagerFactoryUtils] Registering transaction synchronization for JPA EntityManager
não sei mais o q fazer…
depois ele não consegue pesquisar e me retorna isso no console
14:07:11,769 DEBUG [EntityManagerFactoryUtils] Closing JPA EntityManager
14:07:11,770 DEBUG [JpaTransactionManager] Initiating transaction rollback
14:07:11,770 DEBUG [JpaTransactionManager] Rolling back JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@8ee5c0]
14:07:11,770 DEBUG [JpaTransactionManager] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@8ee5c0] after transaction
14:07:11,770 DEBUG [EntityManagerFactoryUtils] Closing JPA EntityManager
14:07:11,771 DEBUG [DefaultListableBeanFactory] Returning cached instance of singleton bean ‘org.springframework.security.core.session.SessionRegistryImpl#0’
14:07:11,771 DEBUG [DefaultListableBeanFactory] Returning cached instance of singleton bean ‘stereotypedBeansRegistrar’
14:07:11,771 DEBUG [UsernamePasswordAuthenticationFilter] Authentication request failed: org.springframework.security.authentication.AuthenticationServiceException: Cannot open connection
14:07:11,771 DEBUG [UsernamePasswordAuthenticationFilter] Updated SecurityContextHolder to contain null Authentication
14:07:11,771 DEBUG [UsernamePasswordAuthenticationFilter] Delegating to authentication failure handlerbr.com.academico.security.LoginFailureHandler@c8c6d9
14:07:11,771 DEBUG [HttpSessionSecurityContextRepository] SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
14:07:11,771 DEBUG [SecurityContextPersistenceFilter] SecurityContextHolder now cleared, as request processing completed
14:07:11,773 DEBUG [FilterChainProxy ] /index.html at position 1 of 11 in additional filter chain; firing Filter: ‘ConcurrentSessionFilter’
14:07:11,773 DEBUG [FilterChainProxy ] /index.html at position 2 of 11 in additional filter chain; firing Filter: ‘SecurityContextPersistenceFilter’
14:07:11,773 DEBUG [HttpSessionSecurityContextRepository] HttpSession returned null object for SPRING_SECURITY_CONTEXT
14:07:11,773 DEBUG [HttpSessionSecurityContextRepository] No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@567117. A new one will be created.
14:07:11,773 DEBUG [FilterChainProxy ] /index.html at position 3 of 11 in additional filter chain; firing Filter: ‘LogoutFilter’
14:07:11,773 DEBUG [FilterChainProxy ] /index.html at position 4 of 11 in additional filter chain; firing Filter: ‘UsernamePasswordAuthenticationFilter’
14:07:11,773 DEBUG [FilterChainProxy ] /index.html at position 5 of 11 in additional filter chain; firing Filter: ‘BasicAuthenticationFilter’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 6 of 11 in additional filter chain; firing Filter: ‘RequestCacheAwareFilter’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 7 of 11 in additional filter chain; firing Filter: ‘SecurityContextHolderAwareRequestFilter’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 8 of 11 in additional filter chain; firing Filter: ‘AnonymousAuthenticationFilter’
14:07:11,774 DEBUG [AnonymousAuthenticationFilter] Populated SecurityContextHolder with anonymous token: ‘org.springframework.security.authentication.AnonymousAuthenticationToken@9056f12c: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@380f4: RemoteIpAddress: 127.0.0.1; SessionId: 93AB56189FEF8AD2211880907918255A; Granted Authorities: ROLE_ANONYMOUS’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 9 of 11 in additional filter chain; firing Filter: ‘SessionManagementFilter’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 10 of 11 in additional filter chain; firing Filter: ‘ExceptionTranslationFilter’
14:07:11,774 DEBUG [FilterChainProxy ] /index.html at position 11 of 11 in additional filter chain; firing Filter: ‘FilterSecurityInterceptor’
14:07:11,774 DEBUG [AntPathRequestMatcher] Checking match of request : ‘/index.html’; against ‘/*’
segue abaixo a minha config…
SpringSecurity
<?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: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 auto-config="true" use-expressions="true" create-session="always"
entry-point-ref="webServiceEntryPoint">
<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>
<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:bean id="webServiceEntryPoint" class="br.com.academico.security.WebServiceAuthenticationEntryPoint"/>
</beans:beans>
UsuarioService.java
package br.com.academico.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import br.com.academico.modelo.Usuario;
import br.com.academico.persistencia.UsuarioPersistence;
@Component
@Transactional
public class UsuarioService implements UserDetailsService{
private UsuarioPersistence service;
@Autowired
public UsuarioService(UsuarioPersistence service){
this.service = service;
}
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Usuario user = service.loadUserByUsername(username);
if(user == null) {
throw new UsernameNotFoundException("Usuario nao existe ou senha incorreta.");
}
user.setUserRoles(service.loadRoles(user));
return user;
}
UsuarioDao.java
package br.com.academico.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import br.com.academico.modelo.Aluno;
import br.com.academico.modelo.Professor;
import br.com.academico.modelo.UserRoles;
import br.com.academico.modelo.Usuario;
import br.com.academico.persistencia.UsuarioPersistence;
@Transactional
@Component
public class UsuarioDao implements UsuarioPersistence{
@PersistenceContext
private EntityManager entityManager;
private Session sessao(){
return ((Session)entityManager.getDelegate());
}
public Usuario loadUserByUsername(String username){
Usuario usuario = (Usuario)sessao().createCriteria(Usuario.class)
.add(Restrictions.eq("username", username))
.uniqueResult();
return usuario;
}
@SuppressWarnings("unchecked")
public List<UserRoles> loadRoles(Usuario user) {
List<UserRoles> lista = sessao().createCriteria(UserRoles.class).add(Restrictions.eq("usuario.username", user.getUsername())).list();
return lista;
}
}
Detalhe: se eu pesquisar fora da requisição de login ele pesquisa tudo normal.