大家好,目前我正在使用一种非常基本的方法将用户登录到应用程序中,即使用带有用户用户名和密码的表单后面的托管 bean 的表单,这对于有 2 个用户的小型应用程序来说很好,但是我我希望增加这一点并有一个注册页面等什么是最好的方法来做到这一点有没有好的教程,我正在使用 netbeans 和 glassfish
这是我当前的工作登录设置
<h:form id="login-form">
<h:outputText value="username:"/>
<h:inputText value="#{loginBean.username}" id="username"/>
<br/>
<h:outputText value="password:"/>
<h:inputSecret value="#{loginBean.password}" id="password"/>
<br/>
<h:commandButton id="button" value="Login" action="#{loginBean.doLogin}"/>
<br/>
<h:commandLink action="#{navigationBean.redirectToInfo}" value="Info page"/>
<br/>
<h:messages />
<br/>
</h:form>
和豆子
*/
@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 7765876811740798583L;
// Simple user database :)
private static final String[] users = {"test:test","kate:123456"};
private String username;
private String password;
private boolean loggedIn;
@ManagedProperty(value="#{navigationBean}")
private NavigationBean navigationBean;
/**
* Login operation.
* @return
*/
public String doLogin() {
// Get every user from our sample database :)
for (String user: users) {
String dbUsername = user.split(":")[0];
String dbPassword = user.split(":")[1];
// Successful login
if (dbUsername.equals(username) && dbPassword.equals(password)) {
loggedIn = true;
return navigationBean.redirectToWelcome();
}
}
// Set login ERROR
FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, msg);
// To to login page
return navigationBean.toLogin();
}
/**
* Logout operation.
* @return
*/
public String doLogout() {
// Set the paremeter indicating that user is logged in to false
loggedIn = false;
// Set logout message
FacesMessage msg = new FacesMessage("Logout success!", "INFO MSG");
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
return navigationBean.toLogin();
}
// ------------------------------
// Getters & Setters