获取/清空cookie

获取cookie
private String getCookie(HttpServletRequest req, String cookieName) {
String cookievalue = req.getHeader("Cookie");
if (ObjectUtils.isEmpty(cookievalue))
return "";

String[] cookies = cookievalue.split(";");
for (String cookie : cookies)
if (cookie.trim().contains(cookieName))
return cookie.trim().substring((cookieName + "=").length());
return "";
}


清空cookie
private void clearCookies(HttpServletRequest request, HttpServletResponse response, String[] cookieNames)
{
Cookie[] cookies = request.getCookies();
if (cookies == null)
return;
for (Cookie c : cookies)
if (ArrayUtils.contains(cookieNames, c.getName())) {
log.info("----------------【CasSecurityInterceptor >> clearCookies: request="+request+",cookieNames="+c.getName()+"】---------");
Cookie cookie = new Cookie(c.getName(), "");
cookie.setDomain(".paic.com.cn");
cookie.setPath("/");
cookie.setMaxAge(0);
cookie.setSecure(true);
response.addCookie(cookie);
}
}


//退出时候的清空cookie
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(ModelMap model, HttpServletRequest request,
HttpServletResponse response) {

HttpSession session = request.getSession(true);
if (session != null) {
session.invalidate();
}
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
if ((cookies[i].getName()).equals("SMSESSION")) {
Cookie cookie = new Cookie("SMSESSION", "");
cookie.setDomain(".paic.com.cn");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
if ((cookies[i].getName()).equals("PASESSION")) {
Cookie cookie = new Cookie("PASESSION", "");
cookie.setDomain(".paic.com.cn");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}

if ((cookies[i].getName()).equals("flag")) {
Cookie cookie = new Cookie("flag", "");
cookie.setDomain(".paic.com.cn");
cookie.setPath("/");
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}

return "logout";

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值