gpt4 book ai didi

java - 使用 Glassfish 3.1.2 和 JAAS 登录 JSF 2.0

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:23 25 4
gpt4 key购买 nike

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

WEB9102: Web Login Failed: com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Security Exception

这是我的登录页面:

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

在此先感谢您的帮助:)

最佳答案

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

关于java - 使用 Glassfish 3.1.2 和 JAAS 登录 JSF 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10710677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com