From 41c9dd07af355beae0b455b42aa5728e8fa7e030 Mon Sep 17 00:00:00 2001 From: wenxin <1731551615@qq.com> Date: Tue, 10 Dec 2024 10:15:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=89=B4=E6=9D=83=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E9=94=99=E8=AF=AF=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- .../linxyun/core/common/enums/ErrorCode.java | 11 ++++++++- .../core/interceptor/SecurityInterceptor.java | 15 ++++++------ .../java/com/linxyun/core/utils/ApiUtils.java | 23 +++++++++++-------- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 83cfb9d..9e419b0 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# Spring-boot-starter-linxyun 林栖云依赖 +# Linxyun-sso-java 林栖云依赖 - 拉取项目 ``` - git clone http://codegit.linxyun.com/wenxin/spring-boot-starter-linxyun.git + git clone http://codegit.linxyun.com/wenxin/linxyun-sso-java.git ``` - 添加到本地 maven 仓库 diff --git a/src/main/java/com/linxyun/core/common/enums/ErrorCode.java b/src/main/java/com/linxyun/core/common/enums/ErrorCode.java index a06de95..069647f 100644 --- a/src/main/java/com/linxyun/core/common/enums/ErrorCode.java +++ b/src/main/java/com/linxyun/core/common/enums/ErrorCode.java @@ -93,7 +93,7 @@ public enum ErrorCode { FILE_UPLOAD_FAILED("3002", "文件上传失败"), // 请求失败 REQUEST_FAILED("3003", "请求失败"), - NONE("6666666", "站位"); + NONE("9999", "异常错误"); @@ -114,4 +114,13 @@ public enum ErrorCode { } return "未知错误"; } + + public static ErrorCode getErrorCodeByCode(String code) { + for (ErrorCode errorCode : values()) { + if (errorCode.getCode().equals(code)) { + return errorCode; + } + } + return ErrorCode.NONE; + } } diff --git a/src/main/java/com/linxyun/core/interceptor/SecurityInterceptor.java b/src/main/java/com/linxyun/core/interceptor/SecurityInterceptor.java index 1302421..3c25531 100644 --- a/src/main/java/com/linxyun/core/interceptor/SecurityInterceptor.java +++ b/src/main/java/com/linxyun/core/interceptor/SecurityInterceptor.java @@ -61,18 +61,17 @@ public class SecurityInterceptor implements HandlerInterceptor { token = matcher.group(); } } - UserAuth userAuth = ApiUtils.getUserAuth(token); - if (userAuth == null) { - // 如果为空,说明 token 无效 - log.info("Token 无效:{}", token); - result = Result.error(ErrorCode.LOGIN_VALIDATION_ERROR); - response.getWriter().write(JSON.toJSONString(result)); + Result authRt = ApiUtils.getUserAuth(token); + assert authRt != null; // 断言 + if (!authRt.isSuccess()) { + response.getWriter().write(JSON.toJSONString(authRt)); return false; } + UserAuth userAuth = authRt.getData(); String userRole = userAuth.getUserRoles(); if (StringUtils.isEmpty(userRole)) { log.info("用户权限为空:{}", userAuth.getUserRoles()); - result = Result.error(ErrorCode.LOGIN_VALIDATION_ERROR); + result = Result.error(ErrorCode.USER_NO_AUTHORITY); response.getWriter().write(JSON.toJSONString(result)); return false; } @@ -86,7 +85,7 @@ public class SecurityInterceptor implements HandlerInterceptor { Map> roleMap = linxyunProperties.getRole(); if (!roleMap.containsKey(userAuth.getUserRoles())) { log.info("用户权限未在系统权限中:{}", userAuth.getUserRoles()); - result = Result.error(ErrorCode.LOGIN_VALIDATION_ERROR); + result = Result.error(ErrorCode.USER_NO_AUTHORITY); response.getWriter().write(JSON.toJSONString(result)); return false; } diff --git a/src/main/java/com/linxyun/core/utils/ApiUtils.java b/src/main/java/com/linxyun/core/utils/ApiUtils.java index 46e3ece..cc3b7cb 100644 --- a/src/main/java/com/linxyun/core/utils/ApiUtils.java +++ b/src/main/java/com/linxyun/core/utils/ApiUtils.java @@ -3,8 +3,11 @@ package com.linxyun.core.utils; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpUtil; +import cn.hutool.http.body.RequestBody; import com.alibaba.fastjson2.JSONObject; +import com.linxyun.core.common.entity.Result; import com.linxyun.core.common.entity.UserAuth; +import com.linxyun.core.common.enums.ErrorCode; import com.linxyun.core.properties.LinxyunProperties; import lombok.extern.slf4j.Slf4j; import net.jodah.expiringmap.ExpirationPolicy; @@ -39,10 +42,10 @@ public class ApiUtils { /** * 单点登录 */ - public static UserAuth userLoginAuth(String token) { + public static Result userLoginAuth(String token) { try { if (StringUtils.isEmpty(token)) { - return null; + return Result.error(ErrorCode.USER_NOT_LOGGED_IN); } String url = getApiUrl("userLoginAuth"); JSONObject body = new JSONObject(); @@ -50,36 +53,38 @@ public class ApiUtils { JSONObject result = HttpUtils.post(url, body); if (result == null) { log.error("LinxyunUtils-userLoginAuth result is null"); - return null; + return Result.error(ErrorCode.REQUEST_FAILED); } log.info("LinxyunUtils-userLoginAuth result: {}", result); if (!result.getBoolean("success")) { + String code = result.getString("code"); String msg = result.getString("msg"); log.error("LinxyunUtils-userLoginAuth result is not success: {}", msg); - return null; + return Result.error(ErrorCode.getErrorCodeByCode(code)); } String data = result.getString("data"); if (StringUtils.isEmpty(data)) { log.error("LinxyunUtils-userLoginAuth result.data is null"); - return null; + return Result.error(ErrorCode.OPERATION_ERROR); } UserAuth userAuth = JSONObject.parseObject(data, UserAuth.class); USER_AUTH_MAP.put(token, userAuth); - return userAuth; + return Result.ok(userAuth); } catch (Exception e) { log.error("linxyunUtils.userLoginAuth error: {}", e.getMessage()); - return null; + return Result.error(ErrorCode.TIMEOUT_ERROR); } } - public static UserAuth getUserAuth(String token) { + public static Result getUserAuth(String token) { if (StringUtils.isEmpty(token)) { return null; } boolean isExist = USER_AUTH_MAP.containsKey(token); if (isExist) { // 存在,直接获取缓存的数据 - return USER_AUTH_MAP.get(token); + UserAuth userAuth = USER_AUTH_MAP.get(token); + return Result.ok(userAuth); } return userLoginAuth(token); }