获取/清空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";

}

你可能感兴趣的:(获取/清空cookie)