Files
longdianningxing/nladmin-common/src/main/java/org/nl/utils/SecurityUtils.java

60 lines
1.4 KiB
Java
Raw Normal View History

2022-07-06 18:32:05 +08:00
package org.nl.utils;
2022-09-21 10:15:52 +08:00
import cn.dev33.satoken.stp.StpUtil;
2022-07-06 18:32:05 +08:00
import lombok.extern.slf4j.Slf4j;
2022-09-21 10:15:52 +08:00
import org.nl.utils.dto.CurrentUser;
2022-07-06 18:32:05 +08:00
/**
2022-09-21 10:15:52 +08:00
* @author: lyd
* @description: 获取当前用户的信息 - 前提下在登录之后将数据存储到session
* @Date:
2022-07-06 18:32:05 +08:00
*/
@Slf4j
public class SecurityUtils {
/**
2022-09-21 10:15:52 +08:00
* 获取系统用户 - CurrentUser
2022-07-06 18:32:05 +08:00
*
2022-09-21 10:15:52 +08:00
* @return 系统用户
2022-07-06 18:32:05 +08:00
*/
2022-09-21 10:15:52 +08:00
public static Object getCurrentUser() {
CurrentUser user = null;
try {
user = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
if (user.getUser() != null) {
return user.getUser();
}
2022-07-06 18:32:05 +08:00
}
2022-09-21 10:15:52 +08:00
catch (Exception e) {
return null;
2022-07-06 18:32:05 +08:00
}
2022-09-21 10:15:52 +08:00
return null;
2022-07-06 18:32:05 +08:00
}
/**
* 获取系统用户名称
*
* @return 系统用户名称
*/
public static String getCurrentUsername() {
2022-09-21 10:15:52 +08:00
CurrentUser currentUser = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
if (currentUser.getUsername() != null) {
return currentUser.getUsername();
2022-07-06 18:32:05 +08:00
}
2022-09-21 10:15:52 +08:00
return "";
2022-07-06 18:32:05 +08:00
}
/**
2022-09-21 10:15:52 +08:00
* 获取系统用户Id
2022-07-06 18:32:05 +08:00
*
2022-09-21 10:15:52 +08:00
* @return 系统用户Id
2022-07-06 18:32:05 +08:00
*/
public static Long getCurrentUserId() {
2022-09-21 10:15:52 +08:00
CurrentUser currentUser = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
if (currentUser.getId() != null) {
return currentUser.getId();
2022-07-06 18:32:05 +08:00
}
2022-09-21 10:15:52 +08:00
return 0L;
2022-07-06 18:32:05 +08:00
}
}