JSF 2.0简单的登录页面 [英] JSF 2.0 Simple login page
问题描述
我需要限制访问应用程序的一部分。为了访问那一部分,用户需要登录。我有一个表在我的数据库称为用户,用用户名和密码散列,以及一个包含两个输入和一个提交登录表单。但是,我不知道我应该使用哪些类/ mathids登录用户(我假设有在JSF此功能的支持)。此外,据我知道,我需要编辑我的web.xml中支持的鉴别。可能有人提出一个典型的解决方案,而我需要,以获得该功能(链接,价值大大AP preciated的教程)做的一般步骤是什么?
I need to restrict the access to a part of the application. In order to access that part, user needs to log in. I have a table in my database called User, with usernames and hashed passwords and a login form that consists of two inputs and a submit. However, I don't know which classes/mathids should I use to log in the user (I assume that there is a support for this functionality in jsf). Also, as far as I know, I need to edit my web.xml to support the authentification. Could someone propose a typical solutions and general steps that I need to do in order to get that functionality (links, tutorials of a value greatly appreciated)?
我也想知道我怎么限制访问如果人不在,所以当在直接链接到一个页面的用户类型,他将被重定向到一个主登录页面登录另一个页面。
i also wonder how do I limit the access to another page if the person is not logged in so when the user types in the direct link to a page, he will be redirected to a main login page.
感谢您事先的任何帮助。
GREM。
Thanks in advance for any help. Grem.
推荐答案
您可以使用Servlet 3.0中引入了HttpServletRequest的API:
You could use the HttpServletRequest API introduced in Servlet 3.0:
/**
* 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;
}
这篇关于JSF 2.0简单的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!