feat: sa-token-quick-login 支持 SpringBoot3 项目。 Closes #IAFQNE。fix: #673

This commit is contained in:
click33 2025-03-14 19:48:45 +08:00
parent a01eef8000
commit 5b5c031bdd
13 changed files with 252 additions and 38 deletions

View File

@ -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 ..

View File

@ -27,6 +27,7 @@
<module>sa-token-demo-oauth2/sa-token-demo-oauth2-server</module>
<module>sa-token-demo-oauth2/sa-token-demo-oauth2-client</module>
<module>sa-token-demo-quick-login</module>
<module>sa-token-demo-quick-login-sb3</module>
<module>sa-token-demo-remember-me/sa-token-demo-remember-me-server</module>
<module>sa-token-demo-solon</module>
<module>sa-token-demo-solon-redisson</module>

View File

@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-demo-quick-login-sb3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.3</version>
</parent>
<!-- 定义 Sa-Token 版本号 -->
<properties>
<sa-token.version>1.40.0</sa-token.version>
</properties>
<dependencies>
<!-- springboot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Sa-Token 权限认证, 在线文档https://sa-token.cc/ -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot3-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- quick-login -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-quick-login</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- @ConfigurationProperties -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 热更新插件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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());
}
}

View File

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

View File

@ -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 = "<br />"
// + "<h1 style='text-align: center;'>Welcome to the system</h1>"
+ "<h1 style='text-align: center;'>资源页 (登录后才可进入本页面) </h1>"
+ "<hr/>"
+ "<p style='text-align: center;'> Sa-Token " + SaTokenConsts.VERSION_NO + " </p>";
return str;
}
}

View File

@ -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:}

View File

@ -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 实现 [ 记住我 ] 模式、后端接口

View File

@ -19,10 +19,15 @@
<dependencies>
<!-- sa-token-spring-boot-starter -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot3-starter</artifactId>
<optional>true</optional>
</dependency>
<!-- 视图引擎 -->
<dependency>

View File

@ -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();
});
}
}

View File

@ -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)) {

View File

@ -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<SaFirewallCheckHook> hooks) {
for (SaFirewallCheckHook hook : hooks) {
SaFirewallStrategy.instance.registerHook(hook);
}
}
}

View File

@ -30,7 +30,7 @@ import java.util.Arrays;
import java.util.List;
/**
* Servlet 全局鉴权过滤器
* Jakarta-Servlet 全局鉴权过滤器
* <p>
* 默认优先级为 -100尽量保证在其它过滤器之前执行
* </p>