CookieUtils.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
  3. */
  4. package com.ruoyi.common.utils.cookie;
  5. import javax.servlet.http.Cookie;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLEncoder;
  10. public class CookieUtils {
  11. /**
  12. * 设置 Cookie(生成时间为1天)
  13. * @param name 名称
  14. * @param value 值
  15. */
  16. public static void setCookie(HttpServletResponse response, String name, String value) {
  17. setCookie(response, name, value, 60*60*24);
  18. }
  19. /**
  20. * 设置 Cookie
  21. * @param name 名称
  22. * @param value 值
  23. */
  24. public static void setCookie(HttpServletResponse response, String name, String value, String path) {
  25. setCookie(response, name, value, path, 60*60*24);
  26. }
  27. /**
  28. * 设置 Cookie
  29. * @param name 名称
  30. * @param value 值
  31. * @param maxAge 生存时间(单位秒)
  32. */
  33. public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
  34. setCookie(response, name, value, "/", maxAge);
  35. }
  36. /**
  37. * 保存
  38. *
  39. * @param response
  40. * @param key
  41. * @param value
  42. * @param maxAge
  43. */
  44. public static void setCookie(HttpServletResponse response, String key, String value, String domain, String path, int maxAge, boolean isHttpOnly) {
  45. Cookie cookie = new Cookie(key, value);
  46. if (domain != null) {
  47. cookie.setDomain(domain);
  48. }
  49. cookie.setPath(path);
  50. cookie.setMaxAge(maxAge);
  51. cookie.setHttpOnly(isHttpOnly);
  52. response.addCookie(cookie);
  53. }
  54. /**
  55. * 设置 Cookie
  56. * @param name 名称
  57. * @param value 值
  58. * @param maxAge 生存时间(单位秒)
  59. * @param path 路径
  60. */
  61. public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
  62. Cookie cookie = new Cookie(name, null);
  63. cookie.setPath(path);
  64. cookie.setMaxAge(maxAge);
  65. try {
  66. cookie.setValue(URLEncoder.encode(value, "utf-8"));
  67. } catch (UnsupportedEncodingException e) {
  68. e.printStackTrace();
  69. }
  70. response.addCookie(cookie);
  71. }
  72. /**
  73. * 获得指定Cookie的值
  74. * @param name 名称
  75. * @return 值
  76. */
  77. public static String getCookie(HttpServletRequest request, String name) {
  78. Cookie cookie = get(request, name);
  79. if (cookie != null) {
  80. return cookie.getValue();
  81. }
  82. return null;
  83. }
  84. /**
  85. * 查询Cookie
  86. *
  87. * @param request
  88. * @param key
  89. */
  90. private static Cookie get(HttpServletRequest request, String key) {
  91. Cookie[] arr_cookie = request.getCookies();
  92. if (arr_cookie != null && arr_cookie.length > 0) {
  93. for (Cookie cookie : arr_cookie) {
  94. if (cookie.getName().equals(key)) {
  95. return cookie;
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. /**
  102. * 删除Cookie
  103. *
  104. * @param request
  105. * @param response
  106. * @param key
  107. */
  108. public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String key) {
  109. Cookie cookie = get(request, key);
  110. if (cookie != null) {
  111. setCookie(response,key,"","/",0);
  112. }
  113. }
  114. }