Problem solving: On Google browser, the solution that can't delete cookies using native JS

Problem scenario

When testing the exit function of the project, it was found that after exiting, the microservice framework generated cookiestill exists. When entering the homepage, there is no abnormal jump, but normal entry. For simple handling, the use of native JSbe cookiedeleted. But after execution, it was cookienot deleted normally and cookiestill exists. This blog is mainly about the reasons and solutions for this situation.

Problem environment

software version
JDK 8

problem causes

Open the devtools page of Google Chrome, check the cookiepage, you can find that there is an attribute inside HttpOnly, this attribute is mainly to make the jsrelevant information in the webpage code unable to obtain, but the server can obtain it normally. The property to be deleted cookieis set HTTPONLY, so it cannot be controlled and cleared by JS. As shown below:
Insert picture description here

solution

Now that the server can operate normally, a clear cookieinterface is developed on the server , and then the interface is called to delete. The delete code is as follows:

public static void removeCookie(HttpServletResponse response, String name) {
    
    
	Cookie uid = new Cookie(name, null);
	uid.setPath("/");
	uid.setMaxAge(0);
	uid.setHttpOnly(true);
	response.addCookie(uid);
}

result

After calling the interface, it will cookiebe deleted smoothly .

to sum up

Pits are everywhere, you need to study and think more! ! !

Ask for praise

If my article is helpful to everyone, you can click like or favorite at the bottom of the article;
if there is a good discussion, you can leave a message;
if you want to continue to view my future articles, you can click Follow
You can scan the following QR code to follow me 'S public account: Fengye Zhixuege, check out my latest share!
Insert picture description here
Bye bye

Guess you like

Origin blog.csdn.net/u013084266/article/details/112362929