From 5b5c031bddd1df451b675efe52cd6153017996cc Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Fri, 14 Mar 2025 19:48:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20sa-token-quick-login=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20SpringBoot3=20=E9=A1=B9=E7=9B=AE=E3=80=82=20Closes?= =?UTF-8?q?=20#IAFQNE=E3=80=82fix:=20#673?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mvn clean.bat | 1 + sa-token-demo/pom.xml | 1 + .../sa-token-demo-quick-login-sb3/pom.xml | 68 +++++++++++++++++++ .../src/main/java/com/pj/SaQuicikStartup.java | 52 ++++++++++++++ .../pj/SaTokenQuickSb3DemoApplication.java | 13 ++++ .../main/java/com/pj/test/TestController.java | 27 ++++++++ .../src/main/resources/application.yml | 31 +++++++++ sa-token-doc/start/download.md | 1 + sa-token-plugin/sa-token-quick-login/pom.xml | 13 ++-- .../dev33/satoken/quick/SaQuickRegister.java | 53 ++++++++------- .../satoken/quick/web/SaQuickController.java | 14 ++-- .../cn/dev33/satoken/solon/SaBeanInject.java | 14 ++++ .../dev33/satoken/filter/SaServletFilter.java | 2 +- 13 files changed, 252 insertions(+), 38 deletions(-) create mode 100644 sa-token-demo/sa-token-demo-quick-login-sb3/pom.xml create mode 100644 sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaQuicikStartup.java create mode 100644 sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaTokenQuickSb3DemoApplication.java create mode 100644 sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/test/TestController.java create mode 100644 sa-token-demo/sa-token-demo-quick-login-sb3/src/main/resources/application.yml diff --git a/mvn clean.bat b/mvn clean.bat index f263dc6f..1e78c72c 100644 --- a/mvn clean.bat +++ b/mvn clean.bat @@ -17,6 +17,7 @@ cd sa-token-demo-hutool-timed-cache & call mvn clean & cd .. cd sa-token-demo-caffeine & call mvn clean & cd .. cd sa-token-demo-jwt & call mvn clean & cd .. cd sa-token-demo-quick-login & call mvn clean & cd .. +cd sa-token-demo-quick-login-sb3 & call mvn clean & cd .. cd sa-token-demo-solon & call mvn clean & cd .. cd sa-token-demo-solon-redisson & call mvn clean & cd .. cd sa-token-demo-springboot & call mvn clean & cd .. diff --git a/sa-token-demo/pom.xml b/sa-token-demo/pom.xml index a452341e..c81b0099 100644 --- a/sa-token-demo/pom.xml +++ b/sa-token-demo/pom.xml @@ -27,6 +27,7 @@ sa-token-demo-oauth2/sa-token-demo-oauth2-server sa-token-demo-oauth2/sa-token-demo-oauth2-client sa-token-demo-quick-login + sa-token-demo-quick-login-sb3 sa-token-demo-remember-me/sa-token-demo-remember-me-server sa-token-demo-solon sa-token-demo-solon-redisson diff --git a/sa-token-demo/sa-token-demo-quick-login-sb3/pom.xml b/sa-token-demo/sa-token-demo-quick-login-sb3/pom.xml new file mode 100644 index 00000000..50b7eb97 --- /dev/null +++ b/sa-token-demo/sa-token-demo-quick-login-sb3/pom.xml @@ -0,0 +1,68 @@ + + 4.0.0 + cn.dev33 + sa-token-demo-quick-login-sb3 + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-parent + 3.4.3 + + + + + 1.40.0 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + cn.dev33 + sa-token-spring-boot3-starter + ${sa-token.version} + + + + + cn.dev33 + sa-token-quick-login + ${sa-token.version} + + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + org.springframework.boot + spring-boot-devtools + true + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaQuicikStartup.java b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaQuicikStartup.java new file mode 100644 index 00000000..ceaae649 --- /dev/null +++ b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaQuicikStartup.java @@ -0,0 +1,52 @@ +package com.pj; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import cn.dev33.satoken.quick.SaQuickManager; + +/** + * springboot启动之后 + * @author click33 + * + */ +@Component +public class SaQuicikStartup implements CommandLineRunner { + + @Value("${spring.application.name:sa-quick}") + private String applicationName; + + @Value("${server.port:8080}") + private String port; + + @Value("${server.servlet.context-path:}") + private String path; + +// @Value("${spring.profiles.active:}") +// private String active; + + @Override + public void run(String... args) throws Exception { + String str = "\n------------- " + applicationName + " 启动成功 (" + getNow() + ") -------------\n" + + " - home: " + "http://localhost:" + port + path + "\n" + + " - name: " + SaQuickManager.getConfig().getName() + "\n"+ + " - pwd : " + SaQuickManager.getConfig().getPwd() + "\n"; + System.out.println(str); + } + + + + /** + * 返回系统当前时间的YYYY-MM-dd hh:mm:ss 字符串格式 + */ + private static String getNow(){ + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); + } + + +} + diff --git a/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaTokenQuickSb3DemoApplication.java b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaTokenQuickSb3DemoApplication.java new file mode 100644 index 00000000..e62c3b34 --- /dev/null +++ b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/SaTokenQuickSb3DemoApplication.java @@ -0,0 +1,13 @@ +package com.pj; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SaTokenQuickSb3DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SaTokenQuickSb3DemoApplication.class, args); + } + +} \ No newline at end of file diff --git a/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/test/TestController.java b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/test/TestController.java new file mode 100644 index 00000000..b7a4d57a --- /dev/null +++ b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/java/com/pj/test/TestController.java @@ -0,0 +1,27 @@ +package com.pj.test; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import cn.dev33.satoken.util.SaTokenConsts; + +/** + * 测试专用Controller + * @author click33 + * + */ +@RestController +public class TestController { + + // 浏览器访问测试: http://localhost:8081 + @RequestMapping({"/"}) + public String index() { + String str = "
" +// + "

Welcome to the system

" + + "

资源页 (登录后才可进入本页面)

" + + "
" + + "

Sa-Token " + SaTokenConsts.VERSION_NO + "

"; + return str; + } + +} diff --git a/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/resources/application.yml b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/resources/application.yml new file mode 100644 index 00000000..8b111db3 --- /dev/null +++ b/sa-token-demo/sa-token-demo-quick-login-sb3/src/main/resources/application.yml @@ -0,0 +1,31 @@ +# 端口 +server: + port: 8081 + +# Sa-Token-Quick-Login 配置 +sa: + # 登录账号 + name: sa + # 登录密码 + pwd: 123456 + # 是否自动随机生成账号密码 (此项为true时, name与pwd失效) + auto: false + # 是否开启全局认证(关闭后将不再强行拦截) + auth: true + # 登录页标题 + title: Sa-Token 登录 + # 是否显示底部版权信息 + copr: true + # 指定拦截路径 + # include: /** + # 指定排除路径 + # exclude: /1.jpg + # 将本地磁盘的某个路径作为静态资源开放 + # dir: file:E:\static + + +# 静态文件路径映射 +spring: + resources: + static-locations: classpath:/META-INF/resources/,classpath:/resources/, classpath:/static/, classpath:/public/, ${sa.dir:} + \ No newline at end of file diff --git a/sa-token-doc/start/download.md b/sa-token-doc/start/download.md index 99b1bd08..b064e5d1 100644 --- a/sa-token-doc/start/download.md +++ b/sa-token-doc/start/download.md @@ -237,6 +237,7 @@ Maven依赖一直无法加载成功?[参考解决方案](https://sa-token.cc/d ├── sa-token-demo-oauth2-server // [示例] Sa-Token 集成 OAuth2.0 (服务端) ├── sa-token-demo-oauth2-server-h5 // [示例] Sa-Token 集成 OAuth2.0 (服务端 - 前后台分离示例) ├── sa-token-demo-quick-login // [示例] Sa-Token 集成 quick-login 模块 + ├── sa-token-demo-quick-login-sb3 // [示例] Sa-Token 集成 quick-login 模块 (SpringBoot3) ├── sa-token-demo-remember-me // [示例] Sa-Token 实现 [ 记住我 ] 模式 ├── page_project // [示例] Sa-Token 实现 [ 记住我 ] 模式、前端页面 ├── sa-token-demo-remember-me-server // [示例] Sa-Token 实现 [ 记住我 ] 模式、后端接口 diff --git a/sa-token-plugin/sa-token-quick-login/pom.xml b/sa-token-plugin/sa-token-quick-login/pom.xml index d303d8f4..a805af34 100644 --- a/sa-token-plugin/sa-token-quick-login/pom.xml +++ b/sa-token-plugin/sa-token-quick-login/pom.xml @@ -19,10 +19,15 @@ - cn.dev33 - sa-token-spring-boot-starter - true - + cn.dev33 + sa-token-spring-boot-starter + true + + + cn.dev33 + sa-token-spring-boot3-starter + true + diff --git a/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/SaQuickRegister.java b/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/SaQuickRegister.java index 5b1ccb13..a9a9b647 100644 --- a/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/SaQuickRegister.java +++ b/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/SaQuickRegister.java @@ -48,44 +48,45 @@ public class SaQuickRegister { */ @Bean @ConfigurationProperties(prefix = CONFIG_VERSION) - SaQuickConfig getSaQuickConfig() { + public SaQuickConfig getSaQuickConfig() { return new SaQuickConfig(); } + /** - * 注册 Sa-Token 全局过滤器 - * - * @return / + * 注册 Sa-Token 全局过滤器 + * + * @return / */ @Bean @Order(SaTokenConsts.ASSEMBLY_ORDER - 1) SaServletFilter getSaServletFilterForQuickLogin() { return new SaServletFilter() - // 拦截路由 - .addInclude("/**") + // 拦截路由 + .addInclude("/**") - // 排除掉登录相关接口,不需要鉴权的 - .addExclude("/favicon.ico", "/saLogin", "/doLogin", "/sa-res/**") + // 排除掉登录相关接口,不需要鉴权的 + .addExclude("/favicon.ico", "/saLogin", "/doLogin", "/sa-res/**") - // 认证函数: 每次请求执行 - .setAuth(obj -> { - SaRouter - .match(SaFoxUtil.convertStringToList(SaQuickManager.getConfig().getInclude())) - .notMatch(SaFoxUtil.convertStringToList(SaQuickManager.getConfig().getExclude())) - .check(r -> { - // 未登录时直接转发到login.html页面 - if (SaQuickManager.getConfig().getAuth() && ! StpUtil.isLogin()) { - SaHolder.getRequest().forward("/saLogin"); - SaRouter.back(); - } - }); - }). - - // 异常处理函数:每次认证函数发生异常时执行此函数 - setError(e -> { - return e.getMessage(); - }); + // 认证函数: 每次请求执行 + .setAuth(obj -> { + SaRouter + .match(SaFoxUtil.convertStringToList(SaQuickManager.getConfig().getInclude())) + .notMatch(SaFoxUtil.convertStringToList(SaQuickManager.getConfig().getExclude())) + .check(r -> { + // 未登录时直接转发到login.html页面 + if (SaQuickManager.getConfig().getAuth() && ! StpUtil.isLogin()) { + SaHolder.getRequest().forward("/saLogin"); + SaRouter.back(); + } + }); + }). + + // 异常处理函数:每次认证函数发生异常时执行此函数 + setError(e -> { + return e.getMessage(); + }); } } diff --git a/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/web/SaQuickController.java b/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/web/SaQuickController.java index 76fdc978..0799f3ca 100644 --- a/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/web/SaQuickController.java +++ b/sa-token-plugin/sa-token-quick-login/src/main/java/cn/dev33/satoken/quick/web/SaQuickController.java @@ -15,17 +15,17 @@ */ package cn.dev33.satoken.quick.web; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.ResponseBody; - import cn.dev33.satoken.quick.SaQuickManager; import cn.dev33.satoken.quick.config.SaQuickConfig; import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.util.SaFoxUtil; import cn.dev33.satoken.util.SaResult; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; /** * 登录Controller,处理登录相关请求 @@ -55,7 +55,7 @@ public class SaQuickController { */ @PostMapping("/doLogin") @ResponseBody - public SaResult doLogin(String name, String pwd) { + public SaResult doLogin(@RequestParam("name") String name, @RequestParam("pwd") String pwd) { // 参数完整性校验 if(SaFoxUtil.isEmpty(name) || SaFoxUtil.isEmpty(pwd)) { diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaBeanInject.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaBeanInject.java index c21c6ff6..8312e985 100644 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaBeanInject.java +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/SaBeanInject.java @@ -37,6 +37,8 @@ import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.strategy.SaAnnotationStrategy; +import cn.dev33.satoken.strategy.SaFirewallStrategy; +import cn.dev33.satoken.strategy.hooks.SaFirewallCheckHook; import cn.dev33.satoken.temp.SaTempInterface; import org.noear.solon.annotation.Bean; import org.noear.solon.annotation.Condition; @@ -236,4 +238,16 @@ public class SaBeanInject { StpUtil.setStpLogic(stpLogic); } + /** + * 注入自定义防火墙校验 hook 集合 + * + * @param hooks / + */ + @Bean + public void setSaFirewallCheckHooks(List hooks) { + for (SaFirewallCheckHook hook : hooks) { + SaFirewallStrategy.instance.registerHook(hook); + } + } + } diff --git a/sa-token-starter/sa-token-spring-boot3-starter/src/main/java/cn/dev33/satoken/filter/SaServletFilter.java b/sa-token-starter/sa-token-spring-boot3-starter/src/main/java/cn/dev33/satoken/filter/SaServletFilter.java index e634a097..b7dfb153 100644 --- a/sa-token-starter/sa-token-spring-boot3-starter/src/main/java/cn/dev33/satoken/filter/SaServletFilter.java +++ b/sa-token-starter/sa-token-spring-boot3-starter/src/main/java/cn/dev33/satoken/filter/SaServletFilter.java @@ -30,7 +30,7 @@ import java.util.Arrays; import java.util.List; /** - * Servlet 全局鉴权过滤器 + * Jakarta-Servlet 全局鉴权过滤器 *

* 默认优先级为 -100,尽量保证在其它过滤器之前执行 *