1

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

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>
4

1 回答 1

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 方法,它是过滤器的核心。

于 2012-11-29T01:38:29.183 回答