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); + } + +}