YMP在線手冊(cè)
/ Cookies操作
Cookies操作
WebMVC模塊針對(duì)Cookies這個(gè)小甜點(diǎn)提供了一個(gè)名為CookieHelper的小工具類,支持Cookie參數(shù)的設(shè)置、讀取和移除操作,同時(shí)支持對(duì)編碼和加密處理,并允許通過(guò)配置參數(shù)調(diào)整Cookie策略;
Cookie配置參數(shù)
#------------------------------------- # Cookie配置參數(shù) #------------------------------------- # Cookie鍵前綴,可選參數(shù),默認(rèn)值為空 ymp.configs.webmvc.cookie_prefix= # Cookie作用域,可選參數(shù),默認(rèn)值為空 ymp.configs.webmvc.cookie_domain= # Cookie作用路徑,可選參數(shù),默認(rèn)值為'/' ymp.configs.webmvc.cookie_path= # Cookie密鑰,可選參數(shù),默認(rèn)值為空 ymp.configs.webmvc.cookie_auth_key= # Cookie密鑰驗(yàn)證是否默認(rèn)開(kāi)啟, 默認(rèn)值為false ymp.configs.webmvc.default_enabled_cookie_auth=
示例代碼:演示Cookie操作
// 創(chuàng)建CookieHelper對(duì)象 CookieHelper _helper = CookieHelper.bind(WebContext.getContext().getOwner()); // 設(shè)置開(kāi)啟采用密鑰加密(將默認(rèn)開(kāi)啟Base64編碼) _helper.allowUseAuthKey(); // 設(shè)置開(kāi)啟采用Base64編碼(默認(rèn)支持UrlEncode編碼) _helper.allowUseBase64(); // 添加或重設(shè)Cookie,過(guò)期時(shí)間基于Session時(shí)效 _helper.setCookie("current_username", "YMPer"); // 添加或重設(shè)Cookie,并指定過(guò)期時(shí)間 _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();