Cookies操作


WebMVC模块针对Cookies这个小甜点提供了一个名为CookieHelper的小工具类,支持Cookie参数的设置、读取和移除操作,同时支持对编码和加密处理,并允许通过配置参数调整Cookie策略;

Cookie配置参数
#-------------------------------------
# Cookie配置参数
#-------------------------------------

# Cookie键前缀,可选参数,默认值为空
ymp.configs.webmvc.cookie_prefix=

# Cookie作用域,可选参数,默认值为空
ymp.configs.webmvc.cookie_domain=

# Cookie作用路径,可选参数,默认值为'/'
ymp.configs.webmvc.cookie_path=

# Cookie密钥,可选参数,默认值为空
ymp.configs.webmvc.cookie_auth_key=

# Cookie密钥验证是否默认开启, 默认值为false
ymp.configs.webmvc.default_enabled_cookie_auth=
示例代码:演示Cookie操作
// 创建CookieHelper对象
CookieHelper _helper = CookieHelper.bind(WebContext.getContext().getOwner());

// 设置开启采用密钥加密(将默认开启Base64编码)
_helper.allowUseAuthKey();

// 设置开启采用Base64编码(默认支持UrlEncode编码)
_helper.allowUseBase64();

// 添加或重设Cookie,过期时间基于Session时效
_helper.setCookie("current_username", "YMPer");

// 添加或重设Cookie,并指定过期时间
_helper.setCookie("current_username", "YMPer", 1800);

// 获取Cookie值
BlurObject _currUsername = _helper.getCookie("current_username");

// 获取全部Cookie
Map<String, BlurObject> _cookies = _helper.getCookies();

// 移除Cookie
_helper.removeCookie("current_username");

// 清理所有的Cookie
_helper.clearCookies();