Commit 9a76ea4c authored by fangxinjiang's avatar fangxinjiang

全局异常处理

parent 82244f9b
......@@ -17,11 +17,14 @@
package com.yifu.cloud.plus.v1.yifu.auth.handler;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.SpringContextHolder;
import com.yifu.cloud.plus.v1.yifu.common.log.event.SysLogEvent;
import com.yifu.cloud.plus.v1.yifu.common.log.util.LogTypeEnum;
import com.yifu.cloud.plus.v1.yifu.common.log.util.SysLogUtils;
import com.yifu.cloud.plus.v1.yifu.common.security.handler.AbstractAuthenticationFailureEventHandler;
import com.yifu.cloud.plus.v1.yifu.common.security.service.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
......@@ -54,6 +57,10 @@ public class YifuAuthenticationFailureEventHandler extends AbstractAuthenticatio
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
YifuUser user = SecurityUtils.getUser();
if (Common.isNotNull(user)){
logVo.setCreateName(user.getNickname());
}
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
......
......@@ -17,10 +17,13 @@
package com.yifu.cloud.plus.v1.yifu.auth.handler;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.SpringContextHolder;
import com.yifu.cloud.plus.v1.yifu.common.log.event.SysLogEvent;
import com.yifu.cloud.plus.v1.yifu.common.log.util.SysLogUtils;
import com.yifu.cloud.plus.v1.yifu.common.security.handler.AbstractAuthenticationSuccessEventHandler;
import com.yifu.cloud.plus.v1.yifu.common.security.service.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
......@@ -50,8 +53,13 @@ public class YifuAuthenticationSuccessEventHandler extends AbstractAuthenticatio
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
YifuUser user = SecurityUtils.getUser();
if (Common.isNotNull(user)){
logVo.setCreateName(user.getNickname());
}
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
}
......
......@@ -7,6 +7,40 @@
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-bom</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.19</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.6.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-core</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-security</artifactId>
</dependency>
</dependencies>
<packaging>pom</packaging>
<name>yifu-common-bom</name>
......@@ -70,11 +104,6 @@
<artifactId>yifu-common-security</artifactId>
<version>${yifu.common.version}</version>
</dependency>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-feign</artifactId>
<version>${yifu.common.version}</version>
</dependency>
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-swagger</artifactId>
......
......@@ -20,6 +20,7 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HttpUtil;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysLog;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
......
......@@ -5,7 +5,6 @@ import com.yifu.cloud.plus.v1.yifu.common.core.exception.ValidateCodeException;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.core.Authentication;
......@@ -29,17 +28,6 @@ import java.util.Set;
@RestControllerAdvice
public class GlobalExceptionHandler {
//指定异常执行方法
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R error(Exception e){
errorLog( e );
if( null != e.getMessage()){
return R.failed("系统异常:"+e.getMessage());
}else{
return R.failed(e,"系统异常!");
}
}
/**
* description:获取用户账号
*
......@@ -68,22 +56,11 @@ public class GlobalExceptionHandler {
public R exception(Exception e) {
errorLog( e );
if( null != e.getMessage()){
return R.failed("系统异常:"+e.getMessage());
return R.failed(e.getMessage());
}else{
return R.failed(e,"系统异常!");
}
}
/*@ExceptionHandler(RateLimitException.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public R exception(RateLimitException e) {
errorLog( e );
if( null != e.getMessage()){
return R.fail("接口限流:"+e.getMessage());
}else{
return R.fail("接口限流:接口访问到达上限,请稍后再试!",e);
}
}*/
@ExceptionHandler({HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
......@@ -152,38 +129,13 @@ public class GlobalExceptionHandler {
* @param e
* @return R
*/
@ExceptionHandler({ValidateCodeException.class,DataException.class})
@ExceptionHandler({ValidateCodeException.class, DataException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R dataExceptionHandler(Exception e) {
errorLog(e);
R result = R.failed("数据异常:"+ e.getMessage());
return result;
}
/**
* 数据库异常 Exception
*
* @param e
* @return R
*/
@ExceptionHandler({MyBatisSystemException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R CommunicationsExceptionHandler(Exception e) {
errorLog(e);
R result = R.failed("数据有误,如检查无误后,请联系管理员!");
return result;
}
/*@ExceptionHandler(CheckedException.class)
@ResponseStatus(HttpStatus.OK)
public R checkedException(CheckedException e) {
errorLog( e );
if( null != e.getMessage()){
return R.fail("系统异常:"+e.getMessage());
}else{
return R.fail("系统异常!",e);
}
}*/
/**
* 异常打印的方法
* @Author pwang
......
......@@ -4,5 +4,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.yifu.cloud.plus.v1.yifu.common.security.component.YifuSecurityInnerAspect,\
com.yifu.cloud.plus.v1.yifu.common.security.component.YifuTokenStoreAutoConfiguration,\
com.yifu.cloud.plus.v1.yifu.common.security.component.YifuTokenStoreAutoCleanSchedule,\
com.yifu.cloud.plus.v1.yifu.common.security.component.YifuSecurityMessageSourceConfiguration
com.yifu.cloud.plus.v1.yifu.common.security.component.YifuSecurityMessageSourceConfiguration,\
com.yifu.cloud.plus.v1.yifu.common.security.exception.GlobalExceptionHandler
......@@ -22,12 +22,12 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.amazonaws.services.s3.model.S3Object;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.oss.OssProperties;
import com.pig4cloud.plugin.oss.service.OssTemplate;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysFile;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysFileMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysFileService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.pig4cloud.plugin.oss.OssProperties;
import com.pig4cloud.plugin.oss.service.OssTemplate;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
......
......@@ -20,15 +20,12 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.dto.SysLogDTO;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysLog;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysMenu;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRoleMenu;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysMenuMapper;
......@@ -47,7 +44,10 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
......
......@@ -20,6 +20,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysPost;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.PostExcelVO;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysPostMapper;
......@@ -27,7 +28,6 @@ import com.yifu.cloud.plus.v1.yifu.admin.service.SysPostService;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
......
......@@ -23,7 +23,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDict;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRole;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysRoleMenu;
......@@ -31,12 +30,10 @@ import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUserRole;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.RoleExcelVO;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysRoleMapper;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysRoleMenuMapper;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysUserMapper;
import com.yifu.cloud.plus.v1.yifu.admin.mapper.SysUserRoleMapper;
import com.yifu.cloud.plus.v1.yifu.admin.service.SysRoleService;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CacheConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.enums.DictTypeEnum;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
......
......@@ -55,6 +55,9 @@ public class GeneratorController {
*/
@GetMapping("/page")
public R<IPage<List<Map<String, Object>>>> getPage(Page page, String tableName, String dsName) {
if (1==1){
throw new RuntimeException("teskdjfaskldfjklsajfdls");
}
return R.ok(generatorService.getPage(page, tableName, dsName));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment