程序验证有时不起作用

时间:2016-02-07 09:47:52

标签: jsf authentication tomcat8

我在Tomcat 8中使用JSF 2.2,并使用基于表单的身份验证。成功登录后,有时会正确显示主页。但是,有些时候,即使在成功登录后,也会显示登录页面。重新输入用户名和密码2-3次后,将显示主页。

XHTML页面如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:b="http://bootsfaces.net/ui">

<h:head>
</h:head>
<h:body>
<h:form id="loginForm">
    <b:container layout="fluid" style="margin-top:100px;">
        <b:row>
            <b:column medium-screen="3">
            </b:column>
            <b:column medium-screen="6">
                <b:panelGrid colSpans="12,0" id="stateGridId">
                    <b:panel title="#{msg['login_panel_key']}" look="primary">
                        <b:row>
                            <b:column medium-screen="12">
                                <b:message for="username" />
                                <b:message for="password" />
                                <b:message for="loginButton" />
                                <br />
                            </b:column>
                        </b:row>
                        <b:row>
                            <b:column offset="2" medium-screen="8">
                                <b:inputText placeholder="#{msg['username_label_key']}" required="true"
                                    id="username" value="#{login.user}" requiredMessage="#{msg['user_req_message']}">
                                    <f:facet name="prepend">
                                        <b:icon name="user" />
                                    </f:facet>
                                </b:inputText>
                            </b:column>
                        </b:row>
                        <b:row>
                            <b:column offset="2" medium-screen="8">
                                <b:inputSecret placeholder="#{msg['password_label_key']}" required="true"
                                    id="password" value="#{login.pwd}" requiredMessage="#{msg['password_req_message']}">
                                    <f:facet name="prepend">
                                        <b:iconAwesome name="key" />
                                    </f:facet>
                                </b:inputSecret>
                            </b:column>
                        </b:row>
                        <b:row>
                            <b:column offset="2" span="4">
                                <b:commandButton id="loginButton" value="#{msg['login_button_key']}" action="#{login.login}"
                                    look="primary" style="width:100%" />
                            </b:column>
                        </b:row>
                    </b:panel>
                </b:panelGrid>
            </b:column>
            <b:column medium-screen="3">
            </b:column>
        </b:row>
    </b:container>
</h:form>
</h:body>
</html>

托管bean看起来像这样:

package com.saptarshibasu.poc.login;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.saptarshibasu.poc.Utils;

@ManagedBean
public class Login {
protected String user;
protected String pwd;

@ManagedProperty(value="#{utils}")
private Utils utils;

public Utils getUtils() {
    return utils;
}

public void setUtils(Utils utils) {
    this.utils = utils;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

public String login() {
    HttpServletRequest origRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
            .getRequest();
    try {
        if(origRequest.getUserPrincipal()==null)
        {
            origRequest.login(user, pwd);
        }
        return "/pages/home.xhtml?faces-redirect=true";
    } catch (ServletException e) {
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage fMessage = new FacesMessage(context.getApplication().getResourceBundle(context, "msg").getObject("login_err_message").toString());
        fMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(utils.findComponent("username").getClientId(), fMessage);
        return null;
    }

}
}

1 个答案:

答案 0 :(得分:0)

这是Bootsfaces commandButton中的一个错误。现在它已修复。详细信息可供here