From 3eaa6b9baf3d3fa7ba7c3aed114372a7e9d114c1 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Wed, 28 Aug 2024 00:28:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=89=8D=E7=AB=AF=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E5=90=8C=E5=90=8Dcookie=E6=97=B6=E7=9A=84=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E9=94=99=E8=AF=BB=E7=8E=B0=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../satoken/context/model/SaRequest.java | 14 +++++++ .../dubbo/model/SaRequestForDubbo.java | 20 ++++++++++ .../dubbo3/model/SaRequestForDubbo3.java | 20 ++++++++++ .../context/grpc/model/SaRequestForGrpc.java | 20 ++++++++++ .../servlet/model/SaRequestForServlet.java | 27 +++++++++++++ .../reactor/model/SaRequestForReactor.java | 38 +++++++++++++++++++ .../reactor/model/SaRequestForReactor.java | 38 +++++++++++++++++++ .../servlet/model/SaRequestForServlet.java | 27 +++++++++++++ .../solon/model/SaRequestForSolon.java | 35 ++++++++++++++++- 9 files changed, 237 insertions(+), 2 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/context/model/SaRequest.java b/sa-token-core/src/main/java/cn/dev33/satoken/context/model/SaRequest.java index 8a0b3d45..91c0f73f 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/context/model/SaRequest.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/context/model/SaRequest.java @@ -131,6 +131,20 @@ public interface SaRequest { */ String getCookieValue(String name); + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + * @param name 键 + * @return 值 + */ + String getCookieFirstValue(String name); + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + String getCookieLastValue(String name); + /** * 返回当前请求path (不包括上下文名称) * @return / diff --git a/sa-token-plugin/sa-token-dubbo/src/main/java/cn/dev33/satoken/context/dubbo/model/SaRequestForDubbo.java b/sa-token-plugin/sa-token-dubbo/src/main/java/cn/dev33/satoken/context/dubbo/model/SaRequestForDubbo.java index f411be02..5cebcd25 100644 --- a/sa-token-plugin/sa-token-dubbo/src/main/java/cn/dev33/satoken/context/dubbo/model/SaRequestForDubbo.java +++ b/sa-token-plugin/sa-token-dubbo/src/main/java/cn/dev33/satoken/context/dubbo/model/SaRequestForDubbo.java @@ -95,6 +95,26 @@ public class SaRequestForDubbo implements SaRequest { return null; } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ + // 不传播 cookie 参数 + return null; + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + // 不传播 cookie 参数 + return null; + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-plugin/sa-token-dubbo3/src/main/java/cn/dev33/satoken/context/dubbo3/model/SaRequestForDubbo3.java b/sa-token-plugin/sa-token-dubbo3/src/main/java/cn/dev33/satoken/context/dubbo3/model/SaRequestForDubbo3.java index 1200f1c0..73e6d2a1 100644 --- a/sa-token-plugin/sa-token-dubbo3/src/main/java/cn/dev33/satoken/context/dubbo3/model/SaRequestForDubbo3.java +++ b/sa-token-plugin/sa-token-dubbo3/src/main/java/cn/dev33/satoken/context/dubbo3/model/SaRequestForDubbo3.java @@ -95,6 +95,26 @@ public class SaRequestForDubbo3 implements SaRequest { return null; } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ + // 不传播 cookie 参数 + return null; + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + // 不传播 cookie 参数 + return null; + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-plugin/sa-token-grpc/src/main/java/cn/dev33/satoken/context/grpc/model/SaRequestForGrpc.java b/sa-token-plugin/sa-token-grpc/src/main/java/cn/dev33/satoken/context/grpc/model/SaRequestForGrpc.java index 184d8007..f6ca8a4d 100644 --- a/sa-token-plugin/sa-token-grpc/src/main/java/cn/dev33/satoken/context/grpc/model/SaRequestForGrpc.java +++ b/sa-token-plugin/sa-token-grpc/src/main/java/cn/dev33/satoken/context/grpc/model/SaRequestForGrpc.java @@ -82,6 +82,26 @@ public class SaRequestForGrpc implements SaRequest { return null; } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ + // 不传播 cookie 参数 + return null; + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + // 不传播 cookie 参数 + return null; + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-starter/sa-token-jakarta-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java b/sa-token-starter/sa-token-jakarta-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java index 57f7f216..cc677c3c 100644 --- a/sa-token-starter/sa-token-jakarta-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java +++ b/sa-token-starter/sa-token-jakarta-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java @@ -109,6 +109,14 @@ public class SaRequestForServlet implements SaRequest { */ @Override public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { @@ -120,6 +128,25 @@ public class SaRequestForServlet implements SaRequest { return null; } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + String value = null; + Cookie[] cookies = request.getCookies(); + if (cookies != null) { + for (Cookie cookie : cookies) { + if (cookie != null && name.equals(cookie.getName())) { + value = cookie.getValue(); + } + } + } + return value; + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java index 7b798cb9..b209afd9 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java @@ -101,6 +101,14 @@ public class SaRequestForReactor implements SaRequest { */ @Override public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ HttpCookie cookie = request.getCookies().getFirst(name); if(cookie == null) { return null; @@ -108,6 +116,36 @@ public class SaRequestForReactor implements SaRequest { return cookie.getValue(); } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + String value = null; + String cookieStr = getHeader("Cookie"); + if(SaFoxUtil.isNotEmpty(cookieStr)) { + String[] cookieItems = cookieStr.split(";"); + for (String item : cookieItems) { + String[] kv = item.split("="); + if (kv.length == 2) { + if (kv[0].trim().equals(name)) { + value = kv[1].trim(); + } + } + } + } + return value; + + // 此种写法无法获取到最后一个 Cookie,WebFlux 底层代码应该是有bug,前端提交多个同名Cookie时只能解析出第一个来 +// List cookies = request.getCookies().get(name); +// if(cookies.isEmpty()) { +// return null; +// } +// return cookies.get(cookies.size() - 1).getValue(); + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java index c8a5c588..e656a4b6 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java @@ -101,6 +101,14 @@ public class SaRequestForReactor implements SaRequest { */ @Override public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ HttpCookie cookie = request.getCookies().getFirst(name); if(cookie == null) { return null; @@ -108,6 +116,36 @@ public class SaRequestForReactor implements SaRequest { return cookie.getValue(); } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + String value = null; + String cookieStr = getHeader("Cookie"); + if(SaFoxUtil.isNotEmpty(cookieStr)) { + String[] cookieItems = cookieStr.split(";"); + for (String item : cookieItems) { + String[] kv = item.split("="); + if (kv.length == 2) { + if (kv[0].trim().equals(name)) { + value = kv[1].trim(); + } + } + } + } + return value; + + // 此种写法无法获取到最后一个 Cookie,WebFlux 底层代码应该是有bug,前端提交多个同名Cookie时只能解析出第一个来 + // List cookies = request.getCookies().get(name); + // if(cookies.isEmpty()) { + // return null; + // } + // return cookies.get(cookies.size() - 1).getValue(); + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-starter/sa-token-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java b/sa-token-starter/sa-token-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java index d1228b51..5088143c 100644 --- a/sa-token-starter/sa-token-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java +++ b/sa-token-starter/sa-token-servlet/src/main/java/cn/dev33/satoken/servlet/model/SaRequestForServlet.java @@ -109,6 +109,14 @@ public class SaRequestForServlet implements SaRequest { */ @Override public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { @@ -120,6 +128,25 @@ public class SaRequestForServlet implements SaRequest { return null; } + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + String value = null; + Cookie[] cookies = request.getCookies(); + if (cookies != null) { + for (Cookie cookie : cookies) { + if (cookie != null && name.equals(cookie.getName())) { + value = cookie.getValue(); + } + } + } + return value; + } + /** * 返回当前请求path (不包括上下文名称) */ diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/model/SaRequestForSolon.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/model/SaRequestForSolon.java index 3de3469a..2b820bff 100644 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/model/SaRequestForSolon.java +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/model/SaRequestForSolon.java @@ -68,8 +68,39 @@ public class SaRequestForSolon implements SaRequest { } @Override - public String getCookieValue(String s) { - return ctx.cookie(s); + public String getCookieValue(String name) { + return getCookieLastValue(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (第一个此名称的) + */ + @Override + public String getCookieFirstValue(String name){ + return ctx.cookie(name); + } + + /** + * 在 [ Cookie作用域 ] 里获取一个值 (最后一个此名称的) + * @param name 键 + * @return 值 + */ + @Override + public String getCookieLastValue(String name){ + String value = null; + String cookieStr = ctx.header("Cookie"); + if(SaFoxUtil.isNotEmpty(cookieStr)) { + String[] cookieItems = cookieStr.split(";"); + for (String item : cookieItems) { + String[] kv = item.split("="); + if (kv.length == 2) { + if (kv[0].trim().equals(name)) { + value = kv[1].trim(); + } + } + } + } + return value; } @Override