當登錄失敗時,過濾器將我重定向到/users/indexLogueados.xhtml
。這是不正確的。過濾器應該將我重定向到/faces/index.xhtml
。過濾器無法正常工作JSF
爲什麼會發生這種情況?
這裏是過濾器:
@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名aut
的Autenticar
管理bean的login()
方法:
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>
你已經調試過了,你確定過濾器中的else {}被調用了嗎? – fatfredyy