Login com JSF - Login failed: Security Exception

0 respostas
felipeaqueiroz

Olá a todos.
Estou com um problema ao logar em minha aplicação JSF 2 no Glassfish 3.1.2, usando o Netbeans 7

Estou recebendo a seguinte mensagem de erro no console do glassfish:
Advertência: WEB9102: Web Login Failed: com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Security Exception
Minha página login.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./modeloLogin.xhtml"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html">
    
    <ui:define name="content">
        <p:growl id="growl" showDetail="true" life="3000" />  

        <div id="formulario">
            <p:panel id="pnl" header="Login">
                <h:form>  
                    <h:panelGrid columns="2" cellpadding="5">  
                        <h:outputLabel for="username" value="Usuário:" />  
                        <p:inputText value="#{beanLogin.username}"   
                                     id="username" required="true" label="username" />  

                        <h:outputLabel for="password" value="Senha:" />  
                        <p:password value="#{beanLogin.password}" feedback="false" minLength="" id="password"  label="password" />
                        
                        <p:commandButton id="loginButton" value="Efetuar Login" update=":growl"
                                         action="#{beanLogin.login()}" ajax="false"/> 
                        <p:commandLink value="Esqueceu a senha?" ></p:commandLink>

                    </h:panelGrid>  
                </h:form>                     
            </p:panel>
        </div> 
    </ui:define>
</ui:composition>
Meu BeanLogin:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sonic.action.login;

import java.security.Principal;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

@Stateless
@Named
public class BeanLogin {

    private String username;
    private String password;

    public BeanLogin() {
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {        
            Principal userPrincipal = request.getUserPrincipal();
            if (request.getUserPrincipal() != null) {
                request.logout();
            }
            request.login(this.username, this.password);
            userPrincipal = request.getUserPrincipal();
            System.out.println(userPrincipal.getName());        
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage("Login failed."));
            return "error";
        }
        return "loginAdmin";
    }

    public void logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        try {
            request.logout();
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage("Logout failed."));
        }
    }
}
Dessa vez eu acho que não é nenhuma configuração do glassfish (finalmente), mas a exceção ocorre na linha
request.login(this.username, this.password);

Agradeço a ajuda de todos.

Criado 22 de maio de 2012
Respostas 0
Participantes 1