JSF 2.0登录+会话范围处理的问题

时间:2011-06-21 22:20:49

标签: session logging jsf-2

我在处理JSF 2.0中的登录时遇到问题。我真的不知道我是否朝着正确的方向前进。我想具体一点,所以我会指出我的应用程序是如何工作的

  1. 会话作用域的登录页面     豆。
  2. 正确登录后的用户         被重定向到下一页         另一个会话scoped bean
  3. 的         第二个bean获取登录名         密码来自第一个bean的字符串         进一步用于数据库
  4. 我的问题是我是否为用户创建了两个会话?如果是这样,我怎么能杀死第一次会议。如果它不是管理登录的最佳方式,请更正我。提前谢谢

    这是一个简单的例子

    登录BEAN

    package main;
    
    import java.io.Serializable;
    import java.sql.SQLException;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    
    @SessionScoped
    
    @ManagedBean(name="loginBean")
    
    public class loginBean implements Serializable
    {
        private String login;
        private String password;
    
        public loginBean()
        {
    
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
    
    
        public String getLogin() {
            return login;
        }
    
        public void setLogin(String login) {
            this.login = login;
        }
    
        public String login() throws ClassNotFoundException, SQLException
        {
            SQL sql = new SQL(); //class connecting do database
            if( sql.login(this.login, this.password) == true )
            {
                return "yes"; 
            }
            else
            {
                return "no";
            }
        }
    
    }
    

    登录页面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">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html">
        <h:head>
    
        </h:head>
        <h:body>
            <h:form>
                <div id="logowanie" style="margin: auto; max-width: 180px; margin-top: 50px; ">
                <table border="0" cellspacing="4" cellpadding="4">
                    <thead></thead>
                    <tbody>
                        <tr>
                            <td><h:outputText value="login:" /></td>
                            <td><h:inputText value="#{loginBean.login}" size="10"/></td>
                        </tr>
                        <tr>
                            <td><h:outputText value="password:" /> </td>
                            <td><h:inputSecret value="#{login.password}" size="10"/></td>
                        </tr>
                        <tr>
                            <td colspan="2"><h:commandButton value="zaloguj" action="#{loginBean.login}" /></td>
                        </tr>
                    </tbody>
    
                </table>
    
                </div>
            </h:form>
        </h:body>
    </html>
    

    PANEL BEAN(第二个豆子)

    package main;
    
    import javax.faces.context.ExternalContext;
    import javax.faces.context.FacesContext;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    @SessionScoped
    @ManagedBean(name="panelBean")
    public class panelBean implements Serializable{
    
        private String login;
        private String password;
        FacesContext context = FacesContext.getCurrentInstance();
    
        public panelBean()
        {
            loginBean Bean = (loginBean) context.getApplication().getVariableResolver().resolveVariable(context, "loginBean");
    
            this.password = Bean.getPassword();
            this.login = Bean.getLogin();
    
    
            Bean.setPassword("");
            Bean.setLogin("");
    
    
            ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
            HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
            HttpSession session = (HttpSession)ectx.getSession(false);
            session.invalidate();
        }
    
    }
    

    FACES CONFIG

    <?xml version='1.0' encoding='UTF-8'?>
    
    <!-- =========== FULL CONFIGURATION FILE ================================== -->
    
    <faces-config version="2.0"
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
        <navigation-rule>
            <from-view-id>/login.xhtml</from-view-id>
            <navigation-case>
                <from-outcome>yes</from-outcome>
                <to-view-id>/panel.xhtml</to-view-id>
            </navigation-case>
            <navigation-case>
                <from-outcome>no</from-outcome>
                <to-view-id>/login.xhtml</to-view-id>
            </navigation-case>
        </navigation-rule>
    </faces-config>
    

1 个答案:

答案 0 :(得分:0)

这不会创建两个会话,而是两个会话范围的bean,它们位于同一个会话中。

但是,如果你创建管理登录视图范围的bean可能会更好,因为没有理由将它作为会话作用域(始终建议保持会话状态较小)。然后,loginBean只会设置panelBean的状态。

相关问题