mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-05-04 12:47:55 +08:00
整理pr
This commit is contained in:
parent
a54a8cc455
commit
e9ab375854
@ -4,7 +4,6 @@ import java.lang.reflect.AnnotatedElement;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||||
@ -73,11 +72,21 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
|
|||||||
if(list == null || list.size() == 0) {
|
if(list == null || list.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 先尝试一下简单匹配,如果可以匹配成功则无需继续模糊匹配
|
||||||
if (list.contains(element)) {
|
if (list.contains(element)) {
|
||||||
return true;
|
return true;
|
||||||
}else{
|
|
||||||
return list.stream().anyMatch(patt-> Pattern.matches(patt.replaceAll("\\*", ".*"), element));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 开始模糊匹配
|
||||||
|
for (String patt : list) {
|
||||||
|
if(SaFoxUtil.vagueMatch(patt, element)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 走出for循环说明没有一个元素可以匹配成功
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -86,14 +95,19 @@ public class SaTokenActionDefaultImpl implements SaTokenAction {
|
|||||||
@Override
|
@Override
|
||||||
public void checkMethodAnnotation(Method method) {
|
public void checkMethodAnnotation(Method method) {
|
||||||
|
|
||||||
// 获取这个 Method 所属的 Class
|
// 先校验 Method 所属 Class 上的注解
|
||||||
Class<?> clazz = method.getDeclaringClass();
|
validateAnnotation(method.getDeclaringClass());
|
||||||
|
|
||||||
validateAnnotation(clazz);
|
// 再校验 Method 上的注解
|
||||||
validateAnnotation(method);
|
validateAnnotation(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateAnnotation(AnnotatedElement target) {
|
/**
|
||||||
|
* 从指定元素校验注解
|
||||||
|
* @param target see note
|
||||||
|
*/
|
||||||
|
protected void validateAnnotation(AnnotatedElement target) {
|
||||||
|
|
||||||
// 校验 @SaCheckLogin 注解
|
// 校验 @SaCheckLogin 注解
|
||||||
if(target.isAnnotationPresent(SaCheckLogin.class)) {
|
if(target.isAnnotationPresent(SaCheckLogin.class)) {
|
||||||
SaCheckLogin at = target.getAnnotation(SaCheckLogin.class);
|
SaCheckLogin at = target.getAnnotation(SaCheckLogin.class);
|
||||||
|
@ -115,7 +115,7 @@ public class SaTokenConfig {
|
|||||||
/**
|
/**
|
||||||
* @return 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
* @return 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
||||||
*/
|
*/
|
||||||
public Boolean isAllowConcurrentLogin() {
|
public Boolean getAllowConcurrentLogin() {
|
||||||
return allowConcurrentLogin;
|
return allowConcurrentLogin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +131,7 @@ public class SaTokenConfig {
|
|||||||
/**
|
/**
|
||||||
* @return 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
* @return 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
||||||
*/
|
*/
|
||||||
public Boolean isShareToken() {
|
public Boolean getIsShare() {
|
||||||
return isShare;
|
return isShare;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +179,7 @@ public class SaTokenConfig {
|
|||||||
/**
|
/**
|
||||||
* @return 是否尝试从cookie里读取token
|
* @return 是否尝试从cookie里读取token
|
||||||
*/
|
*/
|
||||||
public Boolean isReadCookie() {
|
public Boolean getIsReadCookie() {
|
||||||
return isReadCookie;
|
return isReadCookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +111,9 @@ public class SaTokenConfigFactory {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static <T> T getObjectByClass(String str, Class<T> cs) {
|
private static <T> T getObjectByClass(String str, Class<T> cs) {
|
||||||
Object value;
|
Object value;
|
||||||
if (cs.equals(String.class)) {
|
if (str == null) {
|
||||||
|
value = null;
|
||||||
|
} else if (cs.equals(String.class)) {
|
||||||
value = str;
|
value = str;
|
||||||
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
|
} else if (cs.equals(int.class) || cs.equals(Integer.class)) {
|
||||||
value = Integer.valueOf(str);
|
value = Integer.valueOf(str);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cn.dev33.satoken.stp;
|
package cn.dev33.satoken.stp;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -107,7 +106,7 @@ public class StpLogic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 注入Cookie
|
// 注入Cookie
|
||||||
if(config.isReadCookie()){
|
if(config.getIsReadCookie()){
|
||||||
SaResponse response = SaHolder.getResponse();
|
SaResponse response = SaHolder.getResponse();
|
||||||
response.addCookie(getTokenName(), tokenValue, "/", config.getCookieDomain(), cookieTimeout);
|
response.addCookie(getTokenName(), tokenValue, "/", config.getCookieDomain(), cookieTimeout);
|
||||||
}
|
}
|
||||||
@ -138,7 +137,7 @@ public class StpLogic {
|
|||||||
tokenValue = request.getHeader(keyTokenName);
|
tokenValue = request.getHeader(keyTokenName);
|
||||||
}
|
}
|
||||||
// 4. 尝试从cookie里读取
|
// 4. 尝试从cookie里读取
|
||||||
if(tokenValue == null && config.isReadCookie()){
|
if(tokenValue == null && config.getIsReadCookie()){
|
||||||
tokenValue = request.getCookieValue(keyTokenName);
|
tokenValue = request.getCookieValue(keyTokenName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,9 +226,9 @@ public class StpLogic {
|
|||||||
// ------ 2、生成一个token
|
// ------ 2、生成一个token
|
||||||
String tokenValue = null;
|
String tokenValue = null;
|
||||||
// --- 如果允许并发登录
|
// --- 如果允许并发登录
|
||||||
if(config.isAllowConcurrentLogin()) {
|
if(config.getAllowConcurrentLogin()) {
|
||||||
// 如果配置为共享token, 则尝试从Session签名记录里取出token
|
// 如果配置为共享token, 则尝试从Session签名记录里取出token
|
||||||
if(config.isShareToken()) {
|
if(config.getIsShare()) {
|
||||||
tokenValue = getTokenValueByLoginId(loginId, loginModel.getDevice());
|
tokenValue = getTokenValueByLoginId(loginId, loginModel.getDevice());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -291,7 +290,7 @@ public class StpLogic {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 如果打开了cookie模式,第一步,先把cookie清除掉
|
// 如果打开了cookie模式,第一步,先把cookie清除掉
|
||||||
if(getConfig().isReadCookie()){
|
if(getConfig().getIsReadCookie()){
|
||||||
SaHolder.getResponse().deleteCookie(getTokenName());
|
SaHolder.getResponse().deleteCookie(getTokenName());
|
||||||
}
|
}
|
||||||
logoutByTokenValue(tokenValue);
|
logoutByTokenValue(tokenValue);
|
||||||
|
@ -130,4 +130,21 @@ public class SaFoxUtil {
|
|||||||
return list2;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -241,20 +241,6 @@ public class TestController {
|
|||||||
@RequestMapping("test")
|
@RequestMapping("test")
|
||||||
public AjaxJson test() {
|
public AjaxJson test() {
|
||||||
System.out.println("进来了");
|
System.out.println("进来了");
|
||||||
// System.out.println(StpUtil.getTokenInfo());
|
|
||||||
StpUtil.setLoginId(10001);
|
|
||||||
String ss = StpUtil.getSession().get("name", () -> {
|
|
||||||
System.out.println("-=------进入方法");
|
|
||||||
return "zhangsan";
|
|
||||||
});
|
|
||||||
ss = StpUtil.getSession().get("name", () -> {
|
|
||||||
System.out.println("-=------进入方法2");
|
|
||||||
return "zhangsan2";
|
|
||||||
});
|
|
||||||
|
|
||||||
StpUtil.getSession().delete("name");
|
|
||||||
System.out.println(ss);
|
|
||||||
|
|
||||||
return AjaxJson.getSuccess("访问成功");
|
return AjaxJson.getSuccess("访问成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user