I'm working on a Java EE + Primefaces + JPA project with EJB. In the authentication part I decided to use Spring Security, but I'm having trouble getting the EJB inside the Spring controller. I found some tutorials, but I did not succeed. If someone there has done something similar and can give me a light I would be very grateful.
Spring Controller:
public class AppUserDetailsService implements UserDetailsService {
private UserEJBLocal repositorio;
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
Usuario usuario = repositorio.getUsuarioPorLogin(login);
UsuarioSistema user = null;
if(usuario != null) {
user = new UsuarioSistema(usuario, getGrupos(usuario));
}
return user;
}
private Collection<? extends GrantedAuthority> getGrupos(Usuario usuario) {
List<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
for(Grupo grupo : usuario.getGrupos()) {
authorities.add(new SimpleGrantedAuthority(grupo.getNome().toUpperCase()));
}
return authorities;
}
public UserEJBLocal getRepositorio() {
return repositorio;
}
public void setRepositorio(UserEJBLocal repositorio) {
this.repositorio = repositorio;
}
}
EJBs:
public interface UserEJBLocal {
public Usuario getUsuarioPorLogin(String login);
}
@Stateless(mappedName = "ejb/userFind")
public class UserEJB implements UserEJBLocal {
@PersistenceContext(unitName = "martanPU")
private EntityManager em;
@Override
public Usuario getUsuarioPorLogin(String login) {
Usuario usuario = null;
try {
usuario = getEntityManager().createQuery("SELECT user FROM Usuario AS user WHERE user.login = :login", Usuario.class)
.setParameter("login", login.toLowerCase()).getSingleResult();
} catch (NoResultException e) {
e.printStackTrace();
}
return usuario;
}
protected EntityManager getEntityManager() {
return em;
}
}
Spring EJB Configuration:
<beans:bean id = "userEJBLocal"
class = "org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean" >
<property name = "jndiName" value = "ejb/userFind" />
<property name = "businessInterface" value = "br.com.icone.martan.modelo.repositorio.UserEJBLocal" />
</beans:bean>
<beans:bean id = "myController" class = "br.com.icone.martan.security.AppUserDetailsService" >
<property name = "userEJBLocal" ref = "userEJBLocal" />
</beans:bean>