refactor: 获取不到当前登录用户信息则抛出未登录异常
修复 Qodana 扫描问题
This commit is contained in:
parent
a2420d3f4b
commit
d972a4466a
@ -16,16 +16,15 @@
|
||||
|
||||
package top.charles7c.continew.admin.common.config.mybatis;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
import top.charles7c.continew.admin.common.model.dto.LoginUser;
|
||||
import top.charles7c.continew.admin.common.util.helper.LoginHelper;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionCurrentUser;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataPermissionFilter;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.datapermission.DataScope;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据权限过滤器实现类
|
||||
*
|
||||
@ -37,15 +36,12 @@ public class DataPermissionFilterImpl implements DataPermissionFilter {
|
||||
@Override
|
||||
public boolean isFilter() {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
return null != loginUser && !loginUser.isAdmin();
|
||||
return !loginUser.isAdmin();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataPermissionCurrentUser getCurrentUser() {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
if (null == loginUser) {
|
||||
throw new IllegalArgumentException("Current user is not allowed to be empty.");
|
||||
}
|
||||
DataPermissionCurrentUser currentUser = new DataPermissionCurrentUser();
|
||||
currentUser.setUserId(Convert.toStr(loginUser.getId()));
|
||||
currentUser.setDeptId(Convert.toStr(loginUser.getDeptId()));
|
||||
|
@ -17,6 +17,7 @@
|
||||
package top.charles7c.continew.admin.common.util.helper;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@ -67,17 +68,16 @@ public class LoginHelper {
|
||||
/**
|
||||
* 获取登录用户信息
|
||||
*
|
||||
* @return 登录用户信息(获取 TokenSession 时如未登录,会抛出异常)
|
||||
* @return 登录用户信息
|
||||
* @throws NotLoginException 未登录异常
|
||||
*/
|
||||
public static LoginUser getLoginUser() {
|
||||
public static LoginUser getLoginUser() throws NotLoginException {
|
||||
StpUtil.checkLogin();
|
||||
LoginUser loginUser = (LoginUser)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_KEY);
|
||||
if (null != loginUser) {
|
||||
return loginUser;
|
||||
}
|
||||
SaSession tokenSession = StpUtil.getTokenSession();
|
||||
if (null == tokenSession) {
|
||||
return null;
|
||||
}
|
||||
loginUser = (LoginUser)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
|
||||
SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, loginUser);
|
||||
return loginUser;
|
||||
@ -103,7 +103,7 @@ public class LoginHelper {
|
||||
* @return 登录用户 ID
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getId());
|
||||
return getLoginUser().getId();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,7 +112,7 @@ public class LoginHelper {
|
||||
* @return 登录用户名
|
||||
*/
|
||||
public static String getUsername() {
|
||||
return ExceptionUtils.exToNull(() -> getLoginUser().getUsername());
|
||||
return getLoginUser().getUsername();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,12 +16,12 @@
|
||||
|
||||
package top.charles7c.continew.admin.system.model.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 消息和用户关联实体
|
||||
@ -33,6 +33,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
||||
@TableName("sys_message_user")
|
||||
public class MessageUserDO implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
|
@ -22,6 +22,7 @@ import top.charles7c.continew.starter.data.mybatis.plus.query.Query;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryIgnore;
|
||||
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryType;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@ -34,6 +35,7 @@ import java.io.Serializable;
|
||||
@Schema(description = "消息查询条件")
|
||||
public class MessageQuery implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user