我试图用JSF 2.0来以一种整洁的方式实现登录/记住我/注销管理。由于传统的<form action="j_security_check" ...
方式缺乏灵活性,我决定采用不同的方式,但是我发现了一个问题。用户使用JSF 2.0登录
通过<security-domain>
和web.xml中的<security-constraint>
,<login-config>
和<form-login-page>
在申请服务器中正确设置声明性安全。
登录页面:
<h:form id="loginForm">
<h:panelGrid columns="2" cellspacing="5">
<h:outputText value="Username" />
<h:inputText value="#{loginBean.username}" />
<h:outputText value="Password:" />
<h:inputText value="#{loginBean.password}" />
<h:outputLabel value=""/>
<h:commandButton value="Login" action="#{loginBean.login}" />
</h:panelGrid>
</h:form>
和简单LoginBean#登录():
public String login()
{
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
try {
request.login(username, password);
}
catch (ServletException e) {
FacesContext.getCurrentInstance().addMessage("Unknown login...
return null;
}
return "i_dont_know_where_you_were_going";
}
一切正常,但在成功登录后,我不知道如何转发用户到其最初的要求。由于登录页面自动插入在客户端请求和“任何”安全资源之间,因此我需要一种方法来了解将动作重定向到何处。 request.getRequestURL()
没有帮助,可能是因为RequestDispatcher#forward()
(它会覆盖请求url)干预。你认为这是管理登录过程的适当方式吗?如果是这样,关于这个问题的任何暗示?
非常感谢!