sa两套鉴权合并为一套

This commit is contained in:
张松
2025-02-13 10:46:15 +08:00
parent 506efc04a2
commit c3a965a08a
21 changed files with 149 additions and 65 deletions

View File

@@ -50,9 +50,8 @@ public class AuthorizationController {
return CzgResult.success();
}
@SaAdminCheckPermission("/adadas")
@GetMapping("test")
public CzgResult<?> login() {
return CzgResult.success(Map.of("token", StpKit.ADMIN.getShopId()));
return CzgResult.success(Map.of("token", StpKit.USER.getShopId()));
}
}

View File

@@ -0,0 +1,42 @@
package com.czg.controller.user;
import cn.dev33.satoken.stp.StpUtil;
import com.czg.account.entity.UserInfo;
import com.czg.account.service.UserInfoService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户信息管理
* @author Administrator
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserInfoService userInfoService;
/**
* 用户信息获取
* @return 用户信息
*/
@GetMapping
public CzgResult<UserInfo> get() {
return CzgResult.success(userInfoService.getById(StpKit.USER.getLoginIdAsLong()));
}
/**
* 用户信息获取
* @return 用户信息
*/
// @GetMapping
// public CzgResult<UserInfo> get() {
// return CzgResult.success(userInfoService.getById(StpKit.USER.getLoginIdAsLong()));
// }
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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("/**");
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
// }
}

View File

@@ -100,15 +100,14 @@ public class AuthorizationServiceImpl implements AuthorizationService {
}
}
StpKit.ADMIN.login(user.getId());
StpKit.ADMIN.getSession().set("userId", user.getId()).set("shopId", shopInfo.getId()).set("isAdmin", user.getIsAdmin());
StpKit.USER.login(user.getId(), shopInfo.getId(), true, user.getIsAdmin());
// 查询角色
List<SysRole> roleList = sysRoleService.getByUserId(user.getId());
List<String> roleNames = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
StpKit.ADMIN.addRoleList(roleNames);
StpKit.USER.addRoleList(roleNames);
// 权限赋予
List<String> promissionList = sysMenuMapper.selectByUserId(user.getId(), null).stream().map(SysMenu::getPermission).filter(StrUtil::isNotBlank).collect(Collectors.toList());
StpKit.ADMIN.addPermissionList(promissionList);
return new LoginVO(StpKit.ADMIN.getTokenInfo(), promissionList, loginDTO.loginType(), shopInfo);
StpKit.USER.addPermissionList(promissionList);
return new LoginVO(StpKit.USER.getTokenInfo(), promissionList, loginDTO.loginType(), shopInfo);
}
}

View File

@@ -28,7 +28,7 @@ public class CommonServiceImpl implements CommonService {
@Override
public Boolean sendSms(String type) {
SysUser sysUser = sysUserService.queryChain().eq(SysUser::getId, StpKit.ADMIN.getLoginIdAsLong()).one();
SysUser sysUser = sysUserService.queryChain().eq(SysUser::getId, StpKit.USER.getLoginIdAsLong()).one();
if (StrUtil.isBlank(sysUser.getPhone())) {
throw new ApiNotPrintException("账号未绑定手机号");
}
@@ -40,7 +40,7 @@ public class CommonServiceImpl implements CommonService {
@Override
public Boolean checkSmsCode(String type, String code) {
SysUser sysUser = sysUserService.queryChain().eq(SysUser::getId, StpKit.ADMIN.getLoginIdAsLong()).one();
SysUser sysUser = sysUserService.queryChain().eq(SysUser::getId, StpKit.USER.getLoginIdAsLong()).one();
if (StrUtil.isBlank(sysUser.getPhone())) {
throw new ApiNotPrintException("账号未绑定手机号");
}

View File

@@ -79,10 +79,10 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
@Override
public Boolean edit(ShopInfoEditDTO shopInfoEditDTO) {
ShopInfo shopInfo;
if (!StpKit.ADMIN.isAdmin()) {
shopInfo = queryChain().eq(ShopInfo::getId, StpKit.ADMIN.getLoginIdAsLong()).one();
if (!StpKit.USER.isAdmin()) {
shopInfo = queryChain().eq(ShopInfo::getId, StpKit.USER.getLoginIdAsLong()).one();
}else {
shopInfo = getById(StpKit.ADMIN.getLoginIdAsLong());
shopInfo = getById(StpKit.USER.getLoginIdAsLong());
}
if (shopInfo == null) {
throw new CzgException("店铺不存在");
@@ -97,6 +97,6 @@ public class ShopInfoServiceImpl extends ServiceImpl<ShopInfoMapper, ShopInfo> i
@Override
public ShopInfo detail() {
return queryChain().eq(ShopInfo::getId, StpKit.ADMIN.getLoginIdAsLong()).one();
return queryChain().eq(ShopInfo::getId, StpKit.USER.getLoginIdAsLong()).one();
}
}

View File

@@ -20,16 +20,16 @@ public class ShopMerchantServiceImpl extends ServiceImpl<ShopMerchantMapper, Sho
@Override
public ShopMerchant detail() {
ShopMerchant one = queryChain().eq(ShopMerchant::getShopId, StpKit.ADMIN.getLoginIdAsLong()).one();
ShopMerchant one = queryChain().eq(ShopMerchant::getShopId, StpKit.USER.getLoginIdAsLong()).one();
return one == null ? new ShopMerchant() : one;
}
@Override
public Boolean edit(ShopMerchantEditDTO shopMerchantEditDTO) {
ShopMerchant shopMerchant = queryChain().eq(ShopMerchant::getShopId, StpKit.ADMIN.getLoginIdAsLong()).one();
ShopMerchant shopMerchant = queryChain().eq(ShopMerchant::getShopId, StpKit.USER.getLoginIdAsLong()).one();
if (shopMerchant == null) {
shopMerchant = new ShopMerchant();
shopMerchant.setShopId(StpKit.ADMIN.getLoginIdAsLong());
shopMerchant.setShopId(StpKit.USER.getLoginIdAsLong());
BeanUtil.copyProperties(shopMerchantEditDTO, shopMerchant);
return save(shopMerchant);
}

View File

@@ -37,14 +37,14 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
public Boolean add(ShopStaffAddDTO shopStaffAddDTO) {
SysUser sysUser = sysUserService.addUser(shopStaffAddDTO.getName(), shopStaffAddDTO.getAccountName(), shopStaffAddDTO.getAccountPwd(), shopStaffAddDTO.getPhone(), shopStaffAddDTO.getRoleId());
ShopStaff shopStaff = BeanUtil.copyProperties(shopStaffAddDTO, ShopStaff.class);
shopStaff.setShopId(StpKit.ADMIN.getLoginIdAsLong());
shopStaff.setShopId(StpKit.USER.getLoginIdAsLong());
shopStaff.setId(sysUser.getId());
return save(shopStaff);
}
@Override
public Boolean edit(ShopStaffEditDTO shopStaffEditDTO) {
long sysUserId = StpKit.ADMIN.getLoginIdAsLong();
long sysUserId = StpKit.USER.getLoginIdAsLong();
ShopStaff shopStaff = queryChain().eq(ShopStaff::getShopId, sysUserId).eq(ShopStaff::getId, shopStaffEditDTO.getId()).one();
if (shopStaffEditDTO.getRoleId() != null) {
sysUsersRolesService.updateRole(shopStaff.getId(), shopStaffEditDTO.getRoleId());
@@ -66,13 +66,13 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
queryWrapper.like(ShopStaff::getCode, name);
}
queryWrapper.eq(ShopStaff::getShopId, StpKit.ADMIN.getLoginIdAsLong());
queryWrapper.eq(ShopStaff::getShopId, StpKit.USER.getLoginIdAsLong());
return page(PageUtil.buildPage(), queryWrapper);
}
@Override
public Boolean delete(ShopStaffRemoveDTO shopStaffRemoveDTO) {
long sysUserId = StpKit.ADMIN.getLoginIdAsLong();
long sysUserId = StpKit.USER.getLoginIdAsLong();
ShopStaff shopStaff = queryChain().eq(ShopStaff::getShopId, sysUserId).eq(ShopStaff::getId, shopStaffRemoveDTO.getId()).one();
if (shopStaff == null) {
throw new ApiNotPrintException("员工账号不存在");

View File

@@ -24,7 +24,7 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
@Override
public Object getMenu() {
long sysUserId = StpKit.ADMIN.getLoginIdAsLong();
long sysUserId = StpKit.USER.getLoginIdAsLong();
List<SysMenu> allMenus = mapper.selectByUserId(sysUserId, null);
List<MenuVO> rootMenus = new ArrayList<>();
List<MenuVO> allMenuVos = new ArrayList<>();

View File

@@ -48,8 +48,8 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
@Override
public Page<SysRole> getList(PageDTO pageDTO, String key, String startTime, String endTime) {
QueryWrapper queryWrapper = new QueryWrapper();
if (!StpKit.ADMIN.isAdmin()) {
queryWrapper.eq(SysRole::getCreateUserId, StpKit.ADMIN.getLoginIdAsLong());
if (!StpKit.USER.isAdmin()) {
queryWrapper.eq(SysRole::getCreateUserId, StpKit.USER.getLoginIdAsLong());
}
if (StrUtil.isNotBlank(key)) {
@@ -92,8 +92,8 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
sysRole.setName(roleAddDTO.name());
sysRole.setLevel(roleAddDTO.level());
sysRole.setDescription(roleAddDTO.description());
sysRole.setShopId(StpKit.ADMIN.getLoginIdAsLong());
sysRole.setCreateUserId(StpKit.ADMIN.getLoginIdAsLong());
sysRole.setShopId(StpKit.USER.getLoginIdAsLong());
sysRole.setCreateUserId(StpKit.USER.getLoginIdAsLong());
sysRole.setCreateTime(DateUtil.date().toLocalDateTime());
boolean save = save(sysRole);
if (save) {
@@ -106,7 +106,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> imp
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean edit(RoleEditDTO roleEditDTO) {
SysRole role = queryChain().eq(SysRole::getId, roleEditDTO.getId()).eq(SysRole::getCreateUserId, StpKit.ADMIN.getLoginIdAsLong()).one();
SysRole role = queryChain().eq(SysRole::getId, roleEditDTO.getId()).eq(SysRole::getCreateUserId, StpKit.USER.getLoginIdAsLong()).one();
if (role == null) {
throw new ApiNotPrintException("角色不存在");
}

View File

@@ -38,13 +38,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> imp
sysUser.setNickName(nickname);
sysUser.setPhone(phone);
sysUser.setStauts(1);
sysUser.setCreateUserId(StpKit.ADMIN.getLoginIdAsLong());
sysUser.setCreateUserId(StpKit.USER.getLoginIdAsLong());
save(sysUser);
sysUser.setPassword(SecureUtil.md5(sysUser.getId() + accountPwd));
updateById(sysUser);
// 绑定角色
long roleCount = sysRoleMapper.selectCountByQuery(new QueryWrapper().eq(SysRole::getId, roleId).eq(SysRole::getCreateUserId, StpKit.ADMIN.getLoginIdAsLong()));
long roleCount = sysRoleMapper.selectCountByQuery(new QueryWrapper().eq(SysRole::getId, roleId).eq(SysRole::getCreateUserId, StpKit.USER.getLoginIdAsLong()));
if (roleCount == 0) {
throw new CzgException("角色不存在");
}

View File

@@ -62,7 +62,8 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
userInfo.setLastLoginTime(DateUtil.date().toLocalDateTime());
userInfoService.saveOrUpdate(userInfo);
StpKit.USER.login(userInfo.getId());
// StpKit.USER.login(userInfo.getId());
StpKit.USER.login(userInfo.getId(), null, false, false);
return new LoginTokenDTO(StpKit.USER.getTokenValue(), userInfo);
}
}

View File

@@ -35,7 +35,7 @@ public class ShopProdUnitServiceImpl extends ServiceImpl<ShopProdUnitMapper, Sho
if (StrUtil.isNotEmpty(param.getName())) {
queryWrapper.like(ShopProdUnit::getName, param.getName());
}
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
queryWrapper.eq(ShopProdUnit::getShopId, shopId);
queryWrapper.orderBy(ShopProdUnit::getId, false);
return queryWrapper;
@@ -55,13 +55,13 @@ public class ShopProdUnitServiceImpl extends ServiceImpl<ShopProdUnitMapper, Sho
@Override
public ShopProdUnitDTO get(Long id) {
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
return super.getOneAs(query().eq(ShopProdUnit::getId, id).eq(ShopProdUnit::getShopId, shopId), ShopProdUnitDTO.class);
}
@Override
public boolean save(ShopProdUnitDTO dto) {
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
boolean exists = super.exists(query().eq(ShopProdUnit::getName, dto.getName()).eq(ShopProdUnit::getShopId, shopId));
if (exists) {
throw new CzgException("单位名称已存在");
@@ -78,7 +78,7 @@ public class ShopProdUnitServiceImpl extends ServiceImpl<ShopProdUnitMapper, Sho
@Override
public boolean update(ShopProdUnitDTO dto) {
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
boolean exists = super.exists(query().eq(ShopProdUnit::getName, dto.getName()).eq(ShopProdUnit::getShopId, shopId).ne(ShopProdUnit::getId, dto.getId()));
if (exists) {
throw new CzgException("单位名称已存在");
@@ -89,7 +89,7 @@ public class ShopProdUnitServiceImpl extends ServiceImpl<ShopProdUnitMapper, Sho
@Override
public boolean disable(Long id) {
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
return UpdateChain.of(ShopProdUnit.class)
.set(ShopProdUnit::getStatus, StatusEnum.DISABLE.value())
.eq(ShopProdUnit::getId, id)
@@ -99,7 +99,7 @@ public class ShopProdUnitServiceImpl extends ServiceImpl<ShopProdUnitMapper, Sho
@Override
public boolean enable(Long id) {
Long shopId = StpKit.ADMIN.getLoginIdAsLong();
Long shopId = StpKit.USER.getLoginIdAsLong();
return UpdateChain.of(ShopProdUnit.class)
.set(ShopProdUnit::getStatus, StatusEnum.ENABLED.value())
.where(ShopProdUnit::getId).eq(id)

View File

@@ -35,7 +35,7 @@ public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams
return CzgResult.failure("参数编码已存在");
}
Long userId = StpKit.ADMIN.getLoginId(1L);
Long userId = StpKit.USER.getLoginId(1L);
// 新增参数
sysParams = new SysParams();
@@ -60,7 +60,7 @@ public class SysParamsServiceImpl extends ServiceImpl<SysParamsMapper, SysParams
return CzgResult.failure("参数编码已存在");
}
Long userId = StpKit.ADMIN.getLoginId(1L);
Long userId = StpKit.USER.getLoginId(1L);
// 修改参数
sysParams = getById(paramsDTO.getParamCode());

View File

@@ -31,7 +31,7 @@ public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> impl
}
version = BeanUtil.toBean(versionDTO, Version.class);
version.setUpdateUserId(StpKit.ADMIN.getLoginId(1L));
version.setUpdateUserId(StpKit.USER.getLoginId(1L));
save(version);
return CzgResult.success(version.getId());
}
@@ -62,7 +62,7 @@ public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> impl
}
version.setIsForce(versionDTO.getIsForce());
version.setMessage(versionDTO.getMessage());
version.setUpdateUserId(StpKit.ADMIN.getLoginId(1L));
version.setUpdateUserId(StpKit.USER.getLoginId(1L));
updateById(version);
return CzgResult.success(version.getId());