Boa tarde, estou utilizando o spring securtity para autenticação do usuário, porém no meu site eu permito também que o usuário possa se cadastrar na plataforma. Assim, estou utilizando a anottation @Transactional do spring para inserir os dados.
Porém, meu código não funciona quando eu utilizo a anotação @Transactional, como faço então para permitir que um usuário possa se cadastrar na plataforma ?
UserDAO.java
package br.com.utfpr.eventos.dao;
import java.util.List;
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.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.utfpr.eventos.models.User;
@Repository
@Transactional
public class UserDAO implements UserDetailsService {
@PersistenceContext
private EntityManager manager;
public void insert(User user) {
manager.persist(user);
}
public User loadUserByUsername(String email) {
List<User> users = manager.createQuery("select u from user u where u.email = :email", User.class)
.setParameter("email", email)
.getResultList();
if(users.isEmpty()) {
throw new UsernameNotFoundException("Usuário não encontrado");
}
return users.get(0);
}
}
User.java
package br.com.utfpr.eventos.models;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
@Entity
public class User implements UserDetails {
private static final long serialVersionUID = 1L;
@Id
private String email;
private String name;
private String document;
private String password;
@OneToMany(fetch=FetchType.EAGER)
private List<Role> roles = new ArrayList<Role>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document = document;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles;
}
public String getUsername() {
return this.email;
}
public boolean isAccountNonExpired() {
return true;
}
public boolean isAccountNonLocked() {
return true;
}
public boolean isCredentialsNonExpired() {
return true;
}
public boolean isEnabled() {
return true;
}
}
Role.java
package br.com.utfpr.eventos.models;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.springframework.security.core.GrantedAuthority;
@Entity
public class Role implements GrantedAuthority {
private static final long serialVersionUID = 1L;
@Id
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthority() {
return this.name;
}
}
ServletSpringConfiguration.java
package br.com.utfpr.eventos.config;
import javax.servlet.Filter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {SecurityConfiguration.class, AppWebConfiguration.class, JPAConfiguration.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setForceEncoding(true);
encodingFilter.setEncoding("UTF-8");
return new Filter[] {encodingFilter};
}
}