Pessoal, boa tarde.
Sou novato no mundo java e estou tentando desenvolver um projeto aqui com um amigo, mas to quebrando a cabeça aqui com esse lance de bad credentials…
O lance é que eu estou fazendo um aplicativo web (.jsf) que utiliza o Spring Security mas durante a autenticação do usuário no sistema, mesmo eu informando o login que é o email da pessoa e a senha correta a mensagem que me retorna é BAD CREDENTIALS.
Tudo o que eu quero é somente validar o usuário através do email e da senha. Até onde consegui analisar o meu codigo SQL em applicationContext-security.xml está aparentemente correto. Mas como eu estou aprendendo…minha análise não tem muito peso… ?
Se alguem puder me dar uma luz pra eu ver onde está errado, agradeço imensamente.
Segue o meu codigo:
Usuário Java
[code]
package com.matrix.modelo.user;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table (name=“usuario”)
public class Usuario implements Serializable{
private static final long serialVersionUID = -4134705784716581627L;
@Id
@GeneratedValue
@Column (name=“id”)
private Integer id;
@Column (name=“pnome”)
private String pNome;
@Column(name=“snome”)
private String sNome;
@org.hibernate.annotations.NaturalId
@Column(name=“email”)
private String email;
@Column(name=“senha”)
private String senha;
@Column(name=“dataNasc”)
private Date dtNasc;
@ElementCollection(targetClass = String.class)
@JoinTable(
name = "user_profile",
uniqueConstraints = {@UniqueConstraint(columnNames = {"id", "permissao"})},
joinColumns = @JoinColumn (name = "id"))
@Column(name = "permissao")
private Set<String> permissao = new HashSet<String>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getpNome() {
return pNome;
}
public void setpNome(String pNome) {
this.pNome = pNome;
}
public String getsNome() {
return sNome;
}
public void setsNome(String sNome) {
this.sNome = sNome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public Date getDtNasc() {
return dtNasc;
}
public void setDtNasc(Date dtNasc) {
this.dtNasc = dtNasc;
}
public Set<String> getPermissao() {
return permissao;
}
public void setPermissao(Set<String> permissao) {
this.permissao = permissao;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dtNasc == null) ? 0 : dtNasc.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((pNome == null) ? 0 : pNome.hashCode());
result = prime * result
+ ((permissao == null) ? 0 : permissao.hashCode());
result = prime * result + ((sNome == null) ? 0 : sNome.hashCode());
result = prime * result + ((senha == null) ? 0 : senha.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Usuario other = (Usuario) obj;
if (dtNasc == null) {
if (other.dtNasc != null)
return false;
} else if (!dtNasc.equals(other.dtNasc))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (pNome == null) {
if (other.pNome != null)
return false;
} else if (!pNome.equals(other.pNome))
return false;
if (permissao == null) {
if (other.permissao != null)
return false;
} else if (!permissao.equals(other.permissao))
return false;
if (sNome == null) {
if (other.sNome != null)
return false;
} else if (!sNome.equals(other.sNome))
return false;
if (senha == null) {
if (other.senha != null)
return false;
} else if (!senha.equals(other.senha))
return false;
return true;
}
}[/code]
Usuario DAO
package com.matrix.modelo.user;
import java.util.List;
public interface UsuarioDAO {
public void cadastrar(Usuario usuario);
public void atualizar(Usuario usuario);
//public Usuario carregar(Integer id);
}
UsuárioDAOHibernate
[code]
package com.matrix.modelo.user;
import java.util.List;
import org.hibernate.Session;
public class UsuarioDAOHibernate implements UsuarioDAO {
private Session session;
public void setSession(Session session){
this.session = session;
}
public void atualizar(Usuario usuario) {
this.session.update(usuario);
}
public void cadastrar(Usuario usuario){
//if(usuario.getPermissao()==null ||usuario.getPermissao().size()==0){
// Usuario usuarioPermissao = this.carregar(usuario.getId());
// usuario.setPermissao(usuarioPermissao.getPermissao());
//this.session.evict(usuarioPermissao);
//}
this.session.save(usuario);
// }
//public Usuario carregar(Integer id){
//return (Usuario) this.session.get(Usuario.class, id);
}
}[/code]
UsuarioRN (Classe contendo as regras de negocio)
[code]
package com.matrix.modelo.user;
import java.util.List;
import com.matrix.util.DAOFactory;
public class UsuarioRN {
private UsuarioDAO usuarioDAO;
public UsuarioRN(){
this.usuarioDAO = DAOFactory.criarUsuarioDAO();
}
public void cadastrar(Usuario usuario){
Integer id = usuario.getId();
if (id==null|| id==0){
usuario.getPermissao().add(“ROLE_USUARIO”);
this.usuarioDAO.cadastrar(usuario);
}else{
this.usuarioDAO.atualizar(usuario);
}
}
}[/code]
applicationContext-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<http>
<intercept-url pattern="/admin/**" access="ROLE_ADMINISTRADOR" />
<intercept-url pattern="/restrict/**" access="ROLE_USUARIO" />
<form-login login-page="/public/login.jsf"
always-use-default-target="true"
default-target-url="/restrict/insert.jsf"
authentication-failure-url="/public/login.jsf?login_error=1" />
<logout/>
<remember-me />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="IhateitDataSource"
authorities-by-username-query="SELECT u.email, p.permissao
FROM usuario u, user_profile p
WHERE u.id = p.id
AND u.email = ?"
users-by-username-query="SELECT id, email, senha
FROM usuario
WHERE email = ?" />
</authentication-provider>
</authentication-manager>
</b:beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ihateit</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<!-- FILTRO DE CONEXÕES -->
<filter>
<filter-name>conexaoFilter</filter-name>
<filter-class>com.matrix.web.filter.ConexaoHibernateFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>conexaoFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
<!-- FIM DO FILTRO -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>E:\temp-files\</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!--CONFIGURAÇÃO DATASOURCE-->
<resource-ref>
<description>DataSource IhateitDB</description>
<res-ref-name>jdbc/IhateitDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--CONFIGURAÇÃO SPRING SECURITY-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>