Procházet zdrojové kódy

获取登录用户的信息

wanggaokun před 1 měsícem
rodič
revize
47be485011

+ 13 - 2
eco-common/com-web/src/main/java/org/eco/vip/web/core/handler/GlobalExceptionHandler.java → eco-common/com-security/src/main/java/org/eco/vip/security/handler/GlobalExceptionHandler.java

@@ -1,15 +1,16 @@
 /*
  * Copyright (c) 2025 GaoKunW
- *
  */
 
-package org.eco.vip.web.core.handler;
+package org.eco.vip.security.handler;
 
 
+import cn.dev33.satoken.exception.NotLoginException;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.exceptions.ExceptionUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpStatus;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.validation.ConstraintViolation;
 import jakarta.validation.ConstraintViolationException;
@@ -116,6 +117,16 @@ public class GlobalExceptionHandler {
         return CommonResult.fail(NOT_FOUND.getCode(), String.format("请求地址不存在:%s", throwable.getResourcePath()));
     }
 
+    /**
+     * 认证失败
+     */
+    @ExceptionHandler(NotLoginException.class)
+    public CommonResult<Void> handleNotLoginException(NotLoginException loginException, HttpServletRequest request) {
+        String requestURI = request.getRequestURI();
+        log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, loginException.getMessage());
+        return CommonResult.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
+    }
+
     /**
      * 系统异常
      */

+ 2 - 1
eco-common/com-security/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

@@ -1,4 +1,5 @@
 org.eco.vip.security.config.SaTokenConfig
 org.eco.vip.security.config.SecurityConfig
 org.eco.vip.security.handler.AllUrlHandler
-org.eco.vip.security.handler.PermissionHandler
+org.eco.vip.security.handler.PermissionHandler
+org.eco.vip.security.handler.GlobalExceptionHandler

+ 26 - 4
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/controller/auth/AuthController.java

@@ -10,21 +10,23 @@ import jakarta.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import org.eco.vip.auth.domain.auth.pojo.AuthQuery;
 import org.eco.vip.auth.domain.auth.pojo.AuthVO;
+import org.eco.vip.auth.domain.auth.pojo.LoginUserVO;
 import org.eco.vip.auth.domain.client.pojo.ClientVO;
+import org.eco.vip.auth.domain.user.pojo.UserVO;
 import org.eco.vip.auth.service.auth.IAuthService;
 import org.eco.vip.auth.service.client.IClientService;
+import org.eco.vip.auth.service.user.IUserService;
 import org.eco.vip.orm.constant.Constants;
 import org.eco.vip.orm.exception.BusinessException;
 import org.eco.vip.orm.pojo.CommonResult;
+import org.eco.vip.orm.pojo.LoginUser;
 import org.eco.vip.orm.utils.JsonUtils;
 import org.eco.vip.orm.utils.ObjUtils;
 import org.eco.vip.orm.utils.StrUtils;
 import org.eco.vip.orm.utils.ValidatorUtils;
+import org.eco.vip.security.utils.LoginHelper;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import static org.eco.vip.orm.pojo.CommonResult.fail;
 import static org.eco.vip.orm.pojo.CommonResult.success;
@@ -47,6 +49,9 @@ public class AuthController {
     @Resource
     private IClientService clientService;
 
+    @Resource
+    private IUserService userService;
+
     @PostMapping("/login")
     public CommonResult<AuthVO> login(@RequestBody String body) {
         AuthQuery authQueryBody = JsonUtils.parseObject(body, AuthQuery.class);
@@ -66,4 +71,21 @@ public class AuthController {
         }
         return success(adminAuthService.login(authQueryBody, clientVO, grantType));
     }
+
+    @GetMapping("/info")
+    public CommonResult<LoginUserVO> getInfo() {
+        LoginUserVO loginUserVO = new LoginUserVO();
+        LoginUser loginUser = LoginHelper.getLoginUser();
+        if (ObjUtils.isNull(loginUser)) {
+            return fail("用户无权限!");
+        }
+        UserVO userVO = userService.selectById(loginUser.getUserId());
+        if (ObjUtils.isNull(userVO)) {
+            return fail("用户无权限!");
+        }
+        loginUserVO.setUser(userVO);
+        loginUserVO.setPermissionCodes(loginUser.getPermissionCodes());
+        loginUserVO.setRoleCodes(loginUser.getRoleCodes());
+        return success(loginUserVO);
+    }
 }

+ 37 - 0
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/domain/auth/pojo/LoginUserVO.java

@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2025 GaoKunW
+ */
+
+package org.eco.vip.auth.domain.auth.pojo;
+
+
+import lombok.Data;
+import org.eco.vip.auth.domain.user.pojo.UserVO;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * @description LoginUserVO
+ *
+ * @author GaoKunW
+ * @date 2025/7/6 23:22
+ */
+@Data
+public class LoginUserVO implements Serializable {
+
+    /**
+     * 用户基本信息
+     */
+    private UserVO user;
+
+    /**
+     * 菜单权限
+     */
+    private Set<String> permissionCodes;
+
+    /**
+     * 角色权限
+     */
+    private Set<String> roleCodes;
+}

+ 4 - 0
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/domain/user/pojo/UserVO.java

@@ -5,6 +5,8 @@
 
 package org.eco.vip.auth.domain.user.pojo;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
 import com.mybatisflex.annotation.Id;
 import io.github.linpeilie.annotations.AutoMapper;
 import lombok.Data;
@@ -78,6 +80,8 @@ public class UserVO extends BaseEntity {
     /**
      * 密码
      */
+    @JsonIgnore
+    @JsonProperty
     private String password;
 
     /**

+ 1 - 1
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/user/IUserService.java

@@ -71,7 +71,7 @@ public interface IUserService extends IBaseService<User> {
      * @param id ID
      * @return 结果
      */
-    RoleVO selectById(String id);
+    UserVO selectById(String id);
 
     UserVO selectTenantUserByUserName(String tenantId, String username);
 }

+ 2 - 2
eco-nexus-core/auth-biz/src/main/java/org/eco/vip/auth/service/user/UserService.java

@@ -87,8 +87,8 @@ public class UserService extends BaseService<UserMapper, User> implements IUserS
     }
 
     @Override
-    public RoleVO selectById(String id) {
-        return null;
+    public UserVO selectById(String id) {
+        return userMapper.selectOneWithRelationsByIdAs(id, UserVO.class);
     }
 
     @Override