CookieUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package platform.common.util;
  2. import org.springframework.web.util.WebUtils;
  3. import javax.servlet.http.Cookie;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. /**
  7. * @author lhf
  8. */
  9. public class CookieUtil {
  10. /**
  11. * 添加cookie
  12. * @param httpServletResponse
  13. * @param name cookie的key
  14. * @param value cookie的value
  15. * @param secure 表示这个cookie仅在https环境下才能使用
  16. * @param maxAge 最长存活时间 单位为秒
  17. * @param domain
  18. */
  19. public static void create(HttpServletResponse httpServletResponse, String name, String value, Boolean secure, Integer maxAge, String domain, String path) {
  20. Cookie cookie = new Cookie(name, value);
  21. cookie.setHttpOnly(true);
  22. cookie.setSecure(secure);
  23. cookie.setMaxAge(maxAge);
  24. if(domain!=null){
  25. cookie.setDomain(domain);
  26. }
  27. cookie.setPath(path);
  28. httpServletResponse.addCookie(cookie);
  29. }
  30. /**
  31. * 添加cookie
  32. * @param httpServletResponse
  33. * @param name cookie的key
  34. * @param value cookie的value
  35. * @param secure 表示这个cookie仅在https环境下才能使用
  36. * @param maxAge 最长存活时间 单位为秒
  37. * @param domain
  38. */
  39. public static void createWebCookie(HttpServletResponse httpServletResponse, String name, String value, Integer maxAge) {
  40. Cookie cookie = new Cookie(name, value);
  41. cookie.setHttpOnly(false);
  42. cookie.setSecure(false);
  43. cookie.setMaxAge(maxAge);
  44. cookie.setPath("/");
  45. httpServletResponse.addCookie(cookie);
  46. }
  47. /**
  48. * 添加cookie
  49. * @param httpServletResponse
  50. * @param name cookie的key
  51. * @param value cookie的value
  52. * @param secure 表示这个cookie仅在https环境下才能使用
  53. * @param maxAge 最长存活时间 单位为秒
  54. * @param domain
  55. */
  56. public static void createWebCookie(HttpServletResponse httpServletResponse, String name, String value, Integer maxAge) {
  57. Cookie cookie = new Cookie(name, value);
  58. cookie.setHttpOnly(false);
  59. cookie.setSecure(false);
  60. cookie.setMaxAge(maxAge);
  61. cookie.setPath("/");
  62. httpServletResponse.addCookie(cookie);
  63. }
  64. /**
  65. * 往根下面存一个cookie
  66. * * @param name cookie的key
  67. * @param value cookie的value
  68. * @param value secure 表示这个cookie仅在https环境下才能使用
  69. * @param domain domain
  70. * @param maxAge 最长存活时间 单位为秒
  71. * @param response
  72. */
  73. public static void create(HttpServletResponse response, String name ,String value, Boolean secure, Integer maxAge, String domain){
  74. create(response, name, value, secure, maxAge, domain, "/");
  75. }
  76. /**
  77. * 清空所有cookie
  78. * @param httpServletResponse
  79. * @param name
  80. */
  81. public static void clear(HttpServletResponse httpServletResponse, String name) {
  82. Cookie cookie = new Cookie(name, null);
  83. cookie.setPath("/");
  84. cookie.setHttpOnly(true);
  85. cookie.setMaxAge(0);
  86. httpServletResponse.addCookie(cookie);
  87. }
  88. public static String getValue(HttpServletRequest httpServletRequest, String name) {
  89. Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
  90. return cookie != null ? cookie.getValue() : null;
  91. }
  92. /**
  93. * 刷新cookie
  94. * @param request
  95. * @param response
  96. * @param name
  97. * @param domain
  98. * @param maxAge
  99. */
  100. public static void refreshSessionCookie(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
  101. String name, String domain, int maxAge) {
  102. Cookie cookie = WebUtils.getCookie(httpServletRequest, name);
  103. if (cookie != null) {
  104. cookie.setMaxAge(maxAge);
  105. cookie.setDomain(domain);
  106. cookie.setPath("/");
  107. httpServletResponse.addCookie(cookie);
  108. }
  109. }
  110. }