Commit 2ade309f authored by Liang Ding's avatar Liang Ding

🎨 #12932 Cookie

parent 3042f0e6
......@@ -127,7 +127,6 @@ public class IndexProcessor {
cookie = new Cookie(Common.COOKIE_NAME_MOBILE_SKIN, specifiedSkin);
}
cookie.setMaxAge(60 * 60); // 1 hour
cookie.setPath("/");
response.addCookie(cookie);
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) context.attr(Keys.TEMAPLTE_DIR_NAME), dataModel);
......
......@@ -166,11 +166,9 @@ public class SkinConsole {
final Response response = context.getResponse();
final Cookie skinDirNameCookie = new Cookie(Common.COOKIE_NAME_SKIN, skin.getString(Option.ID_C_SKIN_DIR_NAME));
skinDirNameCookie.setMaxAge(60 * 60); // 1 hour
skinDirNameCookie.setPath("/");
response.addCookie(skinDirNameCookie);
final Cookie mobileSkinDirNameCookie = new Cookie(Common.COOKIE_NAME_MOBILE_SKIN, skin.getString(Option.ID_C_MOBILE_SKIN_DIR_NAME));
mobileSkinDirNameCookie.setMaxAge(60 * 60); // 1 hour
mobileSkinDirNameCookie.setPath("/");
response.addCookie(mobileSkinDirNameCookie);
ret.put(Keys.STATUS_CODE, true);
......
......@@ -178,23 +178,19 @@ public class StatisticMgmtService {
final StringBuilder builder = new StringBuilder("[").append("\"").append(request.getRequestURI()).append("\"]");
final Cookie c = new Cookie("visited", URLs.encode(builder.toString()));
c.setMaxAge(COOKIE_EXPIRY);
c.setPath("/");
response.addCookie(c);
} else if (needToAppend) {
cookieJSONArray.put(request.getRequestURI());
final Cookie c = new Cookie("visited", URLs.encode(cookieJSONArray.toString()));
c.setMaxAge(COOKIE_EXPIRY);
c.setPath("/");
response.addCookie(c);
}
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Parses cookie failed, clears the cookie[name=visited]");
final Cookie c = new Cookie("visited", null);
final Cookie c = new Cookie("visited", "");
c.setMaxAge(0);
c.setPath("/");
response.addCookie(c);
}
......
......@@ -46,7 +46,7 @@ import java.util.*;
* Solo utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.9.0.2, Sep 22, 2019
* @version 1.9.0.3, Nov 4, 2019
* @since 2.8.0
*/
public final class Solos {
......@@ -81,11 +81,6 @@ public final class Solos {
*/
public static final String COOKIE_SECRET;
/**
* Cookie HTTP only.
*/
public static final boolean COOKIE_HTTP_ONLY;
static {
ResourceBundle solo;
try {
......@@ -115,8 +110,6 @@ public final class Solos {
cookieSecret = RandomStringUtils.randomAlphanumeric(8);
}
COOKIE_SECRET = cookieSecret;
COOKIE_HTTP_ONLY = Boolean.valueOf(Latkes.getLocalProperty("cookieHttpOnly"));
}
/**
......@@ -273,17 +266,13 @@ public final class Solos {
final String tokenVal = cookieJSONObject.optString(Keys.TOKEN);
final String token = StringUtils.substringBeforeLast(tokenVal, ":");
if (StringUtils.equals(b3Key, token)) {
login(user, response);
return user;
}
}
} catch (final Exception e) {
LOGGER.log(Level.TRACE, "Parses cookie failed, clears the cookie [name=" + COOKIE_NAME + "]");
final Cookie cookie = new Cookie(COOKIE_NAME, null);
final Cookie cookie = new Cookie(COOKIE_NAME, "");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
......@@ -306,9 +295,7 @@ public final class Solos {
cookieJSONObject.put(Keys.TOKEN, b3Key + ":" + random);
final String cookieValue = Crypts.encryptByAES(cookieJSONObject.toString(), COOKIE_SECRET);
final Cookie cookie = new Cookie(COOKIE_NAME, cookieValue);
cookie.setPath("/");
cookie.setMaxAge(COOKIE_EXPIRY);
cookie.setHttpOnly(COOKIE_HTTP_ONLY);
response.addCookie(cookie);
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Can not write cookie", e);
......@@ -324,9 +311,8 @@ public final class Solos {
*/
public static void logout(final Request request, final Response response) {
if (null != response) {
final Cookie cookie = new Cookie(COOKIE_NAME, null);
final Cookie cookie = new Cookie(COOKIE_NAME, "");
cookie.setMaxAge(0);
cookie.setPath("/");
response.addCookie(cookie);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment