未登陆异常处理
This commit is contained in:
parent
aefee46cd0
commit
36bb1816fc
@ -68,6 +68,10 @@ public class JsonResult<E> implements Serializable {
|
||||
return new JsonResult<String>(Code.FAILED, msg,null);
|
||||
}
|
||||
|
||||
public static JsonResult<String> failed(Integer code,String msg) {
|
||||
return new JsonResult<String>(code, msg,null);
|
||||
}
|
||||
|
||||
public static JsonResult<Object> failed(String msg, Object data) {
|
||||
return new JsonResult<>(Code.FAILED, msg, data);
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.cdzy.gateway;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import com.cdzy.common.enums.Code;
|
||||
import com.cdzy.common.model.response.JsonResult;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
@ -17,12 +18,9 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.reactive.resource.NoResourceFoundException;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 网关统一异常处理
|
||||
*
|
||||
@ -43,20 +41,23 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
||||
}
|
||||
|
||||
String msg;
|
||||
|
||||
if (ex instanceof NotFoundException) {
|
||||
Integer code = Code.FAILED;
|
||||
Throwable cause = ex.getCause();
|
||||
if (cause instanceof NotLoginException) {
|
||||
msg = cause.getMessage();
|
||||
code = Code.UN_AUTHENTICATE;
|
||||
return webFluxResponseWriter(response, msg, code);
|
||||
} else if (ex instanceof NotFoundException) {
|
||||
msg = "服务未找到";
|
||||
} else if (ex instanceof ResponseStatusException responseStatusException) {
|
||||
msg = responseStatusException.getMessage();
|
||||
}else {
|
||||
} else if (ex instanceof NoResourceFoundException) {
|
||||
msg = "路径不存在";
|
||||
} else {
|
||||
msg = "内部服务器错误";
|
||||
}
|
||||
if (ex instanceof NoResourceFoundException) {
|
||||
msg = "路径不存在";
|
||||
}
|
||||
|
||||
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
|
||||
|
||||
return webFluxResponseWriter(response, msg,null);
|
||||
return webFluxResponseWriter(response, msg, code);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,7 +69,7 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value,Integer code) {
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, Objects.requireNonNullElse(code, Code.FAILED));
|
||||
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +110,7 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
||||
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
||||
response.setStatusCode(status);
|
||||
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
||||
JsonResult<String> result = JsonResult.failed(value.toString());
|
||||
JsonResult<String> result = JsonResult.failed(code,value.toString());
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
DataBuffer dataBuffer = null;
|
||||
try {
|
||||
|
||||
@ -3,7 +3,6 @@ package com.cdzy.gateway.config;
|
||||
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.cdzy.common.model.response.JsonResult;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -41,9 +40,7 @@ public class SaTokenConfigure {
|
||||
// SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
|
||||
|
||||
// 更多匹配 ... */
|
||||
})
|
||||
// 异常处理方法:每次setAuth函数出现异常时进入
|
||||
.setError(e -> JsonResult.failed(e.getMessage()));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ package com.cdzy.staff.config;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import com.alibaba.nacos.shaded.com.google.gson.JsonSyntaxException;
|
||||
import com.cdzy.common.enums.Code;
|
||||
import com.cdzy.common.ex.EbikeException;
|
||||
import com.cdzy.common.model.response.JsonResult;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@ -33,7 +34,7 @@ public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(NotLoginException.class)
|
||||
public JsonResult<?> bindExceptionHandler(NotLoginException e) {
|
||||
log.error(e.toString());
|
||||
return JsonResult.failed(e.getMessage());
|
||||
return JsonResult.failed(Code.UN_AUTHENTICATE,e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(BindException.class)
|
||||
|
||||
@ -29,6 +29,7 @@ public class EbikeOperator implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@Column(tenantId = true)
|
||||
private Long operatorId;
|
||||
|
||||
private String operatorName;
|
||||
|
||||
@ -45,43 +45,41 @@ public class EbikeRolePermissionServiceImpl extends ServiceImpl<EbikeRolePermiss
|
||||
public void allocate(EbikeRolePermissionVo ebikeRolePermission) {
|
||||
Long roleId = ebikeRolePermission.getRoleId();
|
||||
checkRole(roleId);
|
||||
List<Long> permissionIds = ebikeRolePermission.getPermissionIds();
|
||||
long loginId = StpUtil.getLoginIdAsLong();
|
||||
if (permissionIds != null) {
|
||||
if (ebikeRolePermission.getPermissionIds()!=null && !ebikeRolePermission.getPermissionIds().isEmpty()) {
|
||||
List<Long> permissionIds = new ArrayList<>(ebikeRolePermission.getPermissionIds().stream().distinct().toList());
|
||||
long loginId = StpUtil.getLoginIdAsLong();
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
.where(EBIKE_ROLE_PERMISSION.ROLE_ID.eq(roleId));
|
||||
rolePermissionMapper.deleteByQuery(queryWrapper);
|
||||
if (!permissionIds.isEmpty()) {
|
||||
//获取所有有效权限id
|
||||
queryWrapper.clear();
|
||||
queryWrapper.where(EBIKE_PERMISSION.PERMISSION_ID.in(permissionIds));
|
||||
List<EbikePermission> ebikePermissions = permissionMapper.selectListByQuery(queryWrapper);
|
||||
//获取所有有效权限id
|
||||
queryWrapper.clear();
|
||||
queryWrapper.where(EBIKE_PERMISSION.PERMISSION_ID.in(permissionIds));
|
||||
List<EbikePermission> ebikePermissions = permissionMapper.selectListByQuery(queryWrapper);
|
||||
|
||||
permissionIds.clear();
|
||||
if (ebikePermissions != null && !ebikePermissions.isEmpty()) {
|
||||
List<Long> parentIds = new ArrayList<>();
|
||||
//父级授权
|
||||
for (EbikePermission ebikePermission : ebikePermissions) {
|
||||
permissionIds.add(ebikePermission.getPermissionId());
|
||||
if (ebikePermission.getParentId() != null) {
|
||||
parentIds.add(ebikePermission.getParentId());
|
||||
}
|
||||
permissionIds.clear();
|
||||
if (ebikePermissions != null && !ebikePermissions.isEmpty()) {
|
||||
List<Long> parentIds = new ArrayList<>();
|
||||
//父级授权
|
||||
for (EbikePermission ebikePermission : ebikePermissions) {
|
||||
permissionIds.add(ebikePermission.getPermissionId());
|
||||
if (ebikePermission.getParentId() != null) {
|
||||
parentIds.add(ebikePermission.getParentId());
|
||||
}
|
||||
List<EbikeRolePermission> rolePermissions = permissionIds.stream().map(permissionId -> EbikeRolePermission.builder()
|
||||
.roleId(roleId)
|
||||
.permissionId(permissionId)
|
||||
.createBy(loginId)
|
||||
.isEmpower(false)
|
||||
.build()).toList();
|
||||
List<EbikeRolePermission> parentRolePermissions = parentIds.stream().map(permissionId -> EbikeRolePermission.builder()
|
||||
.roleId(roleId)
|
||||
.permissionId(permissionId)
|
||||
.createBy(loginId)
|
||||
.isEmpower(true)
|
||||
.build()).toList();
|
||||
rolePermissionMapper.insertBatch(rolePermissions);
|
||||
rolePermissionMapper.insertBatch(parentRolePermissions);
|
||||
}
|
||||
List<EbikeRolePermission> rolePermissions = permissionIds.stream().map(permissionId -> EbikeRolePermission.builder()
|
||||
.roleId(roleId)
|
||||
.permissionId(permissionId)
|
||||
.createBy(loginId)
|
||||
.isEmpower(false)
|
||||
.build()).toList();
|
||||
List<EbikeRolePermission> parentRolePermissions = parentIds.stream().map(permissionId -> EbikeRolePermission.builder()
|
||||
.roleId(roleId)
|
||||
.permissionId(permissionId)
|
||||
.createBy(loginId)
|
||||
.isEmpower(true)
|
||||
.build()).distinct().toList();
|
||||
rolePermissionMapper.insertBatch(rolePermissions);
|
||||
rolePermissionMapper.insertBatch(parentRolePermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -91,8 +89,8 @@ public class EbikeRolePermissionServiceImpl extends ServiceImpl<EbikeRolePermiss
|
||||
if (ebikeRole == null) {
|
||||
throw new EbikeException("该角色不存在");
|
||||
}
|
||||
// if (ebikeRole.getSysAdmin()){
|
||||
// throw new EbikeException("系统管理员无需授权");
|
||||
// }
|
||||
if (ebikeRole.getSysAdmin()){
|
||||
throw new EbikeException("系统管理员无需授权");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user