2012-05-22 66 views
3

我有问题,当我尝试用JSF 2.我收到以下消息登录:登录在JSF 2.0 Glassfish的3.1.2和JAAS

WEB9102:网络登录失败:的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.")); 
     } 
    } 
} 

预先感谢帮助:)

回答

3

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