mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-11-28 01:00:05 +08:00
update 优化 sse 异常单独处理 避免出现异常报错问题
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
package org.dromara.common.core.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sse 特制异常
|
||||||
|
*
|
||||||
|
* @author LionLi
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public final class SseException extends RuntimeException {
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误码
|
||||||
|
*/
|
||||||
|
private Integer code;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误提示
|
||||||
|
*/
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误明细,内部调试错误
|
||||||
|
*/
|
||||||
|
private String detailMessage;
|
||||||
|
|
||||||
|
public SseException(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SseException(String message, Integer code) {
|
||||||
|
this.message = message;
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SseException setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SseException setDetailMessage(String detailMessage) {
|
||||||
|
this.detailMessage = detailMessage;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.dromara</groupId>
|
<groupId>org.dromara</groupId>
|
||||||
<artifactId>ruoyi-common-core</artifactId>
|
<artifactId>ruoyi-common-json</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- SpringBoot Web容器 -->
|
<!-- SpringBoot Web容器 -->
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ import jakarta.validation.ConstraintViolationException;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.dromara.common.core.domain.R;
|
import org.dromara.common.core.domain.R;
|
||||||
import org.dromara.common.core.exception.ServiceException;
|
import org.dromara.common.core.exception.ServiceException;
|
||||||
|
import org.dromara.common.core.exception.SseException;
|
||||||
import org.dromara.common.core.exception.base.BaseException;
|
import org.dromara.common.core.exception.base.BaseException;
|
||||||
import org.dromara.common.core.utils.StreamUtils;
|
import org.dromara.common.core.utils.StreamUtils;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.json.utils.JsonUtils;
|
||||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||||
@@ -55,20 +56,25 @@ public class GlobalExceptionHandler {
|
|||||||
return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage());
|
return ObjectUtil.isNotNull(code) ? R.fail(code, e.getMessage()) : R.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 认证失败
|
||||||
|
*/
|
||||||
|
@ResponseStatus(org.springframework.http.HttpStatus.UNAUTHORIZED)
|
||||||
|
@ExceptionHandler(SseException.class)
|
||||||
|
public String handleNotLoginException(SseException e, HttpServletRequest request) {
|
||||||
|
String requestURI = request.getRequestURI();
|
||||||
|
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
||||||
|
return JsonUtils.toJsonString(R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* servlet异常
|
* servlet异常
|
||||||
*/
|
*/
|
||||||
@ExceptionHandler(ServletException.class)
|
@ExceptionHandler(ServletException.class)
|
||||||
public R<Void> handleServletException(ServletException e, HttpServletRequest request) {
|
public R<Void> handleServletException(ServletException e, HttpServletRequest request) {
|
||||||
if (StringUtils.contains(e.getMessage(), "NotLoginException")) {
|
String requestURI = request.getRequestURI();
|
||||||
String requestURI = request.getRequestURI();
|
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
||||||
log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
return R.fail(HttpStatus.HTTP_UNAUTHORIZED, "认证失败,无法访问系统资源");
|
|
||||||
} else {
|
|
||||||
String requestURI = request.getRequestURI();
|
|
||||||
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
|
||||||
return R.fail(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import cn.dev33.satoken.router.SaRouter;
|
|||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.dev33.satoken.util.SaResult;
|
import cn.dev33.satoken.util.SaResult;
|
||||||
import org.dromara.common.core.constant.HttpStatus;
|
import org.dromara.common.core.constant.HttpStatus;
|
||||||
|
import org.dromara.common.core.exception.SseException;
|
||||||
import org.dromara.common.core.utils.SpringUtils;
|
import org.dromara.common.core.utils.SpringUtils;
|
||||||
import org.dromara.common.core.utils.StringUtils;
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
import org.dromara.common.satoken.utils.LoginHelper;
|
import org.dromara.common.satoken.utils.LoginHelper;
|
||||||
@@ -39,11 +40,19 @@ public class AuthFilter {
|
|||||||
SaRouter.match("/**")
|
SaRouter.match("/**")
|
||||||
.notMatch(ignoreWhite.getWhites())
|
.notMatch(ignoreWhite.getWhites())
|
||||||
.check(r -> {
|
.check(r -> {
|
||||||
|
ServerHttpRequest request = SaReactorSyncHolder.getContext().getRequest();
|
||||||
// 检查是否登录 是否有token
|
// 检查是否登录 是否有token
|
||||||
StpUtil.checkLogin();
|
try {
|
||||||
|
StpUtil.checkLogin();
|
||||||
|
} catch (NotLoginException e) {
|
||||||
|
if (request.getURI().getPath().contains("sse")) {
|
||||||
|
throw new SseException(e.getMessage(), e.getCode());
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 检查 header 与 param 里的 clientid 与 token 里的是否一致
|
// 检查 header 与 param 里的 clientid 与 token 里的是否一致
|
||||||
ServerHttpRequest request = SaReactorSyncHolder.getContext().getRequest();
|
|
||||||
String headerCid = request.getHeaders().getFirst(LoginHelper.CLIENT_KEY);
|
String headerCid = request.getHeaders().getFirst(LoginHelper.CLIENT_KEY);
|
||||||
String paramCid = request.getQueryParams().getFirst(LoginHelper.CLIENT_KEY);
|
String paramCid = request.getQueryParams().getFirst(LoginHelper.CLIENT_KEY);
|
||||||
String clientId = StpUtil.getExtra(LoginHelper.CLIENT_KEY).toString();
|
String clientId = StpUtil.getExtra(LoginHelper.CLIENT_KEY).toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user