过滤器无法正常工作JSF

时间:2012-11-28 18:51:13

标签: java jsf authentication servlet-filters

当登录失败时,过滤器会将我重定向到/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() {        
    }

}

这是login()托管bean的Autenticar方法,其托管bean名称为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>

1 个答案:

答案 0 :(得分: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接口中最重要的方法是doFilter方法,它是过滤器的核心。