|
|
@@ -20,12 +20,11 @@ import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.zhengjie.exception.BadRequestException;
|
|
|
-import me.zhengjie.utils.enums.DataScopeEnum;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
-import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
+
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
@@ -41,34 +40,25 @@ public class SecurityUtils {
|
|
|
* @return UserDetails
|
|
|
*/
|
|
|
public static UserDetails getCurrentUser() {
|
|
|
- UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
|
|
|
- return userDetailsService.loadUserByUsername(getCurrentUsername());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取系统用户名称
|
|
|
- *
|
|
|
- * @return 系统用户名称
|
|
|
- */
|
|
|
- public static String getCurrentUsername() {
|
|
|
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
if (authentication == null) {
|
|
|
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
|
|
|
}
|
|
|
if (authentication.getPrincipal() instanceof UserDetails) {
|
|
|
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
|
|
- return userDetails.getUsername();
|
|
|
+ return userDetails;
|
|
|
}
|
|
|
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取系统用户ID
|
|
|
- * @return 系统用户ID
|
|
|
+ * 获取系统用户名称
|
|
|
+ *
|
|
|
+ * @return 系统用户名称
|
|
|
*/
|
|
|
- public static Long getCurrentUserId() {
|
|
|
+ public static String getCurrentUsername() {
|
|
|
UserDetails userDetails = getCurrentUser();
|
|
|
- return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class);
|
|
|
+ return userDetails.getUsername();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -80,16 +70,4 @@ public class SecurityUtils {
|
|
|
JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
|
|
|
return JSONUtil.toList(array,Long.class);
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取数据权限级别
|
|
|
- * @return 级别
|
|
|
- */
|
|
|
- public static String getDataScopeType() {
|
|
|
- List<Long> dataScopes = getCurrentUserDataScope();
|
|
|
- if(dataScopes.size() != 0){
|
|
|
- return "";
|
|
|
- }
|
|
|
- return DataScopeEnum.ALL.getValue();
|
|
|
- }
|
|
|
}
|