用户使用JSF 2.0登录

Fab*_*bio 3 java jsf login java-ee jsf-2

我正在尝试 - 使用JSF 2.0 - 以一种巧妙的方式实现登录/记住我/注销管理.由于传统<form action="j_security_check" ...方式缺乏灵活性,我决定采用不同的路径,但我发现了一个问题.

声明性安全正确地通过设置无论是在应用服务器<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>
Run Code Online (Sandbox Code Playgroud)

简单的LoginBean#login():

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";
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但成功登录后我不知道如何将用户转发到其原始请求.由于登录页面自动插入客户端请求和"任何"安全资源之间,因此我需要一种方法来了解重定向操作的位置.request.getRequestURL( )没有帮助,可能是因为RequestDispatcher#forward()(覆盖了请求URL)干预.您认为这是管理登录过程的适当方式吗?如果是这样,任何关于这个问题的暗示?

非常感谢!

Rob*_*bin 6

在登录视图中添加以下行.它在登录期间存储请求的页面.

<f:param name="redirect" value="#{requestScope['javax.servlet.forward.request_uri']}" />
Run Code Online (Sandbox Code Playgroud)

然后在您的登录bean中获取请求的uri.

FacesContext context = FacesContext.getCurrentInstance();
String redirect = context.getExternalContext().getRequestParameterMap().get("redirect");
Run Code Online (Sandbox Code Playgroud)

添加?faces-redirect=true到字符串并返回它.