1

可能重复:
JSF HTTP 会话登录
避免 JSF+Primefaces 应用程序
Primefaces 登录应用程序上的后退按钮

我已经使用 Prime Faces 开发了 Web 应用程序。现在我想改进这里的登录系统,到目前为止我已经做了以下工作。

1. index.xhtml

<h:form>
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginBean.username}" 
                id="username" required="true" label="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{loginBean.password}" 
                id="password" required="true" label="password" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Login" update=":growl" action="#{loginBean.doLogin}" actionListener="#{loginBean.login}" oncomplete="handleLoginRequest(xhr, status, args)" style="height: 30px; font-size: 12px"  />
<p:commandButton type="reset" value="Clear" style="height: 30px; font-size: 12px"/>
</f:facet>
</h:panelGrid>
</h:form>

2.LoginBean.java

public class LoginBean {
private String username;
private String password;

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public List<user> getUser() {
    List<user> tableList = new ArrayList<user>();    
    String query = "SELECT * FROM users";

    try {

        Connection conn = new Connector().getConn();
        Statement select = conn.createStatement();
        ResultSet result = select.executeQuery(query);

        while (result.next()) {

            user c = new user();

            c.setID(result.getInt("ID"));
            c.setName(result.getString("name"));
            c.setPassword(result.getString("password"));

            tableList.add(c);
        }
        select.close();
        // result.close();
        conn.close();
    } catch (SQLException e) {
        System.err.println("Mysql Statement Error: " + query);
        e.printStackTrace();
    }
    //System.out.println(tableList);
    return tableList;
}

public void login(ActionEvent actionEvent) {

            List<user> tableList3=getUser();

    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;
    boolean loggedIn = false;

    for(int i=0;i<tableList3.size();i++){
             String user_name = tableList3.get(i).getName();
             String pass_word = tableList3.get(i).getPassword();

    if(username != null  && username.equals(user_name) && password != null  && password.equals(pass_word)) {
        loggedIn = true;
        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
                    //return "table?faces-redirect=true";


    } else {
        loggedIn = false;
        msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
                    //return "new?faces-redirect=true";
    }

    FacesContext.getCurrentInstance().addMessage(null, msg);
    context.addCallbackParam("loggedIn", loggedIn);
}
  }

 public String doLogin(){  
List<user> tableList2=getUser();
String msg = null;

for(int i=0;i<tableList2.size();i++){
String user_name = tableList2.get(i).getName();
String pass_word = tableList2.get(i).getPassword();

if(username != null  && username.equals("admin") && password != null  && password.equals("admin")){
msg = "table?faces-redirect=true";
}

else if(user_name.contains(username)&& pass_word.contains(password)&& !user_name.contains("admin")) {
msg = "table1?faces-redirect=true";
    }
}

            return msg;
        }

public String logout() {
           return "index?faces-redirect=true";
}
}

在这里,用户可以在未登录系统的情况下加载页面。我想通过使用会话来防止这种情况。但我不清楚如何做到这一点。所以请帮助我一步一步地做到这一点。

谢谢你。

4

0 回答 0