sa两套鉴权合并为一套
This commit is contained in:
@@ -11,7 +11,8 @@ import java.lang.annotation.Target;
|
||||
* 校验后台是否登录
|
||||
* @author Administrator
|
||||
*/
|
||||
@SaCheckLogin(type = "admin")
|
||||
//@SaCheckLogin(type = "admin")
|
||||
@SaCheckLogin(type = "user")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaAdminCheckLogin {
|
||||
|
||||
@@ -15,7 +15,8 @@ import java.lang.annotation.Target;
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@SaCheckPermission(type = "admin")
|
||||
//@SaCheckPermission(type = "admin")
|
||||
@SaCheckPermission(type = "user")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaAdminCheckPermission {
|
||||
|
||||
@@ -15,7 +15,8 @@ import java.lang.annotation.Target;
|
||||
* @author click33
|
||||
*
|
||||
*/
|
||||
@SaCheckRole(type = "admin")
|
||||
@SaCheckRole(type = "user")
|
||||
//@SaCheckRole(type = "admin")
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE})
|
||||
public @interface SaAdminCheckRole {
|
||||
|
||||
@@ -25,11 +25,11 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
@PostConstruct
|
||||
public void setSaTokenConfig() {
|
||||
// admin配置
|
||||
SaTokenConfig adminConfig = new SaTokenConfig();
|
||||
adminConfig.setTokenName("token");
|
||||
// SaTokenConfig adminConfig = new SaTokenConfig();
|
||||
// adminConfig.setTokenName("token");
|
||||
// config1.setTimeout(1000);
|
||||
adminConfig.setTokenStyle("simple-uuid");
|
||||
StpKit.ADMIN.setConfig(adminConfig);
|
||||
// adminConfig.setTokenStyle("simple-uuid");
|
||||
// StpKit.ADMIN.setConfig(adminConfig);
|
||||
|
||||
// 小程序配置
|
||||
SaTokenConfig userConfig = new SaTokenConfig();
|
||||
@@ -54,14 +54,14 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
// 重置根路径,防止satoken切割根路径导致匹配不到路径
|
||||
ApplicationInfo.routePrefix = "";
|
||||
|
||||
SaRouter.match("/user/**").notMatch("/user/login")
|
||||
.check(r -> StpKit.USER.checkLogin())
|
||||
.setHit(true)
|
||||
SaRouter.match("/**").notMatch("/user/login", "/admin/auth/**")
|
||||
.check(r -> StpKit.USER.checkLogin());
|
||||
// .setHit(true)
|
||||
// .match("/**")
|
||||
.notMatch("/user/**")
|
||||
.notMatch("/admin/auth/**")
|
||||
.notMatch("/admin/feign/**")
|
||||
.check(r -> StpKit.ADMIN.checkLogin());
|
||||
// .notMatch("/user/**")
|
||||
// .notMatch("/admin/auth/**")
|
||||
// .notMatch("/admin/feign/**")
|
||||
// .check(r -> StpKit.ADMIN.checkLogin());
|
||||
})).addPathPatterns("/**");
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class StpInterfaceImpl implements StpInterface {
|
||||
}
|
||||
|
||||
private static @Nullable List<String> getCashInfo(String key) {
|
||||
Object value = StpKit.ADMIN.getSession().get(key);
|
||||
Object value = StpKit.USER.getSession().get(key);
|
||||
if (value instanceof List<?> list) {
|
||||
return (List<String>) list;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.czg.sa;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import com.czg.exception.ApiNotPrintException;
|
||||
import lombok.Getter;
|
||||
@@ -23,15 +25,48 @@ public class MyStpLogic extends StpLogic {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺id
|
||||
* @return 返回店铺id,不存在抛出异常
|
||||
*
|
||||
* @param id 登录账号id
|
||||
* @param shopId 店铺id
|
||||
* @param isManager true 管理端 false 用户端
|
||||
* @param isAdmin 是否为管理员账号
|
||||
*/
|
||||
public Long getShopId() {
|
||||
Object object = StpKit.ADMIN.getSession().get("shopId");
|
||||
if (object == null) {
|
||||
throw new ApiNotPrintException("shopId获取失败");
|
||||
public void login(Long id, Long shopId, boolean isManager, boolean isAdmin) {
|
||||
super.login(id);
|
||||
login(id);
|
||||
if (isManager && shopId == null) {
|
||||
throw new ApiNotPrintException("管理端登录必须传递店铺id");
|
||||
}
|
||||
return (Long) object;
|
||||
SaSession session = getSession().set("userId", id).set("isAdmin", isAdmin).set("isManager", isManager);
|
||||
if (shopId != null) {
|
||||
session.set("shopId", shopId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取店铺id
|
||||
* @param defaultValue 默认值,传递多个只取第一个
|
||||
* @return 返回店铺id,不存在抛出异常,如果传递了默认值不存在返回默认值
|
||||
*/
|
||||
public Long getShopId(Long... defaultValue) {
|
||||
Long defaultVal = defaultValue.length > 0 ? defaultValue[0] : null;
|
||||
Object object = StpKit.USER.getSession().get("isManager");
|
||||
Long shopId;
|
||||
int errType;
|
||||
if (object instanceof Boolean t && t) {
|
||||
Object info = StpKit.USER.getSession().get("shopId");
|
||||
shopId = info instanceof Long l ? l : null;
|
||||
errType = 0;
|
||||
}else {
|
||||
String header = SaHolder.getRequest().getHeader("shopId");
|
||||
shopId = header == null ? null : Long.parseLong(header);
|
||||
errType = 1;
|
||||
}
|
||||
|
||||
if (defaultValue.length == 0 && shopId == null) {
|
||||
throw new ApiNotPrintException(errType == 0 ? "shopId获取失败" : "客户端请求头未携带shopId");
|
||||
}
|
||||
return shopId == null ? defaultVal : shopId;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,7 +74,7 @@ public class MyStpLogic extends StpLogic {
|
||||
* @return 布尔值
|
||||
*/
|
||||
public boolean isAdmin() {
|
||||
Object object = StpKit.ADMIN.getSession().get("isAdmin");
|
||||
Object object = StpKit.USER.getSession().get("isAdmin");
|
||||
return object instanceof Boolean t && t;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.czg.sa;
|
||||
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
/**
|
||||
* StpLogic 门面类,管理项目中所有的 StpLogic 账号体系
|
||||
@@ -11,11 +12,15 @@ public class StpKit {
|
||||
/**
|
||||
* Admin 会话对象,管理 Admin 表所有账号的登录、权限认证
|
||||
*/
|
||||
public static final MyStpLogic ADMIN = new MyStpLogic("admin");
|
||||
// public static final MyStpLogic ADMIN = new MyStpLogic("admin");
|
||||
|
||||
/**
|
||||
* User 会话对象,管理 User 表所有账号的登录、权限认证
|
||||
*/
|
||||
public static final MyStpLogic USER = new MyStpLogic("user");
|
||||
|
||||
|
||||
// public StpKit() {
|
||||
// wait();
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user