JSF 2.0:如何在使用HttpServletRequest.login后重定向到受保护的页面

use*_*352 5 java authentication jsf-2

我正在尝试使用基于表单的身份验证的HttpServletRequest.login.

一切都好(容器告诉登录/密码是否良好),除了在用户输入登录后,我不知道如何将用户重定向到他要求的受保护页面(重新显示登录表单).怎么做?

在此先感谢您的帮助.

代码:

web.xml中:

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>security</realm-name>
    <form-login-config>
        <form-login-page>/faces/loginwithlogin.xhtml</form-login-page>
        <form-error-page>/faces/noaut.xhtml</form-error-page>
    </form-login-config>
</login-config>
Run Code Online (Sandbox Code Playgroud)

页面loginwithlogin.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Authentication</title>
    </h:head>
    <h:body>
        <h:form>
            Login :
            <h:inputText value="#{login.login}" required="true" />
            <p/>
            Mot de passe :
            <h:inputSecret value="#{login.password}" required="true" />
            <p/>
            <h:commandButton value="Connexion" action="#{login.submit}">
                 <f:ajax execute="@form" render="@form" />
            </h:commandButton>
            <h:messages />
        </h:form>
    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

更新:没有Ajax它不起作用.

支持豆:

@Named
@SessionScoped
public class Login implements Serializable {
  private String login;
  private String password;
  // getters and setters 
  ...

  public void submit() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = 
            (HttpServletRequest) context.getExternalContext().getRequest();
    try {
        request.login(login, mdp);
        context.addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_INFO, 
                "OK", null));
    } catch (ServletException e) {
        context.addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                "Bad login", null));
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 5

在基于容器管理表单的身份验证的情况下,登录页面位于由a打开的封面下,RequestDispatcher#forward()因此原始请求URI可用作具有由其标识的名称的请求属性RequestDispatcher#FORWARD_REQUEST_URI.请求属性(基本上,请求范围)在JSF中可用ExternalContext#getRequestMap().

因此,这应该做:

private String requestedURI;

@PostConstruct
public void init() {
    requestedURI = FacesContext.getCurrentInstance().getExternalContext()
        .getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);

    if (requestedURI == null) {
        requestedURI = "some/default/home.xhtml";
    }
}

public void submit() throws IOException {
    // ...

    try {
        request.login(username, password);
        externalContext.redirect(requestedURI);
    } catch (ServletException e) {
        context.addMessage(null, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                "Bad login", null));
    }
}
Run Code Online (Sandbox Code Playgroud)

您只需要创建bean @ViewScoped(JSF)或@ConversationScoped(CDI)而不是@SessionScoped(绝对不是@RequestScoped;否则需要使用不同的方法<f:param><f:viewParam>).