CookieUtils支持中文Cookie名称
This commit is contained in:
@@ -3,14 +3,11 @@
|
|||||||
*/
|
*/
|
||||||
package com.jeesite.common.web;
|
package com.jeesite.common.web;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLDecoder;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import com.jeesite.common.codec.EncodeUtils;
|
||||||
import com.jeesite.common.lang.StringUtils;
|
import com.jeesite.common.lang.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,14 +57,12 @@ public class CookieUtils {
|
|||||||
*/
|
*/
|
||||||
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
|
public static void setCookie(HttpServletResponse response, String name, String value, String path, int maxAge) {
|
||||||
if (StringUtils.isNotBlank(name)){
|
if (StringUtils.isNotBlank(name)){
|
||||||
|
name = EncodeUtils.encodeUrl(name);
|
||||||
|
value = EncodeUtils.encodeUrl(value);
|
||||||
Cookie cookie = new Cookie(name, null);
|
Cookie cookie = new Cookie(name, null);
|
||||||
cookie.setPath(path);
|
cookie.setPath(path);
|
||||||
cookie.setMaxAge(maxAge);
|
cookie.setMaxAge(maxAge);
|
||||||
try {
|
cookie.setValue(value);
|
||||||
cookie.setValue(URLEncoder.encode(value, "utf-8"));
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
response.addCookie(cookie);
|
response.addCookie(cookie);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,15 +108,12 @@ public class CookieUtils {
|
|||||||
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, String path, boolean isRemove) {
|
public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name, String path, boolean isRemove) {
|
||||||
String value = null;
|
String value = null;
|
||||||
if (StringUtils.isNotBlank(name)){
|
if (StringUtils.isNotBlank(name)){
|
||||||
|
name = EncodeUtils.encodeUrl(name);
|
||||||
Cookie[] cookies = request.getCookies();
|
Cookie[] cookies = request.getCookies();
|
||||||
if (cookies != null) {
|
if (cookies != null) {
|
||||||
for (Cookie cookie : cookies) {
|
for (Cookie cookie : cookies) {
|
||||||
if (cookie.getName().equals(name)) {
|
if (cookie.getName().equals(name)) {
|
||||||
try {
|
value = EncodeUtils.decodeUrl(cookie.getValue());
|
||||||
value = URLDecoder.decode(cookie.getValue(), "utf-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
if (isRemove && response != null) {
|
if (isRemove && response != null) {
|
||||||
cookie.setPath(path);
|
cookie.setPath(path);
|
||||||
cookie.setMaxAge(0);
|
cookie.setMaxAge(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user