修改鉴权失败错误异常处理

This commit is contained in:
wenxin 2024-12-10 10:15:25 +08:00
parent 486b34a35f
commit 41c9dd07af
4 changed files with 33 additions and 20 deletions

View File

@ -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 仓库

View File

@ -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;
}
}

View File

@ -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<UserAuth> 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<String, List<String>> 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;
}

View File

@ -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<UserAuth> 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<UserAuth> 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);
}