登录增加userId
This commit is contained in:
@@ -2,6 +2,7 @@ package com.czg.config;
|
|||||||
|
|
||||||
import cn.dev33.satoken.stp.StpInterface;
|
import cn.dev33.satoken.stp.StpInterface;
|
||||||
import com.czg.sa.StpKit;
|
import com.czg.sa.StpKit;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -20,24 +21,39 @@ public class StpInterfaceImpl implements StpInterface {
|
|||||||
@Override
|
@Override
|
||||||
public List<String> getPermissionList(Object loginId, String loginType) {
|
public List<String> getPermissionList(Object loginId, String loginType) {
|
||||||
if ("admin".equals(loginType)) {
|
if ("admin".equals(loginType)) {
|
||||||
Object value = StpKit.ADMIN.getSaTokenDao().getObject("sa:permissionList:" + loginType + ":" + loginId);
|
String key1 = "sa:permissionList:" + loginType + ":" + loginId;
|
||||||
if (value instanceof List<?> list) {
|
List<String> list = getCashInfo(key1);
|
||||||
return (List<String>) list;
|
if (list != null) {
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static @Nullable List<String> getCashInfo(String key) {
|
||||||
|
Object value = StpKit.ADMIN.getSaTokenDao().getObject(key);
|
||||||
|
if (value instanceof List<?> list) {
|
||||||
|
// 续费时间
|
||||||
|
long timeout = StpKit.ADMIN.getTokenTimeout();
|
||||||
|
if (timeout > 0) {
|
||||||
|
StpKit.ADMIN.getSaTokenDao().updateObjectTimeout(key, StpKit.ADMIN.getTokenTimeout() + 120);
|
||||||
|
}
|
||||||
|
return (List<String>) list;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
|
* 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<String> getRoleList(Object loginId, String loginType) {
|
public List<String> getRoleList(Object loginId, String loginType) {
|
||||||
if ("admin".equals(loginType)) {
|
if ("admin".equals(loginType)) {
|
||||||
Object value = StpKit.ADMIN.getSaTokenDao().getObject("sa:roleList:" + loginType + ":" + loginId);
|
String key = "sa:roleList:" + loginType + ":" + loginId;
|
||||||
if (value instanceof List<?> a) {
|
List<String> list = getCashInfo(key);
|
||||||
return (List<String>) a;
|
if (list != null) {
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.czg.sa;
|
package com.czg.sa;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.SaLoginModel;
|
||||||
import cn.dev33.satoken.stp.StpLogic;
|
import cn.dev33.satoken.stp.StpLogic;
|
||||||
|
import com.czg.exception.ApiNotPrintException;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
@@ -13,6 +15,8 @@ import java.util.List;
|
|||||||
public class MyStpLogic extends StpLogic {
|
public class MyStpLogic extends StpLogic {
|
||||||
@Setter
|
@Setter
|
||||||
private boolean isAdmin;
|
private boolean isAdmin;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化 StpLogic, 并指定账号类型
|
* 初始化 StpLogic, 并指定账号类型
|
||||||
*
|
*
|
||||||
@@ -22,13 +26,49 @@ public class MyStpLogic extends StpLogic {
|
|||||||
super(loginType);
|
super(loginType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录
|
||||||
|
* @param sysUserId 系统用户id
|
||||||
|
* @param shopId 店铺id
|
||||||
|
*/
|
||||||
|
public void login(Long sysUserId, Long shopId) {
|
||||||
|
login(sysUserId);
|
||||||
|
setShopId(shopId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取店铺id
|
||||||
|
* @return 返回店铺id,不存在抛出异常
|
||||||
|
*/
|
||||||
|
public Long getShopId() {
|
||||||
|
String key = "sa:shopId:" + getLoginType() + ":" + getLoginId();
|
||||||
|
Object object = getSaTokenDao().getObject(key);
|
||||||
|
if (object == null) {
|
||||||
|
throw new ApiNotPrintException("shopId获取失败");
|
||||||
|
}
|
||||||
|
// 续费时间
|
||||||
|
long timeout = getTokenTimeout();
|
||||||
|
if (timeout > 0) {
|
||||||
|
getSaTokenDao().updateObjectTimeout(key, getTokenTimeout() + 120);
|
||||||
|
}
|
||||||
|
return (Long) object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置店铺id
|
||||||
|
* @param shopId 店铺id
|
||||||
|
*/
|
||||||
|
private void setShopId(Long shopId) {
|
||||||
|
this.getSaTokenDao().setObject("sa:shopId:" + getLoginType() + ":" + getLoginId(), shopId, getTokenTimeout() + 120);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加当前账号的角色
|
* 添加当前账号的角色
|
||||||
* @param roleList 角色列表
|
* @param roleList 角色列表
|
||||||
* @return 当前实例
|
* @return 当前实例
|
||||||
*/
|
*/
|
||||||
public MyStpLogic addRoleList(List<String> roleList) {
|
public MyStpLogic addRoleList(List<String> roleList) {
|
||||||
this.getSaTokenDao().setObject("sa:roleList:" + getLoginType() + ":" + getLoginId(), roleList, getTokenTimeout());
|
this.getSaTokenDao().setObject("sa:roleList:" + getLoginType() + ":" + getLoginId(), roleList, getTokenTimeout() + 120);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +78,7 @@ public class MyStpLogic extends StpLogic {
|
|||||||
* @return 当前实例
|
* @return 当前实例
|
||||||
*/
|
*/
|
||||||
public MyStpLogic addPermissionList(List<String> permissionList) {
|
public MyStpLogic addPermissionList(List<String> permissionList) {
|
||||||
this.getSaTokenDao().setObject("sa:permissionList:" + getLoginType() + ":" + getLoginId(), permissionList, getTokenTimeout());
|
this.getSaTokenDao().setObject("sa:permissionList:" + getLoginType() + ":" + getLoginId(), permissionList, getTokenTimeout() + 120);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StpKit.ADMIN.login(user.getId());
|
StpKit.ADMIN.login(user.getId(), shopInfo.getId());
|
||||||
StpKit.ADMIN.setAdmin(user.getIsAdmin());
|
StpKit.ADMIN.setAdmin(user.getIsAdmin());
|
||||||
// 查询角色
|
// 查询角色
|
||||||
List<SysRole> roleList = sysRoleService.getByUserId(user.getId());
|
List<SysRole> roleList = sysRoleService.getByUserId(user.getId());
|
||||||
|
|||||||
Reference in New Issue
Block a user