Compare commits

...

47 Commits

Author SHA1 Message Date
shengzhang
921e693e7e v1.15.0.RELEASE 发布 2021-03-24 01:44:19 +08:00
shengzhang
a8117a8021 v1.15更新 2021-03-23 02:24:37 +08:00
shengzhang
c574e7f8d7 SaRouterUtil迁移到core核心包,优化依赖架构 2021-03-22 17:02:09 +08:00
shengzhang
36afaf74d3 token前缀校验改为强制模式,如果配置了前缀,则前端提交token时必须带有前缀 2021-03-22 00:04:05 +08:00
shengzhang
55b9c87d6a Session新增timeout操作API 2021-03-21 23:06:06 +08:00
shengzhang
50ffda7bef SaTokenManager新增stpLogicMap集合,记录所有StpLogic的初始化,方便查找 2021-03-20 16:51:18 +08:00
shengzhang
a122a8b083 StpUtil添加setTokenValue方法 2021-03-18 23:51:56 +08:00
shengzhang
69ce298a8a 适配token前缀模式 2021-03-18 13:06:24 +08:00
省长
f92e64b9cf update sa-token-doc/doc/use/jur-auth.md. 2021-03-16 17:27:13 +08:00
省长
d3138cefc8 !21 update sa-token-doc/doc/use/jur-auth.md.
Merge pull request !21 from AppleOfGray/N/A
2021-03-16 17:23:41 +08:00
AppleOfGray
71f2522f08 update sa-token-doc/doc/use/jur-auth.md. 2021-03-16 16:43:05 +08:00
省长
a5e9bd6714 !20 update sa-token-doc/doc/use/session.md.
Merge pull request !20 from AppleOfGray/N/A
2021-03-16 16:16:44 +08:00
AppleOfGray
27bb436a90 update sa-token-doc/doc/use/session.md. 2021-03-16 16:13:39 +08:00
省长
d75c91f0ab !19 update sa-token-doc/doc/use/many-account.md.
Merge pull request !19 from AppleOfGray/N/A
2021-03-16 15:56:38 +08:00
AppleOfGray
8ee9f215a6 update sa-token-doc/doc/use/many-account.md. 2021-03-16 15:55:36 +08:00
shengzhang
1df216879c 文档集成Redis章节新增redis配置示例说明,感谢群友 @-) 提供的建议 2021-03-16 14:44:38 +08:00
click33
91998af186 Merge pull request #48 from zhangzi0291/patch-1
修改cookie中没有path的问题
2021-03-16 00:36:34 +08:00
zhangzi0291
f33df38f64 修改cookie中没有path的问题
addCookies时如果path没有指定,path默认为/
2021-03-16 00:33:12 +08:00
shengzhang
aaf4bb8931 Session新增getString(key)方法,方便取值时类型转换 2021-03-15 17:05:07 +08:00
shengzhang
5808710bbf 修复错别字Request->Response 2021-03-14 14:39:21 +08:00
shengzhang
62787c3257 更改路由拦截示例 2021-03-14 00:04:11 +08:00
省长
6dd39505f9 增加友链 2021-03-13 17:28:03 +08:00
省长
d86f72b469 update README.md. 2021-03-13 17:25:41 +08:00
省长
7b7920123a update README.md. 2021-03-13 17:23:46 +08:00
shengzhang
8872982455 优化文档 2021-03-13 00:54:39 +08:00
shengzhang
80f07ae01e 技术栈 2021-03-13 00:47:43 +08:00
shengzhang
540730d9a4 优化文档 2021-03-12 09:51:37 +08:00
省长
03c8abb269 update README.md. 2021-03-12 09:46:11 +08:00
shengzhang
f9ad71db66 v1.14.0 更新 2021-03-12 01:45:13 +08:00
shengzhang
9fe0e1f97b 优化文档 2021-03-11 01:06:27 +08:00
shengzhang
9f6717f8c0 更换二维码地址 2021-03-10 14:17:43 +08:00
shengzhang
af2d04ca1f 更换二维码 2021-03-10 14:04:58 +08:00
省长
64e6b58a05 update README.md. 2021-03-10 14:01:56 +08:00
shengzhang
275b3cec6f 完成 [记住我]模式 开发文档 2021-03-10 02:37:45 +08:00
省长
e92ff9fb7d !18 update README.md.
Merge pull request !18 from ZhuBJ0510/N/A
2021-03-09 15:02:10 +08:00
ZhuBJ0510
cffd462cd8 update README.md. 2021-03-09 15:01:57 +08:00
省长
9ca2cad5e6 !17 update README.md.
Merge pull request !17 from ZhuBJ0510/N/A
2021-03-09 14:57:28 +08:00
ZhuBJ0510
a076c8f8fc update README.md. 2021-03-09 14:33:17 +08:00
省长
dbddd05a6e !16 update sa-token-doc/doc/use/route-check.md.
Merge pull request !16 from AppleOfGray/N/A
2021-03-09 10:00:17 +08:00
AppleOfGray
18760010fc update sa-token-doc/doc/use/route-check.md. 2021-03-09 09:59:39 +08:00
shengzhang
9151f9a5c8 优化文档 2021-03-09 00:29:57 +08:00
shengzhang
cdaf9fa65a Merge branch 'dev' of https://gitee.com/sz6/sa-token into dev 2021-03-08 23:27:38 +08:00
shengzhang
c316746f0d [密码加密]模块的文档 2021-03-08 23:27:08 +08:00
省长
0695ee6cdf update sa-token-doc/doc/use/many-account.md. 2021-03-08 09:01:42 +08:00
shengzhang
3716b874e3 新增密码加密算法工具类 2021-03-08 02:13:18 +08:00
shengzhang
255a15033e 集成Redis章节提供常见问题说明 2021-03-07 00:15:53 +08:00
省长
ad70bb0300 update sa-token-doc/doc/more/link.md.
增加 vue-next-admin 友链
2021-03-06 14:26:41 +08:00
69 changed files with 1653 additions and 599 deletions

View File

@@ -1,11 +1,11 @@
<p align="center">
<img alt="logo" src="https://gitee.com/sz6/sa-token/raw/master/sa-token-doc/doc/logo.png" width="150" height="150">
</p>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.13.0</h1>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.15.0.RELEASE</h1>
<h4 align="center">这可能是史上功能最全的Java权限认证框架</h4>
<h4 align="center">
<a href="https://gitee.com/sz6/sa-token/stargazers"><img src="https://gitee.com/sz6/sa-token/badge/star.svg"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.13.0-2B9939"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.15.0.RELEASE-2B9939"></a>
<a href="https://github.com/click33/sa-token/stargazers"><img src="https://img.shields.io/github/stars/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/watchers"><img src="https://img.shields.io/github/watchers/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/network/members"><img src="https://img.shields.io/github/forks/click33/sa-token"></a>
@@ -58,6 +58,8 @@ sa-token是一个轻量级Java权限认证框架主要解决登录认证
- **路由拦截式鉴权** —— 根据路由拦截鉴权可适配restful模式
- **自动续签** —— 提供两种token过期策略灵活搭配使用还可自动续签
- **会话治理** —— 提供方便灵活的会话查询接口
- **记住我模式** —— 适配[记住我]模式,重启浏览器免验证
- **密码加密** —— 提供密码加密模块可快速MD5、SHA1、SHA256、AES、RSA加密
- **组件自动注入** —— 零配置与Spring等框架集成
- **更多功能正在集成中...** —— 如有您有好想法或者建议,欢迎加群交流
@@ -87,7 +89,8 @@ StpUtil.checkLogin();
@SaCheckPermission("user:add")
@RequestMapping("/user/insert")
public String insert(SysUser user) {
return "用户增加";
// ...
return "用户增加";
}
```
@@ -179,16 +182,20 @@ sa-token秉承着开放的思想欢迎大家贡献代码为框架添砖加
## 友情链接
[**[ okhttps ]** 一个轻量级http通信框架API设计无比优雅支持 WebSocket 以及 Stomp 协议](https://gitee.com/ejlchina-zhxu/okhttps)
[**[ OkHttps ]** 一个轻量级http通信框架API设计无比优雅支持 WebSocket 以及 Stomp 协议](https://gitee.com/ejlchina-zhxu/okhttps)
[**[ 小诺快速开发平台 ]** 基于SpringBoot2 + AntDesignVue全新快速开发平台同时拥有三个版本](https://xiaonuo.vip/index#pricing)
## 交流群
QQ交流群[1002350610 点击加入](https://jq.qq.com/?_wv=1027&k=45H977HM)
![扫码加群](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/qq-group.png ':size=150')
![扫码加群](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/qq-group.png ':size=230')
**微信群**
**微信群**
![微信群](https://oss.dev33.cn/sa-token/sa-token-qr-wx.png "qr-wx.png")
![微信群](https://dev33-test.oss-cn-beijing.aliyuncs.com/sa-token/i-wx-qr.png ':size=230')
(扫码添加微信备注sa-token邀您加入群聊)
<br>

View File

@@ -8,7 +8,7 @@
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<packaging>pom</packaging>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
<!-- 项目介绍 -->
<name>sa-token</name>
@@ -23,7 +23,7 @@
<module>sa-token-dao-redis</module>
<module>sa-token-dao-redis-jackson</module>
<module>sa-token-spring-aop</module>
<module>sa-token-oauth2</module>
<!-- <module>sa-token-oauth2</module> -->
</modules>
<!-- 开源协议 apache 2.0 -->
@@ -38,7 +38,7 @@
<!-- 一些属性 -->
<properties>
<sa-token-version>1.13.0</sa-token-version>
<sa-token-version>1.15.0.RELEASE</sa-token-version>
<jdk.version>1.8</jdk.version>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>

View File

@@ -1,5 +1,8 @@
package cn.dev33.satoken;
import java.util.HashMap;
import java.util.Map;
import cn.dev33.satoken.action.SaTokenAction;
import cn.dev33.satoken.action.SaTokenActionDefaultImpl;
import cn.dev33.satoken.config.SaTokenConfig;
@@ -12,6 +15,7 @@ import cn.dev33.satoken.servlet.SaTokenServlet;
import cn.dev33.satoken.servlet.SaTokenServletDefaultImpl;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.util.SaTokenInsideUtil;
/**
@@ -21,129 +25,145 @@ import cn.dev33.satoken.util.SaTokenInsideUtil;
*/
public class SaTokenManager {
/**
* 配置文件 Bean
*/
private static SaTokenConfig config;
public static SaTokenConfig getConfig() {
if (config == null) {
initConfig();
}
return config;
}
public static void setConfig(SaTokenConfig config) {
SaTokenManager.config = config;
if(config.getIsV()) {
SaTokenInsideUtil.printSaToken();
}
}
public synchronized static void initConfig() {
public static SaTokenConfig getConfig() {
if (config == null) {
setConfig(SaTokenConfigFactory.createConfig());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (config == null) {
setConfig(SaTokenConfigFactory.createConfig());
}
}
}
return config;
}
/**
* 持久化 Bean
*/
public static SaTokenDao saTokenDao;
public static SaTokenDao getSaTokenDao() {
if (saTokenDao == null) {
initSaTokenDao();
}
return saTokenDao;
}
private static SaTokenDao saTokenDao;
public static void setSaTokenDao(SaTokenDao saTokenDao) {
if(SaTokenManager.saTokenDao != null && (SaTokenManager.saTokenDao instanceof SaTokenDaoDefaultImpl)) {
((SaTokenDaoDefaultImpl)SaTokenManager.saTokenDao).endRefreshTimer();
((SaTokenDaoDefaultImpl)SaTokenManager.saTokenDao).endRefreshThread();
}
SaTokenManager.saTokenDao = saTokenDao;
}
public synchronized static void initSaTokenDao() {
public static SaTokenDao getSaTokenDao() {
if (saTokenDao == null) {
setSaTokenDao(new SaTokenDaoDefaultImpl());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (saTokenDao == null) {
setSaTokenDao(new SaTokenDaoDefaultImpl());
}
}
}
return saTokenDao;
}
/**
* 权限认证 Bean
*/
public static StpInterface stpInterface;
public static StpInterface getStpInterface() {
if (stpInterface == null) {
initStpInterface();
}
return stpInterface;
}
private static StpInterface stpInterface;
public static void setStpInterface(StpInterface stpInterface) {
SaTokenManager.stpInterface = stpInterface;
}
public synchronized static void initStpInterface() {
public static StpInterface getStpInterface() {
if (stpInterface == null) {
setStpInterface(new StpInterfaceDefaultImpl());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (stpInterface == null) {
setStpInterface(new StpInterfaceDefaultImpl());
}
}
}
return stpInterface;
}
/**
* 框架行为 Bean
*/
public static SaTokenAction saTokenAction;
public static SaTokenAction getSaTokenAction() {
if (saTokenAction == null) {
initSaTokenAction();
}
return saTokenAction;
}
private static SaTokenAction saTokenAction;
public static void setSaTokenAction(SaTokenAction saTokenAction) {
SaTokenManager.saTokenAction = saTokenAction;
}
public synchronized static void initSaTokenAction() {
public static SaTokenAction getSaTokenAction() {
if (saTokenAction == null) {
setSaTokenAction(new SaTokenActionDefaultImpl());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (saTokenAction == null) {
setSaTokenAction(new SaTokenActionDefaultImpl());
}
}
}
return saTokenAction;
}
/**
* Cookie操作 Bean
*/
public static SaTokenCookie saTokenCookie;
public static SaTokenCookie getSaTokenCookie() {
if (saTokenCookie == null) {
initSaTokenCookie();
}
return saTokenCookie;
}
private static SaTokenCookie saTokenCookie;
public static void setSaTokenCookie(SaTokenCookie saTokenCookie) {
SaTokenManager.saTokenCookie = saTokenCookie;
}
public synchronized static void initSaTokenCookie() {
public static SaTokenCookie getSaTokenCookie() {
if (saTokenCookie == null) {
setSaTokenCookie(new SaTokenCookieDefaultImpl());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (saTokenCookie == null) {
setSaTokenCookie(new SaTokenCookieDefaultImpl());
}
}
}
return saTokenCookie;
}
/**
* Servlet操作 Bean
*/
public static SaTokenServlet saTokenServlet;
public static SaTokenServlet getSaTokenServlet() {
if (saTokenServlet == null) {
initSaTokenServlet();
}
return saTokenServlet;
}
private static SaTokenServlet saTokenServlet;
public static void setSaTokenServlet(SaTokenServlet saTokenServlet) {
SaTokenManager.saTokenServlet = saTokenServlet;
}
public synchronized static void initSaTokenServlet() {
public static SaTokenServlet getSaTokenServlet() {
if (saTokenServlet == null) {
setSaTokenServlet(new SaTokenServletDefaultImpl());
// 如果对象为空,则使用框架默认方式初始化
synchronized (SaTokenManager.class) {
if (saTokenServlet == null) {
setSaTokenServlet(new SaTokenServletDefaultImpl());
}
}
}
return saTokenServlet;
}
/**
* StpLogic集合, 记录框架所有成功初始化的StpLogic
*/
public static Map<String, StpLogic> stpLogicMap = new HashMap<String, StpLogic>();
/**
* 向集合中 put 一个 StpLogic
* @param stpLogic StpLogic
*/
public static void putStpLogic(StpLogic stpLogic) {
stpLogicMap.put(stpLogic.getLoginKey(), stpLogic);
}
/**
* 根据 LoginKey 获取对应的StpLogic如果不存在则返回null
* @param loginKey 对应的LoginKey
* @return 对应的StpLogic
*/
public static StpLogic getStpLogic(String loginKey) {
return stpLogicMap.get(loginKey);
}
}

View File

@@ -43,7 +43,7 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
return SaTokenInsideUtil.getRandomString(128);
}
// tik风格 (2_14_16)
if(SaTokenConsts.TOKEN_STYLE_RANDOM_TIK.equals(tokenStyle)) {
if(SaTokenConsts.TOKEN_STYLE_TIK.equals(tokenStyle)) {
return SaTokenInsideUtil.getRandomString(2) + "_" + SaTokenInsideUtil.getRandomString(14) + "_" + SaTokenInsideUtil.getRandomString(16) + "__";
}
// 默认还是uuid

View File

@@ -51,6 +51,9 @@ public class SaTokenConfig {
/** 写入Cookie时显式指定的作用域, 常用于单点登录二级域名共享Cookie的场景 */
private String cookieDomain;
/** token前缀, 格式样例(satoken: Bearer xxxx-xxxx-xxxx-xxxx) */
private String tokenPrefix;
/** 是否在初始化配置时打印版本字符画 */
private Boolean isV = true;
@@ -269,6 +272,22 @@ public class SaTokenConfig {
return this;
}
/**
* @return token前缀, 格式样例(satoken: Bearer xxxx-xxxx-xxxx-xxxx)
*/
public String getTokenPrefix() {
return tokenPrefix;
}
/**
* @param tokenPrefix token前缀, 格式样例(satoken: Bearer xxxx-xxxx-xxxx-xxxx)
* @return 对象自身
*/
public SaTokenConfig setTokenPrefix(String tokenPrefix) {
this.tokenPrefix = tokenPrefix;
return this;
}
/**
* @return 是否在初始化配置时打印版本字符画
*/
@@ -285,6 +304,7 @@ public class SaTokenConfig {
return this;
}
/**
* toString
@@ -295,8 +315,8 @@ public class SaTokenConfig {
+ ", allowConcurrentLogin=" + allowConcurrentLogin + ", isShare=" + isShare + ", isReadBody="
+ isReadBody + ", isReadHead=" + isReadHead + ", isReadCookie=" + isReadCookie + ", tokenStyle="
+ tokenStyle + ", dataRefreshPeriod=" + dataRefreshPeriod + ", tokenSessionCheckLogin="
+ tokenSessionCheckLogin + ", autoRenew=" + autoRenew + ", cookieDomain=" + cookieDomain + ", isV="
+ isV + "]";
+ tokenSessionCheckLogin + ", autoRenew=" + autoRenew + ", cookieDomain=" + cookieDomain
+ ", tokenPrefix=" + tokenPrefix + ", isV=" + isV + "]";
}

View File

@@ -44,7 +44,7 @@ public class SaTokenCookieUtil {
*/
public static void addCookie(HttpServletResponse response, String name, String value, String path, String domain, int timeout) {
Cookie cookie = new Cookie(name, value);
if(SaTokenInsideUtil.isEmpty(path) == false) {
if(SaTokenInsideUtil.isEmpty(path) == true) {
path = "/";
}
if(SaTokenInsideUtil.isEmpty(domain) == false) {
@@ -95,4 +95,4 @@ public class SaTokenCookieUtil {
}
}
}
}

View File

@@ -4,12 +4,9 @@ package cn.dev33.satoken.dao;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.concurrent.ConcurrentHashMap;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.util.SaTaskUtil;
import cn.dev33.satoken.util.SaTaskUtil.FunctionRunClass;
import cn.dev33.satoken.util.SaTokenInsideUtil;
/**
@@ -34,7 +31,7 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
* 构造函数
*/
public SaTokenDaoDefaultImpl() {
initRefreshTimer();
initRefreshThread();
}
@@ -166,9 +163,15 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
// --------------------- 定时清理过期数据
/**
* 定时任务对象
* 执行数据清理的线程
*/
public Timer refreshTimer;
public Thread refreshThread;
/**
* 是否继续执行数据清理的线程标记
*/
public boolean refreshFlag;
/**
* 清理所有已经过期的key
@@ -183,30 +186,46 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
/**
* 初始化定时任务
*/
public void initRefreshTimer() {
// 如果已经被初始化过了, 则停止它
if(this.refreshTimer != null) {
this.endRefreshTimer();
}
// 开始新的定时任务
if(SaTokenManager.getConfig().getDataRefreshPeriod() < 0) {
public void initRefreshThread() {
// 如果配置了<=0的值则不启动定时清理
if(SaTokenManager.getConfig().getDataRefreshPeriod() <= 0) {
return;
}
int period = SaTokenManager.getConfig().getDataRefreshPeriod() * 1000;
this.refreshTimer = SaTaskUtil.setInterval(new FunctionRunClass() {
@Override
public void run() {
refreshDataMap();
// 启动定时刷新
this.refreshFlag = true;
this.refreshThread = new Thread(() -> {
for (;;) {
try {
try {
// 如果已经被标记为结束
if(refreshFlag == false) {
return;
}
// 执行清理
refreshDataMap();
} catch (Exception e) {
e.printStackTrace();
}
// 休眠N秒
int dataRefreshPeriod = SaTokenManager.getConfig().getDataRefreshPeriod();
if(dataRefreshPeriod <= 0) {
dataRefreshPeriod = 1;
}
Thread.sleep(dataRefreshPeriod * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}, period, period);
});
refreshThread.start();
}
/**
* 结束定时任务
*/
public void endRefreshTimer() {
this.refreshTimer.cancel();
public void endRefreshThread() {
this.refreshFlag = false;
}

View File

@@ -23,14 +23,27 @@ public class IsRunFunction {
}
/**
* 根据变量决定是否执行此函数
*
* 当 isRun == true 时执行此函数
* @param function 函数
* @return 对象自身
*/
public void exe(SaFunction function) {
public IsRunFunction exe(SaFunction function) {
if (isRun) {
function.run();
}
return this;
}
/**
* 当 isRun == false 时执行此函数
* @param function 函数
* @return 对象自身
*/
public IsRunFunction noExe(SaFunction function) {
if (!isRun) {
function.run();
}
return this;
}
}

View File

@@ -1,4 +1,4 @@
package cn.dev33.satoken.interceptor;
package cn.dev33.satoken.router;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

View File

@@ -1,13 +1,9 @@
package cn.dev33.satoken.interceptor;
package cn.dev33.satoken.router;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.autowired.SaTokenSpringAutowired;
import cn.dev33.satoken.fun.IsRunFunction;
import cn.dev33.satoken.fun.SaFunction;
@@ -18,32 +14,6 @@ import cn.dev33.satoken.fun.SaFunction;
*/
public class SaRouterUtil {
/**
* 在进行路由匹配时所使用的 PathMatcher 对象
*/
private static PathMatcher pathMatcher;
/**
* @return 在进行路由匹配时所使用的的 PathMatcher 对象
*/
public static PathMatcher getPathMatcher() {
if(pathMatcher == null) {
pathMatcher = SaTokenSpringAutowired.pathMatcher;
if(pathMatcher == null) {
pathMatcher = new AntPathMatcher();
}
}
return pathMatcher;
}
/**
* @param pathMatcher 写入: 在进行路由匹配时所使用的的 PathMatcher 对象
*/
public static void setPathMatcher(PathMatcher pathMatcher) {
SaRouterUtil.pathMatcher = pathMatcher;
}
// -------------------- 路由匹配相关 --------------------
/**
@@ -53,10 +23,7 @@ public class SaRouterUtil {
* @return 是否匹配成功
*/
public static boolean isMatch(String pattern, String path) {
if(getPathMatcher().match(pattern, path)) {
return true;
}
return false;
return SaTokenManager.getSaTokenServlet().matchPath(pattern, path);
}
/**

View File

@@ -0,0 +1,61 @@
package cn.dev33.satoken.secure;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
/**
* Base64工具类
* @author kong
*
*/
public class SaBase64Util {
private static Base64.Encoder encoder = Base64.getEncoder();
private static Base64.Decoder decoder = Base64.getDecoder();
/**
* Base64编码byte[] 转 String
* @param bytes byte[]
* @return 字符串
*/
public static String encodeBytesToString(byte[] bytes){
return encoder.encodeToString(bytes);
}
/**
* Base64解码String 转 byte[]
* @param text 字符串
* @return byte[]
*/
public static byte[] decodeStringToBytes(String text){
return decoder.decode(text);
}
/**
* Base64编码String 转 String
* @param text 字符串
* @return Base64格式字符串
*/
public static String encode(String text){
try {
return encoder.encodeToString(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* Base64解码String 转 String
* @param base64Text Base64格式字符串
* @return 字符串
*/
public static String decode(String base64Text){
try {
return new String(decoder.decode(base64Text), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,442 @@
package cn.dev33.satoken.secure;
import java.security.InvalidParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import cn.dev33.satoken.exception.SaTokenException;
/**
* sa-token 常见加密算法工具类
*
* @author kong
*
*/
public class SaSecureUtil {
/**
* Base64编码
*/
private static Base64.Encoder encoder = Base64.getEncoder();
/**
* Base64解码
*/
private static Base64.Decoder decoder = Base64.getDecoder();
// ----------------------- 摘要加密 -----------------------
/**
* md5加密
* @param str 指定字符串
* @return 加密后的字符串
*/
public static String md5(String str) {
str = (str == null ? "" : str);
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] btInput = str.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char[] strA = new char[j * 2];
int k = 0;
for (byte byte0 : md) {
strA[k++] = hexDigits[byte0 >>> 4 & 0xf];
strA[k++] = hexDigits[byte0 & 0xf];
}
return new String(strA);
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* sha1加密
*
* @param str 指定字符串
* @return 加密后的字符串
*/
public static String sha1(String str) {
try {
str = (str == null ? "" : str);
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] b = str.getBytes();
md.update(b);
byte[] b2 = md.digest();
int len = b2.length;
String strA = "0123456789abcdef";
char[] ch = strA.toCharArray();
char[] chs = new char[len * 2];
for (int i = 0, k = 0; i < len; i++) {
byte b3 = b2[i];
chs[k++] = ch[b3 >>> 4 & 0xf];
chs[k++] = ch[b3 & 0xf];
}
return new String(chs);
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* sha256加密
*
* @param str 指定字符串
* @return 加密后的字符串
*/
public static String sha256(String str) {
try {
str = (str == null ? "" : str);
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
byte[] bytes = messageDigest.digest();
StringBuilder builder = new StringBuilder();
String temp;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
builder.append("0");
}
builder.append(temp);
}
return builder.toString();
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* md5加盐加密: md5(md5(str) + md5(salt))
* @param str 字符串
* @param salt 盐
* @return 加密后的字符串
*/
public static String md5BySalt(String str, String salt) {
return md5(md5(str) + md5(salt));
}
// ----------------------- 对称加密 AES -----------------------
/**
* 默认密码算法
*/
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
/**
* AES加密
*
* @param key 加密的密钥
* @param text 需要加密的字符串
* @return 返回Base64转码后的加密数据
*/
public static String aesEncrypt(String key, String text) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = text.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(byteContent);
return encoder.encodeToString(result);
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* AES解密
* @param key 加密的密钥
* @param text 已加密的密文
* @return 返回解密后的数据
*/
public static String aesDecrypt(String key, String text) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(decoder.decode(text));
return new String(result, "utf-8");
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* 生成加密秘钥
* @param password 秘钥
* @return SecretKeySpec
* @throws NoSuchAlgorithmException
*/
private static SecretKeySpec getSecretKey(final String password) throws NoSuchAlgorithmException {
KeyGenerator kg = KeyGenerator.getInstance("AES");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
kg.init(128, random);
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), "AES");
}
// ----------------------- 非对称加密 RSA -----------------------
private static final String ALGORITHM = "RSA";
private static final int KEY_SIZE = 1024;
// ---------- 5个常用方法
/**
* 生成密钥对
* @return Map对象 (private=私钥, public=公钥)
* @throws Exception 异常
*/
public static HashMap<String, String> rsaGenerateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
KeyPair keyPair;
try {
keyPairGenerator.initialize(KEY_SIZE,
new SecureRandom(UUID.randomUUID().toString().replaceAll("-", "").getBytes()));
keyPair = keyPairGenerator.generateKeyPair();
} catch (InvalidParameterException e) {
throw e;
} catch (NullPointerException e) {
throw e;
}
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
HashMap<String, String> map = new HashMap<String, String>(16);
map.put("private", encoder.encodeToString(rsaPrivateKey.getEncoded()));
map.put("public", encoder.encodeToString(rsaPublicKey.getEncoded()));
return map;
}
/**
* RSA公钥加密
* @param publicKeyString 公钥
* @param content 内容
* @return 加密后内容
*/
public static String rsaEncryptByPublic(String publicKeyString, String content) {
try {
// 获得公钥对象
PublicKey publicKey = getPublicKeyFromString(publicKeyString);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 该密钥能够加密的最大字节长度
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8 - 11;
byte[][] arrays = splitBytes(content.getBytes(), splitLength);
StringBuffer stringBuffer = new StringBuffer();
for (byte[] array : arrays) {
stringBuffer.append(bytesToHexString(cipher.doFinal(array)));
}
return stringBuffer.toString();
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* RSA私钥加密
* @param privateKeyString 私钥
* @param content 内容
* @return 加密后内容
*/
public static String rsaEncryptByPrivate(String privateKeyString, String content) {
try {
PrivateKey privateKey = getPrivateKeyFromString(privateKeyString);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
// 该密钥能够加密的最大字节长度
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8 - 11;
byte[][] arrays = splitBytes(content.getBytes(), splitLength);
StringBuffer stringBuffer = new StringBuffer();
for (byte[] array : arrays) {
stringBuffer.append(bytesToHexString(cipher.doFinal(array)));
}
return stringBuffer.toString();
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* RSA公钥解密
* @param publicKeyString 公钥
* @param content 已加密内容
* @return 解密后内容
*/
public static String rsaDecryptByPublic(String publicKeyString, String content) {
try {
PublicKey publicKey = getPublicKeyFromString(publicKeyString);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
// 该密钥能够加密的最大字节长度
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8;
byte[] contentBytes = hexStringToBytes(content);
byte[][] arrays = splitBytes(contentBytes, splitLength);
StringBuffer stringBuffer = new StringBuffer();
for (byte[] array : arrays) {
stringBuffer.append(new String(cipher.doFinal(array)));
}
return stringBuffer.toString();
} catch (Exception e) {
throw new SaTokenException(e);
}
}
/**
* RSA私钥解密
* @param privateKeyString 公钥
* @param content 已加密内容
* @return 解密后内容
*/
public static String rsaDecryptByPrivate(String privateKeyString, String content) {
try {
PrivateKey privateKey = getPrivateKeyFromString(privateKeyString);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 该密钥能够加密的最大字节长度
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8;
byte[] contentBytes = hexStringToBytes(content);
byte[][] arrays = splitBytes(contentBytes, splitLength);
StringBuffer stringBuffer = new StringBuffer();
for (byte[] array : arrays) {
stringBuffer.append(new String(cipher.doFinal(array)));
}
return stringBuffer.toString();
} catch (Exception e) {
throw new SaTokenException(e);
}
}
// ---------- 获取*钥
/** 根据公钥字符串获取 公钥对象 */
private static PublicKey getPublicKeyFromString(String key)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// 过滤掉\r\n
key = key.replace("\r\n", "");
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decoder.decode(key));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
return publicKey;
}
/** 根据私钥字符串获取 私钥对象 */
private static PrivateKey getPrivateKeyFromString(String key)
throws NoSuchAlgorithmException, InvalidKeySpecException {
// 过滤掉\r\n
key = key.replace("\r\n", "");
// 取得私钥
PKCS8EncodedKeySpec x509KeySpec = new PKCS8EncodedKeySpec(decoder.decode(key));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(x509KeySpec);
return privateKey;
}
// ---------- 一些辅助方法
/** 根据限定的每组字节长度,将字节数组分组 */
private static byte[][] splitBytes(byte[] bytes, int splitLength) {
// bytes与splitLength的余数
int remainder = bytes.length % splitLength;
// 数据拆分后的组数余数不为0时加1
int quotient = remainder != 0 ? bytes.length / splitLength + 1 : bytes.length / splitLength;
byte[][] arrays = new byte[quotient][];
byte[] array = null;
for (int i = 0; i < quotient; i++) {
// 如果是最后一组quotient-1,同时余数不等于0就将最后一组设置为remainder的长度
if (i == quotient - 1 && remainder != 0) {
array = new byte[remainder];
System.arraycopy(bytes, i * splitLength, array, 0, remainder);
} else {
array = new byte[splitLength];
System.arraycopy(bytes, i * splitLength, array, 0, splitLength);
}
arrays[i] = array;
}
return arrays;
}
/** 将字节数组转换成16进制字符串 */
private static String bytesToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer(bytes.length);
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(0xFF & bytes[i]);
if (temp.length() < 2) {
sb.append(0);
}
sb.append(temp);
}
return sb.toString();
}
/** 将16进制字符串转换成字节数组 */
private static byte[] hexStringToBytes(String hex) {
int len = (hex.length() / 2);
hex = hex.toUpperCase();
byte[] result = new byte[len];
char[] chars = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(chars[pos]) << 4 | toByte(chars[pos + 1]));
}
return result;
}
/** 将char转换为byte */
private static byte toByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}

View File

@@ -25,4 +25,12 @@ public interface SaTokenServlet {
*/
public HttpServletResponse getResponse();
/**
* 校验指定路由匹配符是否可以匹配成功指定路径
* @param pattern 路由匹配符
* @param path 需要匹配的路径
* @return 是否匹配成功
*/
public boolean matchPath(String pattern, String path);
}

View File

@@ -18,7 +18,7 @@ public class SaTokenServletDefaultImpl implements SaTokenServlet {
*/
@Override
public HttpServletRequest getRequest() {
throw new SaTokenException("请实现SaTokenServlet接口后进行Servlet相关操作");
throw new SaTokenException("SaTokenServlet接口未实现");
}
/**
@@ -26,7 +26,16 @@ public class SaTokenServletDefaultImpl implements SaTokenServlet {
*/
@Override
public HttpServletResponse getResponse() {
throw new SaTokenException("请实现SaTokenServlet接口后进行Servlet相关操作");
throw new SaTokenException("SaTokenServlet接口未实现");
}
/**
* 校验指定路由匹配符是否可以匹配成功指定路径
*/
@Override
public boolean matchPath(String pattern, String path) {
throw new SaTokenException("SaTokenServlet接口未实现");
}
}

View File

@@ -1,7 +1,5 @@
package cn.dev33.satoken.session;
import cn.dev33.satoken.SaTokenManager;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
@@ -9,6 +7,8 @@ import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import cn.dev33.satoken.SaTokenManager;
/**
* Session Model
*
@@ -236,6 +236,7 @@ public class SaSession implements Serializable {
this.update();
}
// ----------------------- 一些操作
/**
@@ -257,4 +258,255 @@ public class SaSession implements Serializable {
}
}
/**
* 获取此Session的剩余存活时间 (单位: 秒)
* @return 此Session的剩余存活时间 (单位: 秒)
*/
public long getTimeout() {
return SaTokenManager.getSaTokenDao().getSessionTimeout(this.id);
}
/**
* 修改此Session的剩余存活时间
* @param timeout 过期时间 (单位: 秒)
*/
public void updateTimeout(long timeout) {
SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, timeout);
}
/**
* 修改此Session的最小剩余存活时间 (只有在Session的过期时间低于指定的minTimeout时才会进行修改)
* @param minTimeout 过期时间 (单位: 秒)
*/
public void updateMinTimeout(long minTimeout) {
if(getTimeout() < minTimeout) {
SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, minTimeout);
}
}
/**
* 修改此Session的最大剩余存活时间 (只有在Session的过期时间高于指定的maxTimeout时才会进行修改)
* @param maxTimeout 过期时间 (单位: 秒)
*/
public void updateMaxTimeout(long maxTimeout) {
if(getTimeout() > maxTimeout) {
SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, maxTimeout);
}
}
// ----------------------- 存取值 (类型转换)
/**
* 写值
* @param key 名称
* @param value 值
*/
public void set(String key, Object value) {
dataMap.put(key, value);
update();
}
/**
* 写值(只有在此key原本无值的时候才会写入)
* @param key 名称
* @param value 值
*/
public void setDefaultValue(String key, Object value) {
if(has(key) == false) {
dataMap.put(key, value);
update();
}
}
/**
* 取值
* @param key key
* @return 值
*/
public Object get(String key) {
return dataMap.get(key);
}
/**
*
* 取值 (指定默认值)
* @param <T> 默认值的类型
* @param key key
* @param defaultValue 取不到值时返回的默认值
* @return 值
*/
public <T> T get(String key, T defaultValue) {
return getValueByDefaultValue(get(key), defaultValue);
}
/**
* 取值 (转String类型)
* @param key key
* @return 值
*/
public String getString(String key) {
Object value = get(key);
if(value == null) {
return null;
}
return String.valueOf(value);
}
/**
* 取值 (转int类型)
* @param key key
* @return 值
*/
public int getInt(String key) {
return getValueByDefaultValue(get(key), 0);
}
/**
* 取值 (转long类型)
* @param key key
* @return 值
*/
public long getLong(String key) {
return getValueByDefaultValue(get(key), 0L);
}
/**
* 取值 (转double类型)
* @param key key
* @return 值
*/
public double getDouble(String key) {
return getValueByDefaultValue(get(key), 0.0);
}
/**
* 取值 (转float类型)
* @param key key
* @return 值
*/
public float getFloat(String key) {
return getValueByDefaultValue(get(key), 0.0f);
}
/**
* 取值 (指定转换类型)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @return 值
*/
public <T> T getModel(String key, Class<T> cs) {
return getValueByClass(get(key), cs);
}
/**
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @param defaultValue 值为Null时返回的默认值
* @return 值
*/
@SuppressWarnings("unchecked")
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
Object value = get(key);
if(valueIsNull(value)) {
return (T)defaultValue;
}
return getValueByClass(value, cs);
}
/**
* 是否含有某个key
* @param key has
* @return 是否含有
*/
public boolean has(String key) {
return !valueIsNull(get(key));
}
/**
* 删值
* @param key 要删除的key
*/
public void delete(String key) {
dataMap.remove(key);
update();
}
// --------- 工具方法
/**
* 判断一个值是否为null
* @param value 指定值
* @return 此value是否为null
*/
public boolean valueIsNull(Object value) {
return value == null || value.equals("");
}
/**
* 将指定值转化为指定类型
* @param <T> 泛型
* @param obj 值
* @param cs 类型
* @return 转换后的值
*/
@SuppressWarnings("unchecked")
protected <T> T getValueByClass(Object obj, Class<T> cs) {
// 如果 obj 本来就是 cs 类型
if(obj != null && obj.getClass().equals(cs)) {
return (T)obj;
}
// 开始转换
String obj2 = String.valueOf(obj);
Object obj3 = null;
if (cs.equals(String.class)) {
obj3 = obj2;
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
obj3 = Integer.valueOf(obj2);
} else if (cs.equals(long.class) || cs.equals(Long.class)) {
obj3 = Long.valueOf(obj2);
} else if (cs.equals(short.class) || cs.equals(Short.class)) {
obj3 = Short.valueOf(obj2);
} else if (cs.equals(byte.class) || cs.equals(Byte.class)) {
obj3 = Byte.valueOf(obj2);
} else if (cs.equals(float.class) || cs.equals(Float.class)) {
obj3 = Float.valueOf(obj2);
} else if (cs.equals(double.class) || cs.equals(Double.class)) {
obj3 = Double.valueOf(obj2);
} else if (cs.equals(boolean.class) || cs.equals(Boolean.class)) {
obj3 = Boolean.valueOf(obj2);
} else {
obj3 = (T)obj;
}
return (T)obj3;
}
/**
* 根据默认值来获取值
* @param <T> 泛型
* @param value 值
* @param defaultValue 默认值
* @return 转换后的值
*/
@SuppressWarnings("unchecked")
protected <T> T getValueByDefaultValue(Object value, T defaultValue) {
// 如果 obj 为 null则直接返回默认值
if(valueIsNull(value)) {
return (T)defaultValue;
}
// 开始转换
Class<T> cs = (Class<T>) defaultValue.getClass();
return getValueByClass(value, cs);
}
}

View File

@@ -2,6 +2,7 @@ package cn.dev33.satoken.stp;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.util.SaTokenConsts;
/**
@@ -17,16 +18,16 @@ public class SaLoginModel {
*/
public String device;
/**
* 是否为持久Cookie临时Cookie在浏览器关闭时会自动删除持久Cookie在重新打开后依然存在
*/
public Boolean isLastingCookie;
/**
* 指定此次登录token的有效期, 单位:秒 如未指定自动取全局配置的timeout值
*/
public Long timeout;
/**
* 是否为临时Cookie临时Cookie会在浏览器关闭时自动删除
*/
public Boolean isTempCookie;
/**
* @return device
@@ -44,6 +45,22 @@ public class SaLoginModel {
return this;
}
/**
* @return isLastingCookie
*/
public Boolean getIsLastingCookie() {
return isLastingCookie;
}
/**
* @param isLastingCookie 要设置的 isLastingCookie
* @return 对象自身
*/
public SaLoginModel setIsLastingCookie(Boolean isLastingCookie) {
this.isLastingCookie = isLastingCookie;
return this;
}
/**
* @return timeout
*/
@@ -60,22 +77,21 @@ public class SaLoginModel {
return this;
}
/**
* @return isTempCookie
*/
public Boolean getIsTempCookie() {
return isTempCookie;
}
/**
* @param isTempCookie 要设置的 isTempCookie
* @return 对象自身
* @return cookie时长
*/
public SaLoginModel setIsTempCookie(Boolean isTempCookie) {
this.isTempCookie = isTempCookie;
return this;
public int getCookieTimeout() {
if(isLastingCookie == false) {
return -1;
}
if(timeout == SaTokenDao.NEVER_EXPIRE) {
return Integer.MAX_VALUE;
}
return (int)(long)timeout;
}
/**
* 构建对象,初始化默认值
* @return 对象自身
@@ -93,12 +109,12 @@ public class SaLoginModel {
if(device == null) {
device = SaTokenConsts.DEFAULT_LOGIN_DEVICE;
}
if(isLastingCookie == null) {
isLastingCookie = true;
}
if(timeout == null) {
timeout = config.getTimeout();
}
if(isTempCookie == null) {
isTempCookie = false;
}
return this;
}
@@ -112,4 +128,15 @@ public class SaLoginModel {
}
/**
* toString
*/
@Override
public String toString() {
return "SaLoginModel [device=" + device + ", isLastingCookie=" + isLastingCookie + ", timeout=" + timeout + "]";
}
}

View File

@@ -24,6 +24,7 @@ import cn.dev33.satoken.fun.SaFunction;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.TokenSign;
import cn.dev33.satoken.util.SaTokenConsts;
import cn.dev33.satoken.util.SaTokenInsideUtil;
/**
* sa-token 权限验证,逻辑实现类
@@ -44,6 +45,8 @@ public class StpLogic {
*/
public StpLogic(String loginKey) {
this.loginKey = loginKey;
// 在 SaTokenManager 中记录下此 StpLogic以便根据 LoginKey 进行查找此对象
SaTokenManager.putStpLogic(this);
}
/**
@@ -84,6 +87,32 @@ public class StpLogic {
return SaTokenManager.getSaTokenAction().createToken(loginId, loginKey);
}
/**
* 在当前会话写入当前tokenValue
* @param tokenValue token值
* @param cookieTimeout Cookie存活时间(秒)
*/
public void setTokenValue(String tokenValue, int cookieTimeout){
SaTokenConfig config = getConfig();
// 将token保存到本次request里
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
// 判断是否配置了token前缀
String tokenPrefix = config.getTokenPrefix();
if(SaTokenInsideUtil.isEmpty(tokenPrefix)) {
request.setAttribute(splicingKeyJustCreatedSave(), tokenValue);
} else {
// 如果配置了token前缀则拼接上前缀一起写入
request.setAttribute(splicingKeyJustCreatedSave(), tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT + tokenValue);
}
// 注入Cookie
if(config.getIsReadCookie() == true){
HttpServletResponse response = SaTokenManager.getSaTokenServlet().getResponse();
SaTokenManager.getSaTokenCookie().addCookie(response, getTokenName(), tokenValue,
"/", config.getCookieDomain(), cookieTimeout);
}
}
/**
* 获取当前tokenValue
* @return 当前tokenValue
@@ -115,7 +144,18 @@ public class StpLogic {
}
}
// 5. 返回
// 5. 如果打开了前缀模式
String tokenPrefix = getConfig().getTokenPrefix();
if(SaTokenInsideUtil.isEmpty(tokenPrefix) == false && SaTokenInsideUtil.isEmpty(tokenValue) == false) {
// 如果token以指定的前缀开头, 则裁剪掉它, 否则视为未提供token
if(tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT)) {
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
} else {
tokenValue = null;
}
}
// 6. 返回
return tokenValue;
}
@@ -161,10 +201,10 @@ public class StpLogic {
/**
* 在当前会话上登录id, 并指定登录设备
* @param loginId 登录id建议的类型long | int | String
* @param isTempCookie 是否为临时Cookie
* @param isLastingCookie 是否为持久Cookie
*/
public void setLoginId(Object loginId, boolean isTempCookie) {
setLoginId(loginId, new SaLoginModel().setIsTempCookie(isTempCookie));
public void setLoginId(Object loginId, boolean isLastingCookie) {
setLoginId(loginId, new SaLoginModel().setIsLastingCookie(isLastingCookie));
}
/**
@@ -175,7 +215,6 @@ public class StpLogic {
public void setLoginId(Object loginId, SaLoginModel loginModel) {
// ------ 1、获取相应对象
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
SaTokenConfig config = getConfig();
SaTokenDao dao = SaTokenManager.getSaTokenDao();
loginModel.build(config);
@@ -190,7 +229,7 @@ public class StpLogic {
}
} else {
// --- 如果不允许并发登录
// 如果此时[id-session]不为null说明此账号在其他地正在登录现在需要先把其它地的同设备token标记为被顶下线
// 如果此时[user-session]不为null说明此账号在其他地正在登录现在需要先把其它地的同设备token标记为被顶下线
SaSession session = getSessionByLoginId(loginId, false);
if(session != null) {
List<TokenSign> tokenSignList = session.getTokenSignList();
@@ -200,7 +239,7 @@ public class StpLogic {
dao.update(splicingKeyTokenValue(tokenSign.getValue()), NotLoginException.BE_REPLACED);
// 2. 清理掉[token-最后操作时间]
clearLastActivity(tokenSign.getValue());
// 3. 清理账号session上的token签名记录
// 3. 清理user-session上的token签名记录
session.removeTokenSign(tokenSign.getValue());
}
}
@@ -216,28 +255,20 @@ public class StpLogic {
if(session == null) {
session = getSessionByLoginId(loginId);
} else {
// 保证此Session的有效期 >= token的有效期
if(dao.getSessionTimeout(session.getId()) < loginModel.getTimeout()) {
dao.updateSessionTimeout(session.getId(), loginModel.getTimeout());
}
session.updateMinTimeout(loginModel.getTimeout());
}
// 在session上记录token签名
session.addTokenSign(new TokenSign(tokenValue, loginModel.getDevice()));
// ------ 4. 持久化其它数据
// token -> uid
dao.set(splicingKeyTokenValue(tokenValue), String.valueOf(loginId), loginModel.getTimeout());
// 将token保存到本次request里
request.setAttribute(splicingKeyJustCreatedSave(), tokenValue);
dao.set(splicingKeyTokenValue(tokenValue), String.valueOf(loginId), loginModel.getTimeout());
// 写入 [最后操作时间]
setLastActivityToNow(tokenValue);
// 注入Cookie
if(config.getIsReadCookie() == true){
HttpServletResponse response = SaTokenManager.getSaTokenServlet().getResponse();
int cookieTimeout = loginModel.getIsTempCookie() ? -1 : (int)(long)loginModel.getTimeout();
SaTokenManager.getSaTokenCookie().addCookie(response, getTokenName(), tokenValue,
"/", config.getCookieDomain(), cookieTimeout);
}
setLastActivityToNow(tokenValue);
// 在当前会话写入当前tokenValue
setTokenValue(tokenValue, loginModel.getCookieTimeout());
}
/**
@@ -578,15 +609,11 @@ public class StpLogic {
if(tokenValue == null || Objects.equals(tokenValue, "")) {
// 随机一个token送给Ta
tokenValue = createTokenValue(null);
// Request做上标记
SaTokenManager.getSaTokenServlet().getRequest().setAttribute(splicingKeyJustCreatedSave(), tokenValue);
// 写入 [最后操作时间]
setLastActivityToNow(tokenValue);
// cookie注入
if(getConfig().getIsReadCookie() == true){
SaTokenManager.getSaTokenCookie().addCookie(SaTokenManager.getSaTokenServlet().getResponse(), getTokenName(), tokenValue,
"/", getConfig().getCookieDomain(), (int)getConfig().getTimeout());
}
// 在当前会话写入这个tokenValue
int cookieTimeout = (int)(getConfig().getTimeout() == SaTokenDao.NEVER_EXPIRE ? Integer.MAX_VALUE : getConfig().getTimeout());
setTokenValue(tokenValue, cookieTimeout);
}
}
// 返回这个token对应的专属session
@@ -1092,7 +1119,7 @@ public class StpLogic {
* @return key
*/
public String splicingKeySwitch() {
return SaTokenConsts.SWITCH_TO_SAVE_KEY + getLoginKey();
return SaTokenConsts.SWITCH_TO_SAVE_KEY + loginKey;
}
/**
@@ -1100,7 +1127,7 @@ public class StpLogic {
* @return key
*/
public String splicingKeyJustCreatedSave() {
return SaTokenConsts.JUST_CREATED_SAVE_KEY + getLoginKey();
return SaTokenConsts.JUST_CREATED_SAVE_KEY + loginKey;
}

View File

@@ -36,6 +36,15 @@ public class StpUtil {
return stpLogic.getTokenName();
}
/**
* 在当前会话写入当前tokenValue
* @param tokenValue token值
* @param cookieTimeout Cookie存活时间(秒)
*/
public static void setTokenValue(String tokenValue, int cookieTimeout){
stpLogic.setTokenValue(tokenValue, cookieTimeout);
}
/**
* 获取当前tokenValue
* @return 当前tokenValue
@@ -75,10 +84,10 @@ public class StpUtil {
/**
* 在当前会话上登录id, 并指定登录设备
* @param loginId 登录id建议的类型long | int | String
* @param isTempCookie 是否为临时Cookie
* @param isLastingCookie 是否为持久Cookie
*/
public void setLoginId(Object loginId, boolean isTempCookie) {
stpLogic.setLoginId(loginId, isTempCookie);
public static void setLoginId(Object loginId, boolean isLastingCookie) {
stpLogic.setLoginId(loginId, isLastingCookie);
}
/**

View File

@@ -1,60 +0,0 @@
package cn.dev33.satoken.util;
import java.util.Timer;
import java.util.TimerTask;
/**
* 任务调度Util
* @author kong
*
*/
public class SaTaskUtil {
/**
* 延时指定毫秒执行一个函数
* @param fc 要执行的函数
* @param delay 延时的毫秒数
* @return timer任务对象
*/
public static Timer setTimeout(FunctionRunClass fc, int delay) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
fc.run();
timer.cancel();
}
}, delay);
return timer;
}
/**
* 延时delay毫秒每隔period毫秒执行一个函数
* @param fc 要执行的函数
* @param delay 延时的毫秒数
* @param period 每隔多少毫秒执行一次
* @return timer任务对象
*/
public static Timer setInterval(FunctionRunClass fc, int delay, int period) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
fc.run();
}
}, delay, period);
return timer;
}
/**
* 封装一个内部类,便于操作
* @author kong
*/
public static interface FunctionRunClass{
/**
* 要执行的方法
*/
public void run();
}
}

View File

@@ -13,7 +13,7 @@ public class SaTokenConsts {
/**
* sa-token 版本号
*/
public static final String VERSION_NO = "v1.13.0";
public static final String VERSION_NO = "v1.15.0.RELEASE";
/**
* sa-token 开源地址
@@ -42,8 +42,8 @@ public class SaTokenConsts {
* 常量key标记: 在进行临时身份切换时使用的key
*/
public static final String SWITCH_TO_SAVE_KEY = "SWITCH_TO_SAVE_KEY_";
// =================== token-style 相关 ===================
/**
@@ -74,7 +74,14 @@ public class SaTokenConsts {
/**
* token风格: tik风格 (2_14_16)
*/
public static final String TOKEN_STYLE_RANDOM_TIK = "tik";
public static final String TOKEN_STYLE_TIK = "tik";
// =================== 其它 ===================
/**
* 连接token前缀和token值的字符
*/
public static final String TOKEN_CONNECTOR_CHAT = " ";
}

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>

View File

@@ -16,7 +16,7 @@
<!-- 定义sa-token版本号 -->
<properties>
<sa-token-version>1.13.0</sa-token-version>
<sa-token-version>1.15.0.RELEASE</sa-token-version>
</properties>
<dependencies>

View File

@@ -16,6 +16,8 @@
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 定义sa-token版本号 -->
<sa-token-version>1.15.0.RELEASE</sa-token-version>
</properties>
<dependencies>
@@ -30,7 +32,7 @@
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.13.0</version>
<version>${sa-token-version}</version>
</dependency>
<!-- OkHttps网络请求库 http://okhttps.ejlchina.com/ -->

View File

@@ -16,6 +16,8 @@
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<!-- 定义sa-token版本号 -->
<sa-token-version>1.15.0.RELEASE</sa-token-version>
</properties>
<dependencies>
@@ -30,21 +32,21 @@
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.13.0</version>
<version>${sa-token-version}</version>
</dependency>
<!-- sa-token 实现 oauth2.0 -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-oauth2</artifactId>
<version>1.13.0-alpha</version>
<version>1.15.0-alpha</version>
</dependency>
<!-- sa-token整合redis (使用jackson序列化方式) -->
<!-- <dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>1.13.0</version>
<version>${sa-token-version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>

View File

@@ -16,7 +16,7 @@
<!-- 定义sa-token版本号 -->
<properties>
<sa-token-version>1.13.0</sa-token-version>
<sa-token-version>1.15.0.RELEASE</sa-token-version>
</properties>
<dependencies>

View File

@@ -1,5 +1,6 @@
package com.pj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@@ -4,29 +4,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
/**
* sa-token代码方式进行配置
* @author kong
*
*/
@Configuration
public class MySaTokenConfig implements WebMvcConfigurer {
// 获取配置Bean (以代码的方式配置sa-token, 此配置会覆盖yml中的配置 )
// @Primary
// @Bean(name="MySaTokenConfig")
public SaTokenConfig getSaTokenConfig() {
SaTokenConfig config = new SaTokenConfig();
config.setTokenName("satoken"); // token名称 (同时也是cookie名称)
config.setTimeout(30 * 24 * 60 * 60); // token有效期单位s 默认30天
config.setActivityTimeout(-1); // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
config.setAllowConcurrentLogin(true); // 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
config.setIsShare(true); // 在多人登录同一账号时是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
config.setTokenStyle("uuid"); // token风格
return config;
}
// 注册sa-token的拦截器打开注解式鉴权功能
@Override
public void addInterceptors(InterceptorRegistry registry) {
@@ -34,5 +21,4 @@ public class MySaTokenConfig implements WebMvcConfigurer {
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
}
}

View File

@@ -5,38 +5,38 @@ import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import com.pj.util.AjaxJson;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.stp.StpUtil;
/**
* 全局异常处理
*/
@RestControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin")
@ControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin")
public class GlobalException {
// 在当前类每个方法进入之前触发的操作
@ModelAttribute
public void get(HttpServletRequest request) throws IOException {
StpUtil.checkPermission("user:add");
}
// 全局异常拦截(拦截项目中的所有异常)
@ResponseBody
@ExceptionHandler
public AjaxJson handlerException(Exception e, HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 打印堆栈,以供调试
System.out.println("全局异常---------------");
e.printStackTrace();
// 不同异常返回不同状态码

View File

@@ -3,8 +3,6 @@ package com.pj.test;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@@ -238,17 +236,16 @@ public class TestController {
return AjaxJson.getSuccessData("登录成功");
}
// 测试 浏览器访问: http://localhost:8081/test/test
@RequestMapping("test")
public AjaxJson test(HttpServletResponse response) {
// StpUtil.getTokenSession().logout();
// StpUtil.logoutByLoginId(10001);
// StpUtil.setLoginId(10001);
// StpUtil.setLoginId(10001, new SaLoginModel().setIsTempCookie(true));
// StpUtil.getLoginId();
public AjaxJson test() {
System.out.println("进来了");
// StpUtil.setLoginId(10001, new SaLoginModel()
// .setDevice("PC") // 此次登录的客户端设备标识, 用于[同端互斥登录]时指定此次登录的设备名称
// .setIsLastingCookie(true) // 是否为持久Cookie临时Cookie在浏览器关闭时会自动删除持久Cookie在重新打开后依然存在
// .setTimeout(60 * 60 * 24 * 7) // 指定此次登录token的有效期, 单位:秒 如未指定自动取全局配置的timeout值
// );
return AjaxJson.getSuccess("访问成功");
}

View File

@@ -1,11 +1,11 @@
<p align="center">
<img alt="logo" src="https://gitee.com/sz6/sa-token/raw/master/sa-token-doc/doc/logo.png" width="150" height="150">
</p>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.13.0</h1>
<h1 align="center" style="margin: 30px 0 30px; font-weight: bold;">sa-token v1.15.0.RELEASE</h1>
<h4 align="center">这可能是史上功能最全的Java权限认证框架</h4>
<h4 align="center">
<a href="https://gitee.com/sz6/sa-token/stargazers"><img src="https://gitee.com/sz6/sa-token/badge/star.svg"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.13.0-2B9939"></a>
<a href="https://github.com/click33/sa-token"><img src="https://img.shields.io/badge/sa--token-v1.15.0.RELEASE-2B9939"></a>
<a href="https://github.com/click33/sa-token/stargazers"><img src="https://img.shields.io/github/stars/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/watchers"><img src="https://img.shields.io/github/watchers/click33/sa-token"></a>
<a href="https://github.com/click33/sa-token/network/members"><img src="https://img.shields.io/github/forks/click33/sa-token"></a>
@@ -55,9 +55,11 @@ sa-token是一个轻量级Java权限认证框架主要解决登录认证
- **多账号认证体系** —— 比如一个商城项目的user表和admin表分开鉴权
- **花式token生成** —— 内置六种token风格还可自定义token生成策略
- **注解式鉴权** —— 优雅的将鉴权与业务代码分离
- **路由拦截式鉴权** —— 设定全局路由拦截,并排除指定路由
- **路由拦截式鉴权** —— 根据路由拦截鉴权可适配restful模式
- **自动续签** —— 提供两种token过期策略灵活搭配使用还可自动续签
- **会话治理** —— 提供方便灵活的会话查询接口
- **记住我模式** —— 适配[记住我]模式,重启浏览器免验证
- **密码加密** —— 提供密码加密模块可快速MD5、SHA1、SHA256、AES、RSA加密
- **组件自动注入** —— 零配置与Spring等框架集成
- **更多功能正在集成中...** —— 如有您有好想法或者建议,欢迎加群交流
@@ -87,7 +89,8 @@ StpUtil.checkLogin();
@SaCheckPermission("user:add")
@RequestMapping("/user/insert")
public String insert(SysUser user) {
return "用户增加";
// ...
return "用户增加";
}
```
@@ -181,15 +184,17 @@ sa-token秉承着开放的思想欢迎大家贡献代码为框架添砖加
## 友情链接
[**[ okhttps ]** 一个轻量级http通信框架API设计无比优雅支持 WebSocket 以及 Stomp 协议](https://gitee.com/ejlchina-zhxu/okhttps)
[**[ 小诺快速开发平台 ]** 基于SpringBoot2 + AntDesignVue全新快速开发平台同时拥有三个版本](https://xiaonuo.vip/index#pricing)
## 交流群
QQ交流群[1002350610 点击加入](https://jq.qq.com/?_wv=1027&k=45H977HM)
![扫码加群](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/qq-group.png ':size=150')
![扫码加群](https://color-test.oss-cn-qingdao.aliyuncs.com/sa-token/qq-group.png ':size=230')
**微信群**
**微信群**
![微信群](https://oss.dev33.cn/sa-token/sa-token-qr-wx.png "qr-wx.png")
![微信群](https://dev33-test.oss-cn-beijing.aliyuncs.com/sa-token/i-wx-qr.png ':size=230')
(扫码添加微信备注sa-token邀您加入群聊)
<br>

View File

@@ -17,9 +17,11 @@
- [注解式鉴权](/use/at-check)
- [路由拦截式鉴权](/use/route-check)
- [花式token](/use/token-style)
- [Token前缀](/use/token-prefix)
- [框架配置](/use/config)
- [会话治理](/use/search-session)
<!-- - [记住我模式](/use/remember-me) -->
- [记住我模式](/use/remember-me)
- [密码加密](/use/password-secure)
- **进阶**
- [集群、分布式](/senior/dcs)
@@ -34,7 +36,9 @@
- **附录**
- [未登录场景值](/fun/not-login-scene)
- [token有效期详解](/fun/token-timeout)
- [Session模型详解](/fun/session-model)
- [TokenInfo参数详解](/fun/token-info)
- [框架源码所有技术栈](/fun/tech-stack)

View File

@@ -0,0 +1,21 @@
# Session模型详解
---
`sa-token`中, `Session` 分为三种, 分别是:
- `User-Session`: 指的是框架为每个`loginId`分配的`Session`
- `Token-Session`: 指的是框架为每个`token`分配的`Session`
- `自定义Session`: 指的是以一个`特定的值`作为SessionId来分配的`Session`
User-Session和Token-Session到底有什么不同下面这张图可以解释两者的区别
![../static/session-model.png](../static/session-model.png)
简而言之:
- `Token-Session` 以token为主只要token不同那么对应的Session对象就不同
- `User-Session` 以UserId为主只要token指向的UserId一致那么对应的Session对象就一致
- `自定义Session` 以特定的key为主不同key对应不同的Session对象

View File

@@ -0,0 +1,15 @@
# sa-token 源码用到的所有技术栈
包括但不限于以下:
- Maven多模块项目
- Servlet API、临时Cookie与永久Cookie、Request参数获取
- SpringBoot2.0、Redis、Jackson、Hutool、jwt
- SpringBoot自定义starter、Spring包扫码 + 依赖注入、AOP注解切面、yml配置映射、拦截器
- Java8 接口与default实现、静态方法、枚举、定时器、异常类、泛型、反射、IO流、自定义注解、Lambda表达式、函数式编程
- package-info注释、Serializable序列化接口、synchronized锁
- java加密算法MD5、SHA1、SHA256、AES、RSA
- OAuth2.0、同域单点登录、集群与分布式、路由Ant匹配

View File

@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>sa-token</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="sa-token是一个java权限认证框架功能全面上手简单登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、Spring集成...零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
<meta name="description" content="sa-token是一个java权限认证框架功能全面上手简单登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、记住我模式、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、密码加密、jwt集成、Spring集成...有了sa-token你所有的权限认证问题都不再是问题">
<meta name="keywords" content="sa-token,sa-token框架,sa-token文档,java权限认证">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="logo.png">
@@ -21,6 +21,8 @@
<nav>
<select onchange="location.href=this.value">
<option value="http://sa-token.dev33.cn/doc/index.html">最新版</option>
<option value="http://sa-token.dev33.cn/v/v1.14.0/doc/index.html">v1.14.0</option>
<option value="http://sa-token.dev33.cn/v/v1.13.0/doc/index.html">v1.13.0</option>
<option value="http://sa-token.dev33.cn/v/v1.12.1/doc/index.html">v1.12.1</option>
<option value="http://sa-token.dev33.cn/v/v1.12.0/doc/index.html">v1.12.0</option>
<option value="http://sa-token.dev33.cn/v/v1.11.0/doc/index.html">v1.11.0</option>
@@ -42,7 +44,7 @@
</div>
<script>
var name = '<img style="width: 50px; height: 50px; vertical-align: middle;" src="logo.png" alt="logo" /> ';
name += '<b style="font-size: 24px; vertical-align: middle;">sa-token</b> <sub>v1.13.0</sub>'
name += '<b style="font-size: 24px; vertical-align: middle;">sa-token</b> <sub>v1.15.0.RELEASE</sub>'
window.$docsify = {
name: name, // 名字
repo: 'https://github.com/click33/sa-token', // github地址

View File

@@ -17,6 +17,9 @@
[[sa-admin] - 一个多窗口后台模板,流畅、易上手、提高生产力](https://gitee.com/ejlchina-zhxu/okhttps)
[[vue-next-admin] - 一套为开发者快速开发准备的基于 vue2.x 越看越精彩的后台管理系统一站式平台模板](https://gitee.com/lyt-top/vue-admin-wonderful)
[[小诺快速开发平台] - 基于SpringBoot2 + AntDesignVue全新快速开发平台同时拥有三个版本](https://xiaonuo.vip/index#pricing)
<br>
虚位以待...

View File

@@ -1,6 +1,44 @@
# 更新日志
### 2021-3-23 @v1.15.0
- 新增:文档添加源码涉及技术栈说明
- 优化:优化路由拦截器模块文档,更简洁的示例
- 修复修复非web环境下的错误提示Request->Response
- 修复修复Cookie注入时path判断错误感谢@zhangzi0291提供的PR
- 新增文档集成Redis章节新增redis配置示例说明感谢群友 `@-)` 提供的建议
- 新增增加token前缀模式可在配置token读取前缀适配`Bearer token`规范 **[重要]**
- 优化:`SaTokenManager`初始化Bean去除`initXxx`方法,优化代码逻辑
- 新增:`SaTokenManager`新增`stpLogicMap`集合,记录所有`StpLogic`的初始化,方便查找
- 新增:`Session`新增timeout操作API可灵活修改Session的剩余有效时间
- 新增token前缀改为强制校验模式如果配置了前缀则前端提交token时必须带有
- 优化:精简`SaRouteInterceptor`,只保留自定义验证和默认的登陆验证,去除冗余功能
- 优化:`SaRouterUtil`迁移到core核心包优化依赖架构
- 优化默认Dao实现类里`Timer定时器`改为子线程 + sleep 模拟
- 新增:`Session`新增各种类型转换API可快速方便存取值 **[重要]**
- 升级注意:
- `SaRouterUtil`类迁移到核心包注意更换import地址
- `SaRouteInterceptor`去出冗余API详情参考路由鉴权部分
### 2021-3-12 @v1.14.0
- 新增:新增`SaLoginModel`登录参数Model适配 [记住我] 模式 **[重要]**
- 新增:新增 `StpUtil.setLoginId()` 时指定token有效期可灵活控制用户的一次登录免验证时长
- 新增新增Cookie时间判断`timeout`设置为-1时`Cookie`有效期将为`Integer.MAX_VALUE` **[重要]**
- 新增新增密码加密工具类可快速MD5、SHA1、SHA256、AES、RSA加密 **[重要]**
- 新增:新增 OAuth2.0 模块 **[重要]**
- 新增:`SaTokenConfig`配置类所有set方法支持链式调用
- 新增:`SaOAuth2Config` sa-token oauth2 配置类所有set方法新增支持链式调用
- 优化:`StpLogic`类所有`getKey`方法重名为`splicingKey`,更语义化的函数名称
- 新增:`IsRunFunction`新增`noExe`函数,用于指定当`isRun`值为`false`时执行的函数
- 新增:`SaSession`新增数据存取值操作API
- 优化:优化`SaTokenDao`接口增加Object操作API
- 优化jwt示例`createToken`方法去除默认秘钥判断,只在启动项目时打印警告
- 文档:常见问题新增示例(修改密码后如何立即掉线)
- 文档:权限认证文档新增[如何把权限精确搭到按钮级]示例说明
- 文档:优化文档,部分模块添加图片说明
### 2021-2-9 @v1.13.0
- 优化:优化源码注释与文档
- 新增文档集成Gitalk评论系统

View File

@@ -9,14 +9,14 @@
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</dependency>
```
## Gradle依赖
Gradle用户引入依赖
```
implementation 'cn.dev33:sa-token-spring-boot-starter:1.13.0'
implementation 'cn.dev33:sa-token-spring-boot-starter:1.15.0.RELEASE'
```
@@ -34,8 +34,12 @@ implementation 'cn.dev33:sa-token-spring-boot-starter:1.13.0'
├── sa-token-dao-redis // sa-token整合redis (使用jdk默认序列化方式)
├── sa-token-dao-redis-jackson // sa-token整合redis (使用jackson序列化方式)
├── sa-token-spring-aop // sa-token整合SpringAOP 注解鉴权
├── sa-token-oauth2 // sa-token实现 OAuth2.0 模块(内测暂未发布)
├── sa-token-demo-springboot // sa-token示例
├── sa-token-demo-jwt // sa-token集成jwt示例
├── sa-token-demo-oauth2 // sa-token集成OAuth2.0模块示例
├── sa-token-demo-oauth2-client // OAuth2.0 客户端
├── sa-token-demo-oauth2-server // OAuth2.0 服务端
├── sa-token-doc // sa-token开发文档
├──pom.xml
```
@@ -46,7 +50,7 @@ implementation 'cn.dev33:sa-token-spring-boot-starter:1.13.0'
## jar包下载
[点击下载sa-token-1.6.0.jar](https://oss.dev33.cn/sa-token/sa-token-1.6.0.jar)
(注意:当前仅提供`v1.6.0`版本jar包下载更多版本请前往maven中央仓库获取)
(注意:当前仅提供`v1.6.0`版本jar包下载更多版本请前往maven中央仓库获取[直达链接](https://search.maven.org/search?q=sa-token))

View File

@@ -19,7 +19,7 @@
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</dependency>
```

BIN
sa-token-doc/doc/static/kickout.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
sa-token-doc/doc/static/not-jur.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
sa-token-doc/doc/static/not-login.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@@ -21,7 +21,7 @@ sa-token内置两种模式完成注解鉴权分别是`AOP模式`和`拦截器
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-aop</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</dependency>
```

View File

@@ -71,4 +71,5 @@ public class MySaTokenConfig {
| dataRefreshPeriod | int | 30 | 默认dao层实现类中每次清理过期数据间隔的时间 (单位: 秒) 默认值30秒设置为-1代表不启动定时清理 |
| tokenSessionCheckLogin | Boolean | true | 获取token专属session时是否必须登录 (如果配置为true会在每次获取token专属session时校验是否登录) |
| autoRenew | Boolean | true | 是否打开自动续签 (如果此值为true, 框架会在每次直接或间接调用getLoginId()时进行一次过期检查与续签操作) |
| tokenPrefix | Boolean | true | token前缀, 格式样例(satoken: Bearer xxxx-xxxx-xxxx-xxxx) [参考token前缀](/use/token-prefix) |
| isV | Boolean | true | 是否在初始化配置时打印版本字符画 |

View File

@@ -9,13 +9,12 @@ Sa-token默认将会话数据保存在内存中此模式读写速度最快
### 1. sa-token 整合 Redis (使用jdk默认序列化方式)
(注意: 整合Redis只需要引入对应的pom依赖即可所有上层API保持不变)
``` xml
<!-- sa-token整合redis (使用jdk默认序列化方式) -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</dependency>
```
优点兼容性好缺点Session序列化后基本不可读对开发者来讲等同于乱码
@@ -27,7 +26,7 @@ Sa-token默认将会话数据保存在内存中此模式读写速度最快
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</dependency>
```
优点Session序列化后可读性强可灵活手动修改缺点兼容性稍差
@@ -35,7 +34,10 @@ Sa-token默认将会话数据保存在内存中此模式读写速度最快
<br>
**请注意无论使用哪种序列化方式你都必须为项目提供一个Redis实例化方案例如**
### 集成Redis请注意
**1. 无论使用哪种序列化方式你都必须为项目提供一个Redis实例化方案例如**
``` xml
<!-- 提供redis连接池 -->
<dependency>
@@ -44,6 +46,38 @@ Sa-token默认将会话数据保存在内存中此模式读写速度最快
</dependency>
```
**2. 引入了依赖我还需要为Redis配置连接信息吗** <br>
需要只有项目初始化了正确的Redis实例`sa-token`才可以使用Redis进行数据持久化参考以下`yml配置`
``` java
# 端口
spring:
# redis配置
redis:
# Redis数据库索引默认为0
database: 1
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
# password:
# 连接超时时间(毫秒)
timeout: 1000ms
lettuce:
pool:
# 连接池最大连接数
max-active: 200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
# 连接池中的最大空闲连接
max-idle: 10
# 连接池中的最小空闲连接
min-idle: 0
```
**3. 集成Redis后是我额外手动保存数据还是框架自动保存** <br>
框架自动保存。集成`Redis`只需要引入对应的`pom依赖`即可框架所有上层API保持不变
<br><br>

View File

@@ -8,7 +8,10 @@
有,就让你通过。没有?那么禁止访问!
再往底了说,就是每个账号都会拥有一个权限码集合,我来验证这个集合中是否包含指定的权限码 <br/>
例如:当前账号拥有权限码集合:`["user:add", "user:delete", "user:get"]`,这时候我来验证权 `"user:update"`,则结果就是:**验证失败,禁止访问**
例如:当前账号拥有权限码集合:`["user:add", "user:delete", "user:get"]`,这时候我来验证权 `"user:update"`,则结果就是:**验证失败,禁止访问**
(注意: 冒号无特殊含义,可有可无)
![无权限](../static/not-jur.png)
所以现在问题的核心就是:
1. 如何获取一个账号所拥有的的权限码集合

View File

@@ -1,6 +1,8 @@
# 踢人下线
所谓踢人下线,核心操作就是找到其指定`loginId`对应的`token`,并设置其失效
![踢下线](../static/kickout.png)
---

View File

@@ -7,6 +7,7 @@
所谓登录验证,说白了就是限制某些接口只有登录后才能访问(如:查询我的账号资料) <br>
那么判断一个会话是否登录的依据是什么?当然是登录成功后框架给你做个标记!然后在需要鉴权的接口里检查此标记,有标记者视为已登录,无标记者视为未登录!
![暂未登录](../static/not-login.png)
### 登录与注销

View File

@@ -43,12 +43,12 @@
``` java
// 底层的 StpLogic 对象
public static StpLogic stpLogic = new StpLogic("login") {
// 重写 `getTokenName` 函数,返回一个与 `StpUtil` 不同的token名称, 防止冲突
public static StpLogic stpLogic = new StpLogic("user") {
// 重写 `splicingKeyTokenName` 函数,返回一个与 `StpUtil` 不同的token名称, 防止冲突
@Override
public String getTokenName() {
return super.getKeyTokenName() + "-user";
}
public String splicingKeyTokenName() {
return super.splicingKeyTokenName()+"-user";
}
};
```

View File

@@ -0,0 +1,83 @@
# 密码加密
严格来讲,密码加密不属于 [权限认证] 的范畴,但是对于大多数系统来讲,密码加密又是安全认证不可或缺的部分,
所以,应大家要求,`sa-token``v1.14版本`添加密码加密模块,该模块非常简单,仅仅封装了一些常见的加密算法
### 摘要加密
md5、sha1、sha256
``` java
// md5加密
SaSecureUtil.md5("123456");
// sha1加密
SaSecureUtil.sha1("123456");
// sha256加密
SaSecureUtil.sha256("123456");
// md5加盐加密: md5(md5(str) + md5(salt))
SaSecureUtil.md5BySalt("123456", "salt");
```
### 对称加密
AES加密
``` java
// 定义秘钥和明文
String key = "123456";
String text = "sa-token 一个轻量级java权限认证框架";
// 加密
String ciphertext = SaSecureUtil.aesEncrypt(key, text);
System.out.println("AES加密后" + ciphertext);
// 解密
String text2 = SaSecureUtil.aesDecrypt(key, ciphertext);
System.out.println("AES解密后" + text2);
```
### 非对称加密
RSA加密
``` java
// 定义私钥和公钥
String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO+wmt01pwm9lHMdq7A8gkEigk0XKMfjv+4IjAFhWCSiTeP7dtlnceFJbkWxvbc7Qo3fCOpwmfcskwUc3VSgyiJkNJDs9ivPbvlt8IU2bZ+PBDxYxSCJFrgouVOpAr8ar/b6gNuYTi1vt3FkGtSjACFb002/68RKUTye8/tdcVilAgMBAAECgYA1COmrSqTUJeuD8Su9ChZ0HROhxR8T45PjMmbwIz7ilDsR1+E7R4VOKPZKW4Kz2VvnklMhtJqMs4MwXWunvxAaUFzQTTg2Fu/WU8Y9ha14OaWZABfChMZlpkmpJW9arKmI22ZuxCEsFGxghTiJQ3tK8npj5IZq5vk+6mFHQ6aJAQJBAPghz91Dpuj+0bOUfOUmzi22obWCBncAD/0CqCLnJlpfOoa9bOcXSusGuSPuKy5KiGyblHMgKI6bq7gcM2DWrGUCQQD3SkOcmia2s/6i7DUEzMKaB0bkkX4Ela/xrfV+A3GzTPv9bIBamu0VIHznuiZbeNeyw7sVo4/GTItq/zn2QJdBAkEA8xHsVoyXTVeShaDIWJKTFyT5dJ1TR++/udKIcuiNIap34tZdgGPI+EM1yoTduBM7YWlnGwA9urW0mj7F9e9WIQJAFjxqSfmeg40512KP/ed/lCQVXtYqU7U2BfBTg8pBfhLtEcOg4wTNTroGITwe2NjL5HovJ2n2sqkNXEio6Ji0QQJAFLW1Kt80qypMqot+mHhS+0KfdOpaKeMWMSR4Ij5VfE63WzETEeWAMQESxzhavN1WOTb3/p6icgcVbgPQBaWhGg==";
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvsJrdNacJvZRzHauwPIJBIoJNFyjH47/uCIwBYVgkok3j+3bZZ3HhSW5Fsb23O0KN3wjqcJn3LJMFHN1UoMoiZDSQ7PYrz275bfCFNm2fjwQ8WMUgiRa4KLlTqQK/Gq/2+oDbmE4tb7dxZBrUowAhW9NNv+vESlE8nvP7XXFYpQIDAQAB";
// 文本
String text = "sa-token 一个轻量级java权限认证框架";
// 使用公钥加密
String ciphertext = SaSecureUtil.rsaEncryptByPublic(publicKey, text);
System.out.println("公钥加密后:" + ciphertext);
// 使用私钥解密
String text2 = SaSecureUtil.rsaDecryptByPrivate(privateKey, ciphertext);
System.out.println("私钥解密后:" + text2);
```
你可能会有疑问,私钥和公钥这么长的一大串,我怎么弄出来,手写吗?当然不是,调用以下方法生成即可
``` java
// 生成一对公钥和私钥其中Map对象 (private=私钥, public=公钥)
System.out.println(SaSecureUtil.rsaGenerateKeyPair());
```
### Base64编码与解码
``` java
// 文本
String text = "sa-token 一个轻量级java权限认证框架";
// 使用Base64编码
String base64Text = SaBase64Util.encode(text);
System.out.println("Base64编码后" + base64Text);
// 使用Base64解码
String text2 = SaBase64Util.decode(base64Text);
System.out.println("Base64解码后" + text2);
```
<br>
如需更多加密算法请提交pr

View File

@@ -10,9 +10,71 @@
### 在sa-token中实现记住我功能
sa-token的登录授权默认就是`记住我`模式,为了实现`非记住我`模式, 你需要做一些适配
sa-token的登录授权**默认就是`[记住我]`模式**,为了实现`[非记住我]`模式, 你需要在登录时如下设置:
``` java
// 设置登录账号id为10001第二个参数指定是否为[记住我]当此值为false后关闭浏览器后再次打开需要重新登录
StpUtil.setLoginId(10001, false);
```
那么sa-token实现`[记住我]`的具体原理是?
### 实现原理
Cookie作为浏览器提供的默认会话跟踪机制其生命周期有两种形式分别是
- 临时Cookie有效期为本次会话只要关闭浏览器窗口Cookie就会消失
- 永久Cookie有效期为一个具体的时间在时间未到期之前即使用户关闭了浏览器Cookie也不会消失
利用Cookie的此特性我们便可以轻松实现 [记住我] 模式:
- 勾选[记住我]按钮时:调用`StpUtil.setLoginId(10001, true)`,在浏览器写入一个`永久Cookie`储存token此时用户即使重启浏览器token依然有效
- 不勾选[记住我]按钮时:调用`StpUtil.setLoginId(10001, false)`,在浏览器写入一个`临时Cookie`储存token此时用户在重启浏览器后token便会消失导致会话失效
### 前后台分离模式下如何实现[记住我]?
此时机智的你😏很快发现一个问题Cookie虽好却无法在前后端分离环境下使用那是不是代表上述方案在APP、小程序等环境中无效
准确的讲答案是肯定的任何基于Cookie的认证方案在前后台分离环境下都会失效原因在于这些客户端默认没有实现Cookie功能不过好在这些客户端一般都提供了替代方案
唯一遗憾的是此场景中token的生命周期需要我们在前端手动控制
以经典跨端框架 [uni-app](https://uniapp.dcloud.io/) 为例,我们可以使用如下方式达到同样的效果:
``` js
// 使用本地存储保存token达到 [永久Cookie] 的效果
uni.setStorageSync("satoken", "xxxx-xxxx-xxxx-xxxx-xxx");
// 使用globalData保存token达到 [临时Cookie] 的效果
getApp().globalData.satoken = "xxxx-xxxx-xxxx-xxxx-xxx";
```
如果你决定在PC浏览器环境下进行前后台分离模式开发那么更加简单
``` js
// 使用 localStorage 保存token达到 [永久Cookie] 的效果
localStorage.setItem("satoken", "xxxx-xxxx-xxxx-xxxx-xxx");
// 使用 sessionStorage 保存token达到 [临时Cookie] 的效果
sessionStorage.setItem("satoken", "xxxx-xxxx-xxxx-xxxx-xxx");
```
Remember me, it's too easy!
### 登录时指定token有效期
登录时不仅可以指定是否为`[记住我]`模式还可以指定一个特定的时间作为token有效时长如下示例
``` java
// 示例1
// 指定token有效期(单位: 秒)如下所示token七天有效
StpUtil.setLoginId(10001, new SaLoginModel().setTimeout(60 * 60 * 24 * 7));
// ----------------------- 示例2所有参数
// `SaLoginModel`为登录参数Model其有诸多参数决定登录时的各种逻辑例如
StpUtil.setLoginId(10001, new SaLoginModel()
.setDevice("PC") // 此次登录的客户端设备标识, 用于[同端互斥登录]时指定此次登录的设备名称
.setIsLastingCookie(true) // 是否为持久Cookie临时Cookie在浏览器关闭时会自动删除持久Cookie在重新打开后依然存在
.setTimeout(60 * 60 * 24 * 7) // 指定此次登录token的有效期, 单位:秒 如未指定自动取全局配置的timeout值
);
```

View File

@@ -10,7 +10,7 @@
## 1、注册路由拦截器
`springboot2.0`为例, 新建配置类`MySaTokenConfig.java`
`SpringBoot2.0`为例, 新建配置类`MySaTokenConfig.java`
``` java
@Configuration
public class MySaTokenConfig implements WebMvcConfigurer {
@@ -22,49 +22,19 @@ public class MySaTokenConfig implements WebMvcConfigurer {
}
}
```
以上代码,我们注册了一个登录验证拦截器,并且排除了`/user/doLogin`接口用来开放登录 <br>
以上代码,我们注册了一个登录验证拦截器,并且排除了`/user/doLogin`接口用来开放登录(除了`/user/doLogin`以外的所有接口都需要登录才能访问) <br>
那么我们如何进行权限认证拦截呢,且往下看
## 2、所有拦截器示例
``` java
@Configuration
public class MySaTokenConfig implements WebMvcConfigurer {
// 注册sa-token的所有拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册一个登录验证拦截器
registry.addInterceptor(SaRouteInterceptor.createLoginVal()).addPathPatterns("/**").excludePathPatterns("/user/doLogin");
// 注册一个角色认证拦截器
registry.addInterceptor(SaRouteInterceptor.createRoleVal("super-admin")).addPathPatterns("/**");
// 注册一个权限认证拦截器
registry.addInterceptor(SaRouteInterceptor.createPermissionVal("user:add", "user:deelete")).addPathPatterns("/UserController/**");
// 注册一个自定义认证拦截器 (可以写任意认证代码)
registry.addInterceptor(new SaRouteInterceptor((request, response, handler)->{
System.out.println("---------- 进入自定义认证 --------------- ");
// 你可以在这里写任意认证代码, 例如: StpUtil.checkLogin();
})).addPathPatterns("/**");
}
}
```
(你不必像上面的示例一样注册所有拦截器,只要按需注册即可
## 3、让我们利用自定义拦截器做点快活的事情
你可以根据路由划分模块,不同模块不同鉴权
## 2、自定义权限验证规则
你可以使用函数式编程自定义验证规则
``` java
@Configuration
public class MySaTokenConfig implements WebMvcConfigurer {
// 注册sa-token的所有拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册路由拦截器,自定义验证规则
registry.addInterceptor(new SaRouteInterceptor((request, response, handler)->{
// 根据路由划分模块,不同模块不同鉴权
SaRouterUtil.match("/user/**", () -> StpUtil.checkPermission("user"));
@@ -78,9 +48,8 @@ public class MySaTokenConfig implements WebMvcConfigurer {
}
```
## 4、完整的示例
最终的代码,可能会类似于下面的样子:
## 3、完整示例
所有用法示例
``` java
@Configuration
@@ -88,12 +57,15 @@ public class MySaTokenConfig implements WebMvcConfigurer {
// 注册sa-token的拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 自定义验证拦截器
// 注册路由拦截器,自定义验证规则
registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> {
// 登录验证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
SaRouterUtil.match("/**", "/user/doLogin", () -> StpUtil.checkLogin());
// 登录验证 -- 排除多个路径
SaRouterUtil.match(Arrays.asList("/**"), Arrays.asList("/user/doLogin", "/user/reg"), () -> StpUtil.checkLogin());
// 角色认证 -- 拦截以 admin 开头的路由,必须具备[admin]角色或者[super-admin]角色才可以通过认证
SaRouterUtil.match("/admin/**", () -> StpUtil.checkRoleOr("admin", "super-admin"));
@@ -108,6 +80,16 @@ public class MySaTokenConfig implements WebMvcConfigurer {
// 匹配 restful 风格路由
SaRouterUtil.match("/article/get/{id}", () -> StpUtil.checkPermission("article"));
// 检查请求方式
SaRouterUtil.match("/notice/**", () -> {
if(request.getMethod().equals(HttpMethod.GET.toString())) {
StpUtil.checkPermission("notice");
}
});
// 在多账号模式下可以使用任意StpUtil进行校验
SaRouterUtil.match("/user/**", () -> StpUserUtil.checkLogin());
})).addPathPatterns("/**");
}
}
@@ -117,6 +99,18 @@ public class MySaTokenConfig implements WebMvcConfigurer {
## 注意事项
在`v1.14`及以前版本下,路由拦截器提供了提供了封装式写法,该方法代码比较冗余,在`v1.15`版本已移除,解决方案如下:
``` java
// 原写法
registry.addInterceptor(SaRouteInterceptor.createPermissionVal("user")).addPathPatterns("/user/**");
// 改为以下方式,效果同上
registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> {
SaRouterUtil.match("/user/**", () -> StpUtil.checkPermission("user"));
})).addPathPatterns("/**");
```

View File

@@ -9,6 +9,8 @@ Session是会话中专业的数据缓存组件通过`Session`我们可以很
- `Token-Session`: 指的是框架为每个`token`分配的`Session`
- `自定义Session`: 指的是以一个`特定的值`作为SessionId来分配的`Session`
> 有关User-Session与Token-Session的详细区别请参考[Session模型详解](/fun/session-model)
### User-Session
有关账号Session的API如下
@@ -63,19 +65,97 @@ SaSessionCustomUtil.deleteSessionById("goods-10001");
### Session相关操作
那么获取到的`SaSession`具体有哪些方法可供操作?
``` java
session.getId(); // 返回此Session的id
session.getCreateTime(); // 返回此Session的创建时间 (时间戳)
session.getAttribute('name'); // 在Session上获取一个值
session.getAttribute('name', 'zhang'); // 在Session上获取一个值并指定取不到值时返回的默认值
session.setAttribute('name', 'zhang'); // 在Session上写入一个值
session.removeAttribute('name'); // 在Session上移除一个值
session.clearAttribute(); // 清空此Session的所有
session.containsAttribute('name'); // 获取此Session是否含有指定key (返回true或false)
session.attributeKeys(); // 获取此Session会话上所有key (返回Set<String>)
session.getDataMap(); // 返回此Session会话上的底层数据对象如果更新map里的值请调用session.update()方法避免产生脏数据)
session.update(); // 将这个Session从持久库更新一下
session.logout(); // 注销此Session会话 (从持久库删除此Session)
// 返回此Session的id
session.getId();
// 返回此Session的创建时间 (时间戳)
session.getCreateTime();
// 在Session上获取一个
session.getAttribute('name');
// 在Session上获取一个值并指定取不到值时返回的默认值
session.getAttribute('name', 'zhang');
// 在Session上写入一个值
session.setAttribute('name', 'zhang');
// 在Session上移除一个值
session.removeAttribute('name');
// 清空此Session的所有值
session.clearAttribute();
// 获取此Session是否含有指定key (返回true或false)
session.containsAttribute('name');
// 获取此Session会话上所有key (返回Set<String>)
session.attributeKeys();
// 返回此Session会话上的底层数据对象如果更新map里的值请调用session.update()方法避免产生脏数据)
session.getDataMap();
// 将这个Session从持久库更新一下
session.update();
// 注销此Session会话 (从持久库删除此Session)
session.logout();
```
具体可参考`javax.servlet.http.HttpSession``SaSession`所含方法与其大体类似
### 类型转换API
由于Session存取值默认的类型都是Object因此我们通常会写很多不必要类型转换代码 <br>
为了简化操作sa-token自`v1.15.0`封装了存取值API的类型转换你可以非常方便的调用以下方法
``` java
// 写值
session.set("name", "zhang");
// 写值(只有在此key原本无值的时候才会写入)
session.set("name", "zhang");
// 取值
session.get("name");
// 取值 (指定默认值)
session.get("name", "<defaultValue>");
// 取值 (转String类型)
session.getString("name");
// 取值 (转int类型)
session.getInt("age");
// 取值 (转long类型)
session.getLong("age");
// 取值 (转double类型)
session.getDouble("result");
// 取值 (转float类型)
session.getFloat("result");
// 取值 (指定转换类型)
session.getModel("key", Student.class);
// 取值 (指定转换类型, 并指定值为Null时返回的默认值)
session.getModel("key", Student.class, <defaultValue>);
// 是否含有某个key
session.has("key");
```
### Session环境隔离说明
有同学经常会把 `SaSession` 与 `HttpSession` 进行混淆,例如:
``` java
@PostMapping("/resetPoints")
public void reset(HttpSession session) {
// 在HttpSession上写入一个值
session.setAttribute("name", 66);
// 在SaSession进行取值
System.out.println(StpUtil.getSession().getAttribute("name")); // 输出null
}
```
**要点:**
1. `SaSession` 与 `HttpSession` 没有任何关系,在`HttpSession`上写入的值,在`SaSession`中无法取出
2. `HttpSession`并未被框架接管在使用sa-token时请在任何情况下均使用`SaSession`,不要使用`HttpSession`

View File

@@ -0,0 +1,32 @@
# Token前缀
### 需求场景
在某些系统中前端提交token时会在前面加个固定的前缀例如
``` js
{
"satoken": "Bearer xxxx-xxxx-xxxx-xxxx"
}
```
此时后端如果不做任何特殊处理,框架将会把`Bearer `视为token的一部分无法正常读取token信息导致鉴权失败
为此我们需要在yml中添加如下配置
``` java
spring:
# sa-token配置
sa-token:
# token前缀
tokenPrefix: Bearer
```
此时 sa-token 便可在读取token时裁剪掉 `Bearer`,成功获取`xxxx-xxxx-xxxx-xxxx`
### 注意点
1. `token前缀` 与 `token值` 之间必须有一个空格
2. 一旦配置了`token前缀`则前端提交token时必须带有前缀否则会导致框架无法读取token
3. 由于`Cookie`中无法存储空格字符也就意味配置token前缀后`Cookie`鉴权方式将会失效此时只能将token提交到`header`里进行传输

View File

@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>sa-token</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="sa-token是一个java权限认证框架功能全面上手简单登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、Spring集成...零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有">
<meta name="description" content="sa-token是一个java权限认证框架功能全面上手简单登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、记住我模式、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、密码加密、jwt集成、Spring集成...有了sa-token你所有的权限认证问题都不再是问题">
<meta name="keywords" content="sa-token,sa-token框架,sa-token文档,java权限认证">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="doc/logo.png">
@@ -41,10 +41,10 @@
<!-- 内容部分 -->
<div class="main-box">
<div class="content-box">
<h1>sa-token<small>v1.13.0</small></h1>
<h1>sa-token<small>v1.15.0.RELEASE</small></h1>
<div class="sub-title">这可能是史上功能最全的java权限认证框架</div>
<!-- <p>0配置开箱即用低学习成本</p> -->
<p>登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、Spring集成...</p>
<p>登录验证、权限验证、Session会话、踢人下线、集成Redis、分布式会话、单点登录、前后台分离、记住我模式、模拟他人账号、临时身份切换、多账号体系、注解式鉴权、路由拦截式鉴权、花式token、自动续签、同端互斥登录、会话治理、密码加密、jwt集成、Spring集成...</p>
<!-- <p>零配置开箱即用,覆盖所有应用场景,你所需要的功能,这里都有</p> -->
<p>有了sa-token你所有的权限认证问题都不再是问题</p>
<div class="btn-box">

View File

@@ -9,3 +9,4 @@ sa-token-oauth2 模块是 sa-token 实现 oauth2.0 的部分,目前该模块
3. 根据控制台打印,访问测试地址即可:[http://localhost:8002/login.html](http://localhost:8002/login.html)
可结合代码注释学习查看

View File

@@ -7,13 +7,13 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>
<name>sa-token-dao-redis</name>
<artifactId>sa-token-oauth2</artifactId>
<version>1.13.0-alpha</version>
<version>1.15.0-alpha</version>
<description>sa-token realization oauth2.0</description>
<dependencies>

View File

@@ -523,7 +523,7 @@ public interface SaOAuth2Interface {
}
/**
* 获取key[Client&账号]最新授权码记录, 持久化使用的key
* 获取key[Client and 账号]最新授权码记录, 持久化使用的key
* @param loginId 账号id
* @param clientId 应用id
* @return key

View File

@@ -51,6 +51,7 @@ public class SaOAuth2Util {
* @param clientId 应用id
* @param scope 权限
* @param loginId 账号id
* @return 是否已经授权
*/
public static boolean isGrant(Object loginId, String clientId, String scope) {
return SaOAuth2Manager.getInterface().isGrant(loginId, clientId, scope);

View File

@@ -50,6 +50,7 @@ public class RequestAuthModel {
/**
* @param clientId 要设置的 clientId
* @return 对象自身
*/
public RequestAuthModel setClientId(String clientId) {
this.clientId = clientId;
@@ -65,6 +66,7 @@ public class RequestAuthModel {
/**
* @param scope 要设置的 scope
* @return 对象自身
*/
public RequestAuthModel setScope(String scope) {
this.scope = scope;
@@ -80,6 +82,7 @@ public class RequestAuthModel {
/**
* @param loginId 要设置的 loginId
* @return 对象自身
*/
public RequestAuthModel setLoginId(Object loginId) {
this.loginId = loginId;
@@ -95,6 +98,7 @@ public class RequestAuthModel {
/**
* @param redirectUri 要设置的 redirectUri
* @return 对象自身
*/
public RequestAuthModel setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
@@ -110,6 +114,7 @@ public class RequestAuthModel {
/**
* @param responseType 要设置的 responseType
* @return 对象自身
*/
public RequestAuthModel setResponseType(String responseType) {
this.responseType = responseType;
@@ -125,6 +130,7 @@ public class RequestAuthModel {
/**
* @param state 要设置的 state
* @return 对象自身
*/
public RequestAuthModel setState(String state) {
this.state = state;
@@ -133,6 +139,7 @@ public class RequestAuthModel {
/**
* 检查此Model参数是否有效
* @return 对象自身
*/
public RequestAuthModel checkModel() {
if(SaTokenInsideUtil.isEmpty(clientId)) {

View File

@@ -26,7 +26,7 @@ public class ScopeModel {
}
/**
* 构造一个
* @param id 权限id
* @param name 权限名称
* @param introduce 权限详细介绍
*/
public ScopeModel(String name, String introduce) {

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>

View File

@@ -7,7 +7,7 @@
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-parent</artifactId>
<version>1.13.0</version>
<version>1.15.0.RELEASE</version>
</parent>
<packaging>jar</packaging>

View File

@@ -105,19 +105,15 @@ public class SaTokenSpringAutowired {
SaTokenManager.setSaTokenServlet(saTokenServlet);
}
/**
* 路由匹配器
*/
public static PathMatcher pathMatcher;
/**
* 利用自动匹配特性获取SpringMVC框架内部使用的路由匹配器
*
* @param pathMatcher 要设置的 pathMatcher
*/
@Autowired(required = false)
public static void setPathMatcher(PathMatcher pathMatcher) {
SaTokenSpringAutowired.pathMatcher = pathMatcher;
public void setPathMatcher(PathMatcher pathMatcher) {
SaTokenServletSpringImpl.setPathMatcher(pathMatcher);
}
}

View File

@@ -5,8 +5,7 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.router.SaRouteFunction;
import cn.dev33.satoken.stp.StpUtil;
/**
@@ -15,204 +14,31 @@ import cn.dev33.satoken.stp.StpUtil;
*/
public class SaRouteInterceptor implements HandlerInterceptor {
// ----------------- 属性 -----------------
/**
* 每次进入拦截器的[执行函数]
*/
public SaRouteFunction function;
/**
* 底层的 StpLogic 对象
*/
private StpLogic stpLogic;
/**
* 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
*/
private int type;
/**
* 验证模式 AND | OR
*/
private SaMode mode;
/**
* 标识码数组
*/
private String[] codes;
/**
* 自定义模式下的执行函数
*/
private SaRouteFunction function;
/**
* 表示登录验证
*/
public static final int LOGIN = 1;
/**
* 表示角色验证
*/
public static final int ROLE = 2;
/**
* 表示权限验证
*/
public static final int PERMISSION = 3;
/**
* 表示自定义验证
*/
public static final int CUSTOM = 4;
/**
* @return 底层的 StpLogic 对象
*/
public StpLogic getStpLogic() {
if(stpLogic == null) {
stpLogic = StpUtil.stpLogic;
}
return stpLogic;
}
/**
* @param stpLogic 底层的 StpLogic 对象
* @return 拦截器自身
*/
public SaRouteInterceptor setStpLogic(StpLogic stpLogic) {
this.stpLogic = stpLogic;
return this;
}
/**
* @return 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
*/
public int getType() {
return type;
}
/**
* @param type 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
* @return 拦截器自身
*/
public SaRouteInterceptor setType(int type) {
this.type = type;
return this;
}
/**
* @return 验证模式 AND | OR
*/
public SaMode getMode() {
return mode;
}
/**
* @param mode 验证模式 AND | OR
* @return 拦截器自身
*/
public SaRouteInterceptor setMode(SaMode mode) {
this.mode = mode;
return this;
}
/**
* @return 标识码数组
*/
public String[] getCodes() {
return codes;
}
/**
* @param codes 标识码数组
* @return 拦截器自身
*/
public SaRouteInterceptor setCodes(String... codes) {
this.codes = codes;
return this;
}
/**
* @return 自定义模式下的执行函数
*/
public SaRouteFunction getFunction() {
return function;
}
/**
* @param function 设置自定义模式下的执行函数
* @return 拦截器自身
*/
public SaRouteInterceptor setFunction(SaRouteFunction function) {
this.type = SaRouteInterceptor.CUSTOM;
this.function = function;
return this;
}
// ----------------- 构建相关 -----------------
/**
* 创建 (全参数)
* @param type 验证类型 (1=登录验证, 2=角色验证, 3=权限验证, 4=自定义验证)
* @param mode 验证模式 AND | OR
* @param codes 标识码数组
*/
public SaRouteInterceptor(int type, SaMode mode, String... codes) {
super();
this.type = type;
this.mode = mode;
this.codes = codes;
}
/**
* 创建 (默认为登录验证)
* 创建一个路由拦截器
*/
public SaRouteInterceptor() {
this(SaRouteInterceptor.LOGIN, null, new String[0]);
}
/**
* 创建 (默认为自定义认证)
* @param function 自定义模式下的执行函数
* 创建, 并指定[执行函数]
* @param function [执行函数]
*/
public SaRouteInterceptor(SaRouteFunction function) {
this(SaRouteInterceptor.CUSTOM, null, new String[0]);
setFunction(function);
this.function = function;
}
/**
* 构建一个模式为登录认证的sa路由拦截器
* @return sa拦截器
*/
public static SaRouteInterceptor createLoginVal() {
return new SaRouteInterceptor();
}
/**
* 构建一个模式为角色认证的sa路由拦截器
* @param roles 需要验证的角色标识列表
* @return sa拦截器
*/
public static SaRouteInterceptor createRoleVal(String... roles) {
return new SaRouteInterceptor(SaRouteInterceptor.ROLE, SaMode.AND, roles);
}
/**
* 构建一个模式为权限认证的sa路由拦截器
* @param permissions 需要验证的权限列表
* @return sa拦截器
*/
public static SaRouteInterceptor createPermissionVal(String... permissions) {
return new SaRouteInterceptor(SaRouteInterceptor.PERMISSION, SaMode.AND, permissions);
}
/**
* 创建一个模式为自定义认证的sa路由拦截器
* 静态方法快速构建一个
* @param function 自定义模式下的执行函数
* @return sa拦截器
* @return sa路由拦截器
*/
public static SaRouteInterceptor createCustomVal(SaRouteFunction function) {
public static SaRouteInterceptor newInstance(SaRouteFunction function) {
return new SaRouteInterceptor(function);
}
@@ -226,22 +52,11 @@ public class SaRouteInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 根据模式进行验证
if(this.type == SaRouteInterceptor.LOGIN) {
getStpLogic().checkLogin();
} else if(this.type == SaRouteInterceptor.ROLE) {
if(mode == SaMode.AND) {
getStpLogic().checkRoleAnd(codes);
} else {
getStpLogic().checkRoleOr(codes);
}
} else if(this.type == SaRouteInterceptor.PERMISSION) {
if(mode == SaMode.AND) {
getStpLogic().checkPermissionAnd(codes);
} else {
getStpLogic().checkPermissionOr(codes);
}
} else if(this.type == SaRouteInterceptor.CUSTOM) {
// 如果未提供function默认进行登录验证
if(function == null) {
StpUtil.checkLogin();
} else {
// 否则执行函数
function.run(request, response, handler);
}
@@ -250,6 +65,4 @@ public class SaRouteInterceptor implements HandlerInterceptor {
}
}

View File

@@ -3,10 +3,13 @@ package cn.dev33.satoken.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import cn.dev33.satoken.servlet.SaTokenServlet;
/**
* sa-token 对cookie的相关操作 接口实现类
* sa-token 对Cookie的相关操作 接口实现类
*
* @author kong
*
@@ -29,4 +32,37 @@ public class SaTokenServletSpringImpl implements SaTokenServlet {
return SpringMVCUtil.getResponse();
}
/**
* 路由匹配器
*/
private static PathMatcher pathMatcher;
/**
* 获取路由匹配器
* @return 路由匹配器
*/
public static PathMatcher getPathMatcher() {
if(pathMatcher == null) {
pathMatcher = new AntPathMatcher();
}
return pathMatcher;
}
/**
* 写入路由匹配器
* @param pathMatcher 路由匹配器
*/
public static void setPathMatcher(PathMatcher pathMatcher) {
SaTokenServletSpringImpl.pathMatcher = pathMatcher;
}
/**
* 校验指定路由匹配符是否可以匹配成功指定路径
*/
@Override
public boolean matchPath(String pattern, String path) {
return getPathMatcher().match(pattern, path);
}
}

View File

@@ -34,7 +34,7 @@ public class SpringMVCUtil {
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes == null) {
throw new SaTokenException("非Web上下文无法获取Request");
throw new SaTokenException("非Web上下文无法获取Response");
}
return servletRequestAttributes.getResponse();
}