Commit bb594c92 authored by yanzg's avatar yanzg

异常处理显示

parent 08a1a844
......@@ -25,6 +25,10 @@ public class CookiesHelper {
* 单个Cookies最大长度
*/
public static final int ITEM_MAX_SIZE = 1000;
/**
* 默认最大有效期
*/
public static final int DEFAULT_MAX_AGE = 3 * 24 * 60 * 60;
/**
* 编码方式
*/
......@@ -43,11 +47,13 @@ public class CookiesHelper {
/**
* 读取缓存Cookies
*
* @param key 键值
* @param key 键值
* @param remove 读取后是否删除
* @return 缓存的键值
*/
public static String get(String key) throws UnsupportedEncodingException {
public static String get(String key, boolean remove) throws UnsupportedEncodingException {
HttpServletRequest request = getRequest();
HttpServletResponse response = getResposne();
if (request.getCookies() == null) {
return StringHelper.EMPTY;
}
......@@ -57,6 +63,7 @@ public class CookiesHelper {
// 遍历cookies找到对应的Cookies
for (Cookie item : request.getCookies()) {
if (item.getName().equals(key)) {
removeCookie(response, item.getName());
if (StringHelper.isNumber(item.getValue())) {
keyLength = StringHelper.toInt(item.getValue());
continue;
......@@ -64,6 +71,7 @@ public class CookiesHelper {
return URLDecoder.decode(item.getValue(), CHARSET);
}
} else if (item.getName().startsWith(key)) {
removeCookie(response, item.getName());
map.put(key, item.getValue());
}
}
......@@ -106,17 +114,41 @@ public class CookiesHelper {
}
}
/**
* 删除Cookie对象
*
* @param response 输出流
* @param key 键值
*/
private static void removeCookie(HttpServletResponse response, String key) {
addCookie(response, key, StringHelper.EMPTY, 0);
}
/**
* 添加Cookies缓存
*
* @param key 键
* @param value 值
* @param response 输出对象
* @param key 键
* @param value 值
*/
private static void addCookie(HttpServletResponse response, String key, String value) {
addCookie(response, key, value, DEFAULT_MAX_AGE);
}
/**
* 添加Cookies缓存
*
* @param response 输出对象
* @param key 键
* @param value 值
* @param maxAge 最大有效期
*/
private static void addCookie(HttpServletResponse response, String key, String value, int maxAge) {
// 创建cookie对象
Cookie cookie = new Cookie(key, value);
// 设置根目录生效
cookie.setPath("/");
cookie.setMaxAge(maxAge);
// 服务器把cookie响应给客户端,所有的cookie对象,都会在服务器端创建,通过http响应给客户端(浏览器)
response.addCookie(cookie);
}
......@@ -160,7 +192,7 @@ public class CookiesHelper {
* 登录初始化
*/
public static void tokenInit() throws UnsupportedEncodingException {
String tokenRequest = get(CookiesHelper.TOKEN_STRING_KEY);
String tokenRequest = get(CookiesHelper.TOKEN_STRING_KEY, true);
if (!StringHelper.isEmpty(tokenRequest)) {
TokenHelper.setTokenString(tokenRequest);
}
......
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