我需要限制对应用程序的一部分的访问。为了访问该部分,用户需要登录。我的数据库中有一个名为 User 的表,其中包含用户名和散列密码以及由两个输入和一个提交组成的登录表单。但是,我不知道应该使用哪些类/数学来登录用户(我假设 jsf 中支持此功能)。另外,据我所知,我需要编辑 web.xml 以支持身份验证。有人可以提出一个典型的解决方案和一般步骤,我需要做才能获得该功能(链接,非常有价值的教程)?
我还想知道如果该人未登录,如何限制对另一个页面的访问,因此当用户键入页面的直接链接时,他将被重定向到主登录页面。
预先感谢您的任何帮助。
格莱姆.
您可以使用 Servlet 3.0 中引入的 HttpServletRequest API:
/**
* Performs authentication via HttpServletRequest API
*/
public String login(String username, String password) throws IOException {
try {
getRequest().login(username, password);
this.user = userDao.find(username);
} catch (ServletException e) {
JsfUtil.addErrorMessage(JsfUtil.getStringResource("loginFailed"));
return null;
}
return "/index?faces-redirect=true";
}
public String logout() throws ServletException {
this.user = null;
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
if (isAuthenticated())
getRequest().logout();
return "logout";
}
public boolean isAuthenticated() {
return getRequest().getUserPrincipal() != null;
}
public static HttpServletRequest getRequest() {
Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
return request instanceof HttpServletRequest
? (HttpServletRequest) request : null;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)