From 2ef879c75f00e59c131ad5ff0bf9dd0b297bed83 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Sat, 12 Apr 2025 16:54:23 +0800 Subject: [PATCH] =?UTF-8?q?demo:=20=E6=96=B0=E5=A2=9E=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=EF=BC=9A=E5=A6=82=E4=BD=95=E5=9C=A8=E5=93=8D=E5=BA=94=E5=BC=8F?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=8B=E7=9A=84=20Filter=20=E9=87=8C?= =?UTF-8?q?=E8=B0=83=E7=94=A8=20Sa-Token=20=E5=90=8C=E6=AD=A5=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/pj/satoken/MyFilter.java | 48 +++++++++++++++++++ .../main/java/com/pj/satoken/MyFilter.java | 48 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 sa-token-demo/sa-token-demo-webflux-springboot3/src/main/java/com/pj/satoken/MyFilter.java create mode 100644 sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/MyFilter.java diff --git a/sa-token-demo/sa-token-demo-webflux-springboot3/src/main/java/com/pj/satoken/MyFilter.java b/sa-token-demo/sa-token-demo-webflux-springboot3/src/main/java/com/pj/satoken/MyFilter.java new file mode 100644 index 00000000..130582a3 --- /dev/null +++ b/sa-token-demo/sa-token-demo-webflux-springboot3/src/main/java/com/pj/satoken/MyFilter.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pj.satoken; + +import cn.dev33.satoken.reactor.context.SaReactorSyncHolder; +import cn.dev33.satoken.stp.StpUtil; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +/** + * 自定义过滤器 + */ +@Component +public class MyFilter implements WebFilter { + + @Override + public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { + System.out.println("进入自定义过滤器"); + + try { + // 先 set 上下文,再调用 Sa-Token 同步 API,并在 finally 里清除上下文 + SaReactorSyncHolder.setContext(exchange); + System.out.println(StpUtil.isLogin()); + } + finally { + SaReactorSyncHolder.clearContext(); + } + + return chain.filter(exchange); + } + +} diff --git a/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/MyFilter.java b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/MyFilter.java new file mode 100644 index 00000000..130582a3 --- /dev/null +++ b/sa-token-demo/sa-token-demo-webflux/src/main/java/com/pj/satoken/MyFilter.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pj.satoken; + +import cn.dev33.satoken.reactor.context.SaReactorSyncHolder; +import cn.dev33.satoken.stp.StpUtil; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; +import reactor.core.publisher.Mono; + +/** + * 自定义过滤器 + */ +@Component +public class MyFilter implements WebFilter { + + @Override + public Mono filter(ServerWebExchange exchange, WebFilterChain chain) { + System.out.println("进入自定义过滤器"); + + try { + // 先 set 上下文,再调用 Sa-Token 同步 API,并在 finally 里清除上下文 + SaReactorSyncHolder.setContext(exchange); + System.out.println(StpUtil.isLogin()); + } + finally { + SaReactorSyncHolder.clearContext(); + } + + return chain.filter(exchange); + } + +}