When setting the Cookie, when the domain name is set, the backend cannot get the Cookie, just comment out the domain setting

Wrote a public method to set Cookie

It was found that when the domain name was set, the backend could not get the cookie, just comment out the domain setting.

And, when the PATH is not set, for example, the requested URL is the project directory + "/login", and the cookie's PATH is the root directory:/

The new cookie code is as follows:

    /**
     * 设置Cookie的值,并使其在指定时间内生效
     *
     * @param cookieMaxage cookie生效的最大秒数
     */
    private static final void doSetCookie(HttpServletRequest request, HttpServletResponse response,
            String cookieName, String cookieValue, int cookieMaxage, boolean isEncode) {
        try {
            if (cookieValue == null) {
                cookieValue = "";
            } else if (isEncode) {
                cookieValue = URLEncoder.encode(cookieValue, "utf-8");
            }
            Cookie cookie = new Cookie(cookieName, cookieValue);
            cookie.setMaxAge(cookieMaxage);
            /*if (null != request) {// 设置域名的cookie
                String domainName = getDomainName(request);
                log.info(domainName);
                if (!"localhost".equals(domainName)) {
                    cookie.setDomain(domainName);
                }
            }
            cookie.setPath("/");*/
            response.addCookie(cookie);
        } catch (Exception e) {
             log.error("操作cookie异常", e);
        }
    }

 

The code for obtaining cookies is as follows:

    /**
     * 得到Cookie的值,
     *
     * @param request
     * @param cookieName
     * @return
     */
    public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) {
        Cookie[] cookieList = request.getCookies();
        if (cookieList == null || cookieName == null) {
            return null;
        }
        String retValue = null;
        try {
            for (int i = 0; i < cookieList.length; i++) {
                if (cookieList[i].getName().equals(cookieName)) {
                    if (isDecoder) {
                        retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8");
                    } else {
                        retValue = cookieList[i].getValue();
                    }
                    break;
                }
            }
        } catch (UnsupportedEncodingException e) {
            log.error("操作cookie异常", e);
        }
        return retValue;
    }

 

 

(MaxAge can be a positive number, which means that the cookie can exist from the creation to the expiration time, in seconds, this cookie will be stored on the client computer, save as a cookie file, no matter close the browser or close the computer, until the time It will not expire until then.

It can be a negative number, which means that the cookie is only stored in the browser's memory. As long as the browser is closed, the cookie will disappear. The default value of maxAge is -1.

It can also be 0, which means that the cookie is deleted from the client computer or browser memory. )

If the maxAge attribute is a positive number, it means that the cookie will automatically expire after maxAge seconds. The browser will persist the cookie whose maxAge is a positive number, that is, write it into the corresponding cookie file. Regardless of whether the customer closes the browser or the computer, as long as it is maxAge seconds before, the cookie is still valid when logging in to the website.

If maxAge is a negative number, it means that the cookie is only valid in this browser window and the child windows opened in this window, and the cookie becomes invalid after closing the window. The cookie whose maxAge is a negative number is a temporary cookie that will not be persisted and will not be written to the cookie file. Cookie information is stored in the browser memory, so the cookie disappears when you close the browser. Cookie default maxAge value is -1.

If maxAge is 0, it means that the cookie is deleted. The cookie mechanism does not provide a way to delete cookies, so the effect of deleting cookies can be achieved by setting the cookie to expire immediately. Invalid cookies will be deleted from the cookie file or memory by the browser.

The Cookie operation method provided by the response object has only one add operation add(Cookie cookie). If you want to modify a cookie, you can only use a cookie with the same name to overwrite the original cookie to achieve the purpose of modification. Only need to modify maxAge to 0 when deleting.

In the project encountered, a cookie was created in Action, maxAge is -1, and then to delete the cookie in another method, you can create a cookie with the same name and domain, and then set maxAge to 0, and then Delete the cookie file on the client side or the cookie in the browser memory through the addCookie method of response.

Note 1. When modifying or deleting a cookie, all attributes of the newly created cookie except value and maxAge, such as name, path, domain, etc., must be exactly the same as the original cookie. Otherwise, the browser will regard two different cookies as not being overwritten, resulting in failure to modify and delete.

Note 2: When reading the Cookie from the client, other attributes including maxAge are unreadable and will not be submitted. When the browser submits a cookie, only the name and value attributes will be submitted. The maxAge attribute is only used by the browser to determine whether the cookie has expired.

 

Guess you like

Origin blog.csdn.net/u013282737/article/details/105687919