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

🎨 #12932 Cookie

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