mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-08-25 10:08:02 +08:00
新增权限通配符
This commit is contained in:
parent
f1104b6a43
commit
e40a39e3ef
@ -16,7 +16,7 @@ import cn.dev33.satoken.listener.SaTokenListenerDefaultImpl;
|
||||
import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* 管理sa-token所有接口对象
|
||||
@ -32,7 +32,7 @@ public class SaTokenManager {
|
||||
public static void setConfig(SaTokenConfig config) {
|
||||
SaTokenManager.config = config;
|
||||
if(config.getIsV()) {
|
||||
SaTokenInsideUtil.printSaToken();
|
||||
SaFoxUtil.printSaToken();
|
||||
}
|
||||
}
|
||||
public static SaTokenConfig getConfig() {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.dev33.satoken.action;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
|
||||
/**
|
||||
@ -25,4 +27,12 @@ public interface SaTokenAction {
|
||||
*/
|
||||
public SaSession createSession(String sessionId);
|
||||
|
||||
/**
|
||||
* 指定集合是否包含指定元素(模糊匹配)
|
||||
* @param list 集合
|
||||
* @param element 元素
|
||||
* @return 是否包含
|
||||
*/
|
||||
public boolean hasElement(List<String> list, String element);
|
||||
|
||||
}
|
||||
|
@ -1,11 +1,12 @@
|
||||
package cn.dev33.satoken.action;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import cn.dev33.satoken.SaTokenManager;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* 对 SaTokenAction 接口的默认实现
|
||||
@ -14,7 +15,6 @@ import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
*/
|
||||
public class SaTokenActionDefaultImpl implements SaTokenAction {
|
||||
|
||||
|
||||
/**
|
||||
* 根据一定的算法生成一个token
|
||||
*/
|
||||
@ -32,25 +32,24 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
|
||||
}
|
||||
// 32位随机字符串
|
||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_32.equals(tokenStyle)) {
|
||||
return SaTokenInsideUtil.getRandomString(32);
|
||||
return SaFoxUtil.getRandomString(32);
|
||||
}
|
||||
// 64位随机字符串
|
||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_64.equals(tokenStyle)) {
|
||||
return SaTokenInsideUtil.getRandomString(64);
|
||||
return SaFoxUtil.getRandomString(64);
|
||||
}
|
||||
// 128位随机字符串
|
||||
if(SaTokenConsts.TOKEN_STYLE_RANDOM_128.equals(tokenStyle)) {
|
||||
return SaTokenInsideUtil.getRandomString(128);
|
||||
return SaFoxUtil.getRandomString(128);
|
||||
}
|
||||
// tik风格 (2_14_16)
|
||||
if(SaTokenConsts.TOKEN_STYLE_TIK.equals(tokenStyle)) {
|
||||
return SaTokenInsideUtil.getRandomString(2) + "_" + SaTokenInsideUtil.getRandomString(14) + "_" + SaTokenInsideUtil.getRandomString(16) + "__";
|
||||
return SaFoxUtil.getRandomString(2) + "_" + SaFoxUtil.getRandomString(14) + "_" + SaFoxUtil.getRandomString(16) + "__";
|
||||
}
|
||||
// 默认,还是uuid
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据 SessionId 创建一个 Session
|
||||
*/
|
||||
@ -59,4 +58,23 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
|
||||
return new SaSession(sessionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定集合是否包含指定元素(模糊匹配)
|
||||
*/
|
||||
@Override
|
||||
public boolean hasElement(List<String> list, String element) {
|
||||
// 集合为空直接返回false
|
||||
if(list == null || list.size() == 0) {
|
||||
return false;
|
||||
}
|
||||
// 遍历匹配
|
||||
for (String patt : list) {
|
||||
if(SaFoxUtil.vagueMatch(patt, element)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// 走出for循环说明没有一个元素可以匹配成功
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -337,6 +337,4 @@ public class SaTokenConfig {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import cn.dev33.satoken.SaTokenManager;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* sa-token持久层默认的实现类 , 基于内存Map
|
||||
@ -235,7 +235,7 @@ public class SaTokenDaoDefaultImpl implements SaTokenDao {
|
||||
|
||||
@Override
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size) {
|
||||
return SaTokenInsideUtil.searchList(expireMap.keySet(), prefix, keyword, start, size);
|
||||
return SaFoxUtil.searchList(expireMap.keySet(), prefix, keyword, start, size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,7 +4,7 @@ import java.util.Date;
|
||||
|
||||
import cn.dev33.satoken.SaTokenManager;
|
||||
import cn.dev33.satoken.stp.SaLoginModel;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token 侦听器的默认实现:log打印
|
||||
@ -51,7 +51,7 @@ public class SaTokenListenerDefaultImpl implements SaTokenListener {
|
||||
@Override
|
||||
public void doDisable(String loginKey, Object loginId, long disableTime) {
|
||||
Date date = new Date(System.currentTimeMillis() + disableTime * 1000);
|
||||
println("账号[" + loginId + "]被封禁 (解封时间: " + SaTokenInsideUtil.formatDate(date) + ")");
|
||||
println("账号[" + loginId + "]被封禁 (解封时间: " + SaFoxUtil.formatDate(date) + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -24,7 +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;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* sa-token 权限验证,逻辑实现类
|
||||
@ -98,7 +98,7 @@ public class StpLogic {
|
||||
SaStorage storage = SaTokenManager.getSaTokenContext().getStorage();
|
||||
// 判断是否配置了token前缀
|
||||
String tokenPrefix = config.getTokenPrefix();
|
||||
if(SaTokenInsideUtil.isEmpty(tokenPrefix)) {
|
||||
if(SaFoxUtil.isEmpty(tokenPrefix)) {
|
||||
storage.set(splicingKeyJustCreatedSave(), tokenValue);
|
||||
} else {
|
||||
// 如果配置了token前缀,则拼接上前缀一起写入
|
||||
@ -143,7 +143,7 @@ public class StpLogic {
|
||||
|
||||
// 5. 如果打开了前缀模式
|
||||
String tokenPrefix = getConfig().getTokenPrefix();
|
||||
if(SaTokenInsideUtil.isEmpty(tokenPrefix) == false && SaTokenInsideUtil.isEmpty(tokenValue) == false) {
|
||||
if(SaFoxUtil.isEmpty(tokenPrefix) == false && SaFoxUtil.isEmpty(tokenValue) == false) {
|
||||
// 如果token以指定的前缀开头, 则裁剪掉它, 否则视为未提供token
|
||||
if(tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT)) {
|
||||
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
|
||||
@ -882,7 +882,8 @@ public class StpLogic {
|
||||
*/
|
||||
public boolean hasRole(Object loginId, String role) {
|
||||
List<String> roleList = SaTokenManager.getStpInterface().getRoleList(loginId, loginKey);
|
||||
return !(roleList == null || roleList.contains(role) == false);
|
||||
return SaTokenManager.getSaTokenAction().hasElement(roleList, role);
|
||||
// return !(roleList == null || roleList.contains(role) == false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -912,7 +913,7 @@ public class StpLogic {
|
||||
Object loginId = getLoginId();
|
||||
List<String> roleList = SaTokenManager.getStpInterface().getRoleList(loginId, loginKey);
|
||||
for (String role : roleArray) {
|
||||
if(roleList.contains(role) == false) {
|
||||
if(SaTokenManager.getSaTokenAction().hasElement(roleList, role) == false) {
|
||||
throw new NotRoleException(role, this.loginKey);
|
||||
}
|
||||
}
|
||||
@ -926,7 +927,7 @@ public class StpLogic {
|
||||
Object loginId = getLoginId();
|
||||
List<String> roleList = SaTokenManager.getStpInterface().getRoleList(loginId, loginKey);
|
||||
for (String role : roleArray) {
|
||||
if(roleList.contains(role) == true) {
|
||||
if(SaTokenManager.getSaTokenAction().hasElement(roleList, role) == true) {
|
||||
// 有的话提前退出
|
||||
return;
|
||||
}
|
||||
@ -947,7 +948,8 @@ public class StpLogic {
|
||||
*/
|
||||
public boolean hasPermission(Object loginId, String permission) {
|
||||
List<String> permissionList = SaTokenManager.getStpInterface().getPermissionList(loginId, loginKey);
|
||||
return !(permissionList == null || permissionList.contains(permission) == false);
|
||||
return SaTokenManager.getSaTokenAction().hasElement(permissionList, permission);
|
||||
// return !(permissionList == null || permissionList.contains(permission) == false);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -977,7 +979,7 @@ public class StpLogic {
|
||||
Object loginId = getLoginId();
|
||||
List<String> permissionList = SaTokenManager.getStpInterface().getPermissionList(loginId, loginKey);
|
||||
for (String permission : permissionArray) {
|
||||
if(permissionList.contains(permission) == false) {
|
||||
if(SaTokenManager.getSaTokenAction().hasElement(permissionList, permission) == false) {
|
||||
throw new NotPermissionException(permission, this.loginKey);
|
||||
}
|
||||
}
|
||||
@ -991,7 +993,7 @@ public class StpLogic {
|
||||
Object loginId = getLoginId();
|
||||
List<String> permissionList = SaTokenManager.getStpInterface().getPermissionList(loginId, loginKey);
|
||||
for (String permission : permissionArray) {
|
||||
if(permissionList.contains(permission) == true) {
|
||||
if(SaTokenManager.getSaTokenAction().hasElement(permissionList, permission) == true) {
|
||||
// 有的话提前退出
|
||||
return;
|
||||
}
|
||||
|
@ -7,14 +7,15 @@ import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* sa-token 内部代码工具类
|
||||
* Sa-Token 内部工具类
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public class SaTokenInsideUtil {
|
||||
public class SaFoxUtil {
|
||||
|
||||
/**
|
||||
* 打印 sa-token 版本字符画
|
||||
@ -129,4 +130,21 @@ public class SaTokenInsideUtil {
|
||||
return list2;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串模糊匹配
|
||||
* <p>example:
|
||||
* <p> user* user-add -> true
|
||||
* <p> user* art-add -> false
|
||||
* @param patt 表达式
|
||||
* @param str 待匹配的字符串
|
||||
* @return 是否可以匹配
|
||||
*/
|
||||
public static boolean vagueMatch(String patt, String str) {
|
||||
// 如果表达式不带有*号,则只需简单equals即可 (速度提升200倍)
|
||||
if(patt.indexOf("*") == -1) {
|
||||
return patt.equals(str);
|
||||
}
|
||||
return Pattern.matches(patt.replaceAll("\\*", ".*"), str);
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,7 @@ import org.springframework.stereotype.Component;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* sa-token持久层的实现类, 基于redis (使用 jackson 序列化方式)
|
||||
@ -220,7 +220,7 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size) {
|
||||
Set<String> keys = stringRedisTemplate.keys(prefix + "*" + keyword + "*");
|
||||
List<String> list = new ArrayList<String>(keys);
|
||||
return SaTokenInsideUtil.searchList(list, start, size);
|
||||
return SaFoxUtil.searchList(list, start, size);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* sa-token持久层的实现类, 基于redis
|
||||
@ -201,7 +201,7 @@ public class SaTokenDaoRedis implements SaTokenDao {
|
||||
public List<String> searchData(String prefix, String keyword, int start, int size) {
|
||||
Set<String> keys = stringRedisTemplate.keys(prefix + "*" + keyword + "*");
|
||||
List<String> list = new ArrayList<String>(keys);
|
||||
return SaTokenInsideUtil.searchList(list, start, size);
|
||||
return SaFoxUtil.searchList(list, start, size);
|
||||
}
|
||||
|
||||
|
||||
|
@ -241,8 +241,6 @@ public class TestController {
|
||||
@RequestMapping("test")
|
||||
public AjaxJson test() {
|
||||
System.out.println("进来了");
|
||||
StpUtil.disable(10001, 10002);
|
||||
StpUtil.untieDisable(10001);
|
||||
return AjaxJson.getSuccess("访问成功");
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ import cn.dev33.satoken.oauth2.model.CodeModel;
|
||||
import cn.dev33.satoken.oauth2.model.RequestAuthModel;
|
||||
import cn.dev33.satoken.oauth2.util.SaOAuth2Consts;
|
||||
import cn.dev33.satoken.oauth2.util.SaOAuth2InsideUtil;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* sa-token-oauth2 模块 逻辑接口
|
||||
@ -435,7 +435,7 @@ public interface SaOAuth2Interface {
|
||||
* @return 授权码
|
||||
*/
|
||||
public default String createCode(String clientId, String scope, Object loginId) {
|
||||
return SaTokenInsideUtil.getRandomString(60).toLowerCase();
|
||||
return SaFoxUtil.getRandomString(60).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -444,7 +444,7 @@ public interface SaOAuth2Interface {
|
||||
* @return AccessToken
|
||||
*/
|
||||
public default String createAccessToken(CodeModel codeModel) {
|
||||
return SaTokenInsideUtil.getRandomString(60).toLowerCase();
|
||||
return SaFoxUtil.getRandomString(60).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -453,7 +453,7 @@ public interface SaOAuth2Interface {
|
||||
* @return RefreshToken
|
||||
*/
|
||||
public default String createRefreshToken(CodeModel codeModel) {
|
||||
return SaTokenInsideUtil.getRandomString(60).toLowerCase();
|
||||
return SaFoxUtil.getRandomString(60).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cn.dev33.satoken.oauth2.model;
|
||||
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* 请求授权参数的Model
|
||||
@ -142,16 +142,16 @@ public class RequestAuthModel {
|
||||
* @return 对象自身
|
||||
*/
|
||||
public RequestAuthModel checkModel() {
|
||||
if(SaTokenInsideUtil.isEmpty(clientId)) {
|
||||
if(SaFoxUtil.isEmpty(clientId)) {
|
||||
throw new SaTokenException("无效client_id");
|
||||
}
|
||||
if(SaTokenInsideUtil.isEmpty(scope)) {
|
||||
if(SaFoxUtil.isEmpty(scope)) {
|
||||
throw new SaTokenException("无效scope");
|
||||
}
|
||||
if(SaTokenInsideUtil.isEmpty(redirectUri)) {
|
||||
if(SaFoxUtil.isEmpty(redirectUri)) {
|
||||
throw new SaTokenException("无效redirect_uri");
|
||||
}
|
||||
if(SaTokenInsideUtil.isEmpty(String.valueOf(loginId))) {
|
||||
if(SaFoxUtil.isEmpty(String.valueOf(loginId))) {
|
||||
throw new SaTokenException("无效LoginId");
|
||||
}
|
||||
return this;
|
||||
|
@ -5,7 +5,7 @@ import org.springframework.http.ResponseCookie.ResponseCookieBuilder;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Response for Reactor
|
||||
@ -57,13 +57,13 @@ public class SaResponseForReactor implements SaResponse {
|
||||
;
|
||||
|
||||
// set path
|
||||
if(SaTokenInsideUtil.isEmpty(path) == true) {
|
||||
if(SaFoxUtil.isEmpty(path) == true) {
|
||||
path = "/";
|
||||
}
|
||||
builder.path(path);
|
||||
|
||||
// set domain
|
||||
if(SaTokenInsideUtil.isEmpty(domain) == false) {
|
||||
if(SaFoxUtil.isEmpty(domain) == false) {
|
||||
builder.domain(domain);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.util.SaTokenInsideUtil;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Response for Servlet
|
||||
@ -48,10 +48,10 @@ public class SaResponseForServlet implements SaResponse {
|
||||
@Override
|
||||
public void addCookie(String name, String value, String path, String domain, int timeout) {
|
||||
Cookie cookie = new Cookie(name, value);
|
||||
if(SaTokenInsideUtil.isEmpty(path) == true) {
|
||||
if(SaFoxUtil.isEmpty(path) == true) {
|
||||
path = "/";
|
||||
}
|
||||
if(SaTokenInsideUtil.isEmpty(domain) == false) {
|
||||
if(SaFoxUtil.isEmpty(domain) == false) {
|
||||
cookie.setDomain(domain);
|
||||
}
|
||||
cookie.setPath(path);
|
||||
|
Loading…
Reference in New Issue
Block a user