Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保持登录状态';行不通_Java_Jsf_Cookies_Servlet Filters_Stay Logged In - Fatal编程技术网

Java 保持登录状态';行不通

Java 保持登录状态';行不通,java,jsf,cookies,servlet-filters,stay-logged-in,Java,Jsf,Cookies,Servlet Filters,Stay Logged In,我试着做 这是我添加cookie的方法 public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { Cookie cookie = new Cookie(name, value); cookie.setMaxAge(maxAge); response.addCookie(cookie); } 此方法的

我试着做

这是我添加cookie的方法

public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
此方法的调用在此处。。。(省略另一代码)

登录过程工作正常。但是我关闭浏览器并打开,此时getCookieValue方法不返回我的cookie。我不知道,因为那样会发生

谢谢


PS:对不起,我的英语

要详细说明@eis所说的内容,请尝试以下内容:

public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath( "/" );
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

我没看到你为那块饼干设置任何路径。需要设置路径。您是否验证了http通信会发生什么情况?cookie设置是否正确?请使用Set-Cookie标头检查http流量,并将其添加到问题中。您好,感谢回复。在第一种方法中,使用了。天气真好!请重读我写的东西。我没有看到你为cookie设置任何路径。我可以看到你在添加饼干。嗨,你说得对,很好。非常感谢。我不知道,因为@BalusC没有添加这一行。快乐日。我以前的答案只是一个开始的例子,如果你不在webapp中使用子文件夹,它应该会起作用。无论如何,我已经更新了我的旧答案。
@WebFilter(filterName="FiltroSeguridad")
public class FiltroSeguridad implements Filter{


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpSession session = httpRequest.getSession(true);
        CuentaUsuario cuentaUsuario = (CuentaUsuario)session.getAttribute(Constantes.USER);

        //Si el usuario no ha iniciado sesión
        if(Utils.isEmpty(cuentaUsuario)){
            //Se obtiene valor de la cookie
            String uuid = Utils.getCookieValue(httpRequest, Constantes.COOKIE_LOGIN);

            if(!Utils.isEmpty(uuid)){
                Map<String, Object> parametros = new HashMap<String, Object>();
                parametros.put("email", Encryption.decrypt(uuid));

                //Existe un usuario con el valor guardado en la cookie
                List<CuentaUsuario> cuentasUsuarios = crudeServiceBean.findWithNamedQuery(NamedQueries.CONSULTA_LOGIN_CUENTA_COOKIE, parametros, 1);
                if(!Utils.isEmpty(cuentasUsuarios)){
                    session.setAttribute(Constantes.USER, cuentasUsuarios.get(0));
                    Utils.addCookie(httpResponse, Constantes.COOKIE_LOGIN, uuid, Constantes.COOKIE_AGE);
                    cuentaUsuario = cuentasUsuarios.get(0);
                }else{
                    Utils.removeCookie(httpResponse, Constantes.COOKIE_LOGIN);
                }
            }
        }

        //No existe cookie y el usuario no está logueado
        if(Utils.isEmpty(cuentaUsuario)){
            session.setAttribute(Constantes.RUTA,httpRequest.getRequestURI());
            httpResponse.sendRedirect(httpRequest.getContextPath()+"/generales/login.xhtml");
        }else{
            chain.doFilter(request, response);
        }
    }

}
public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath( "/" );
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }