3

当我尝试使用 JSF 2 登录时遇到问题。我收到以下消息:

WEB9102:Web 登录失败:com.sun.enterprise.security.auth.login.common.LoginException:登录失败:安全异常

这是我的登录页面:

<?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>

这是我的 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.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author 081315620876
 */
@ManagedBean
@SessionScoped
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(); 
         System.out.println(context == null);
         System.out.println(request == null);
        try {        
            request.login(this.username, this.password);         
           if(request.isUserInRole("admin")) {  
                return "/pages/protected/admin/index.xhtml?faces-redirect=true";  
           } else if(request.isUserInRole("professor")){  
                return "/pages/protected/professor/index.xhtml?faces-redirect=true";  
           }         
        } catch (ServletException e) {
            context.addMessage(null, new FacesMessage("Login failed."));
            return "error";
        }
        return "login.xhtml";
    }

    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."));
        }
    }
}

提前感谢您的帮助:)

4

1 回答 1

3

登录失败:安全异常意味着您的用户名或密码错误。如果您使用的是数据库,请检查您是否已正确连接以及您的用户名和密码是否正确。

于 2012-07-27T12:27:39.177 回答