可能重复:
JSF HTTP Session Login
Avoid back button on JSF+Primefaces application
Primefaces Login Application
我已经使用Prime Face开发了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";
}
}
在这里,用户可以加载登录到系统的页面。我想通过使用session来防止这种情况。但我还不清楚如何做到这一点。所以请一步一步帮助我这样做
谢谢。