我正在尝试将 HttpServletRequest.login 与基于表单的身份验证一起使用。
一切正常(容器告诉登录名/密码是否正确),除了用户输入登录名后,我不知道如何将用户重定向到他要求的受保护页面(重新显示登录表单)。怎么做?
在此先感谢您的帮助。
编码:
网页.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>
页面 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>
更新:没有 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));
}
}
}