Java后台Cookie设置

5,568 阅读1分钟

后台设置

    public String admin(HttpServletRequest request, HttpServletResponse response) {
        Principal userPrincipal = request.getUserPrincipal();
        // 获取sessionId
        HttpSession session = request.getSession(false);
        String id = session.getId();
        System.out.println("session" + id);

        //获取request中的cookie
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + ":" + cookie.getValue());
        }

        //添加cookie
        Cookie cookie = new Cookie("username", userPrincipal.getName());
        cookie.setMaxAge(1000);
        cookie.setPath("/");
        cookie.setSecure(false);
        cookie.setVersion(0);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return "admin";
    }

前端响应

前台操作js库 www.npmjs.com/package/js-…