Cookie operations
The WebMVC module provides a small tool class called CookieHelper for the little dessert of Cookies, which supports the setting, reading and removal of Cookie parameters. It also supports encoding and encryption processing, and allows configuration parameters. Adjust Cookie Policy;
Cookie configuration parameters
#------------------------------------- # 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=
Sample code: Demonstrate Cookie operation
// 创建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();