I'm having an authentication problem when I add the datasource to my project, so when I try to sign in, authentication happens but not the transition to the post-login page. I use the custom UserDetailsService, get the connection to the bank, but can not authenticate, and no error appears my context.xml is:
i <Resource auth="Container"
name="jdbc/dbtest"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="10"
maxWait="-1"
username="test"
password="test"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/dbteste?autoReconnect=true"
debugMode="true"
validationQuery="SELECT 1">
My hibernate.cfg.xml looks like this:
i <session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/dbtest</property><property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<property name="connection.shutdown">true</property>
My applicationContext looks like this:
<jee:jndi-lookup expected-type="javax.sql.DataSource" id="securityDataSource" jndi-name="jdbc/dbtest"></jee:jndi-lookup>
<b:bean id="loginBean" name="authenticationService" class="br.com.app.controller.AuthenticationService">
<b:property name="authenticationManager" ref="authenticationManager"></b:property>
</b:bean>
<http auto-config="true" use-expressions="true" access-denied-page="/index.xhtml">
<form-login login-page="/login.xhtml"
always-use-default-target="true"
default-target-url="/cadastro/central.xhtml"
authentication-failure-url="/login.xhtml?login_error=1" />
<logout invalidate-session="true"
logout-success-url="/index.xhtml"
delete-cookies="JSESSION"/>
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>
</authentication-manager>
<b:bean id="userDetailsService" class="br.com.app.controller.UserDetailsServiceImpl"/>
web.xml looks like this:
<resource-ref>
<description>DataSource</description>
<res-ref-name>jdbc/dbtest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And the UserDetailsServiceImp implementation looks like this:
public class UserDetailsServiceImpl implements UserDetailsService{
private UsuarioDAO us;
@PersistenceContext
private Session sessao;
public UserDetailsServiceImpl() {
us = new UsuarioDAO();
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return findByUsername(username);
}
private Usuario findByUsername(String username) {
Usuario login;
sessao = us.getSession();
try {
System.out.println("Entrou na Classe UserDetaisServiceImpl e procurando o usuário");
Criteria cr = sessao.createCriteria(Usuario.class);
cr.add(Restrictions.eq("email", username));
login = (Usuario) cr.uniqueResult();
System.out.println("Login: "+login.toString());
if (login == null) {
FacesContext.getCurrentInstance().addMessage("Info", new FacesMessage(FacesMessage.SEVERITY_ERROR, "UserDetailsService: Não foi possivel fazer login, sua lincença expirou ou o login foi digitado incorretamente", "Esse usuário não existe!"));
}
if (!login.isCredentialsNonExpired()) {
FacesContext.getCurrentInstance().addMessage("Info", new FacesMessage(FacesMessage.SEVERITY_ERROR, "UserDetailsService: Não foi possivel fazer login, licença expirou", "Favor tente novamente!"));
return null;
} else {
return login;
}
} catch (NoResultException e) {
e.printStackTrace();
throw new UsernameNotFoundException("UserDetailsService: Usuario nao encontrado: " + e.getMessage());
} finally {
sessao.close();
}
}
I tested here using a login that does not exist and returns that the login is wrong, I have tried it in several ways and I can not, it can not transition from the login page to the login page.