当登录失败时,过滤器会将我重定向到/users/indexLogueados.xhtml
. 这是不正确的。过滤器应该将我重定向到/faces/index.xhtml
.
为什么会这样?
这是过滤器:
@WebFilter("/faces/usuarios/*")
public class FiltroUsuarios implements Filter {
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
Autenticar auten = (Autenticar) req.getSession().getAttribute("aut");
if (auten != null && auten.isLogueado()) {
// User is logged in, so just continue request.
chain.doFilter(request, response);
} else {
// User is not logged in, so redirect to index.
HttpServletResponse res = (HttpServletResponse) response;
res.sendRedirect(req.getContextPath() + "/faces/index.xhtml");
}
}
@Override
public void destroy() {
}
}
这是具有托管 bean 名称的托管 bean的login()
方法:Autenticar
aut
public String login() {
RequestContext context = RequestContext.getCurrentInstance();
FacesMessage msg;
try {
cl = ClienteService.getInstance().getClienteLogin(nombre);
if (contrasenia.equals(cl.getClave())) {
logueado = true;
ruta = "usuarios/indexLogueados.xhtml";
msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Bienvenido ", nombre);
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
logueado = false;
ruta = "index.xhtml";
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error de usuario", "Credenciales erroneas");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
} catch (Exception ex) {
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Error de usuario", "Credenciales erroneas");
ruta = "index.xhtml";
logueado = false;
FacesContext.getCurrentInstance().addMessage(null, msg);
}
FacesContext.getCurrentInstance().addMessage(null, msg);
context.addCallbackParam("logueado", logueado);
logueado = false;
return ruta;
}
这是登录表单:
<h:form id="formularioAltas">
<div class="buttonBarItem">
<p:commandButton id="basic" value="" style="background:url(css/images/backgroundL1.png) no-repeat; border: none" styleClass="clientes" onclick="dlg2.show();" type="button" />
<h:panelGrid columns="2" cellpadding="5">
<h:outputLabel for="nombre" value="Usuario: " />
<p:inputText value="#{aut.nombre}"
id="nombre" required="true" label="Usuario" />
<h:outputLabel for="contrasenia" value="Contraseña:" />
<h:inputSecret value="#{aut.contrasenia}"
id="contrasenia" required="true" label="Contraseña" />
<f:facet name="footer">
<p:commandButton id="loginButton" value="Entrar" action="#{aut.login}" type="submit" styleClass="ui-state-default-formu" />
</f:facet>
</h:panelGrid>
</div>
</h:form>