2012-11-28 202 views
1

當登錄失敗時,過濾器將我重定向到/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名autAutenticar管理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> 
+0

你已經調試過了,你確定過濾器中的else {}被調用了嗎? – fatfredyy

回答

1

chain.doFilter(request, response);必須每次都執行(即使在發送重定向之後),但它僅在if部分中執行。將它移到外面並更改您的評估條件。

@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 not logged in, so redirect to index. 
     HttpServletResponse res = (HttpServletResponse) response; 
     res.sendRedirect(req.getContextPath() + "/faces/index.xhtml"); 
    } 
    //Just continue request. 
    chain.doFilter(request, response); 
} 

更多信息:

最重要Filter界面中的方法是doFi過濾法,這是過濾器的核心。

+0

那麼,如何做重定向,使過濾器的行爲? – dalcon

+0

當我重定向.. .. .. aut.login返回一個url,我不使用過濾器 – dalcon

+0

解決我重定向... HttpServletRequest請求; request =(HttpServletRequest)FacesContext.getCurrentInstance()。getExternalContext()。getRequest(); FacesContext.getCurrentInstance()。getExternalContext()。redirect(request.getContextPath()+ ruta); return; ....謝謝 – dalcon

相關問題