Browse Source

feat: 修改app鉴权接口排序、添加app图标

weijianghai 2 năm trước cách đây
mục cha
commit
f71de17137

+ 35 - 26
src/main/java/com/nokia/config/ControllerExceptionHandler.java

@@ -2,16 +2,18 @@ package com.nokia.config;
 
 import com.nokia.common.R;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.TypeMismatchException;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.http.converter.HttpMessageConversionException;
 import org.springframework.validation.BindException;
 import org.springframework.validation.FieldError;
-import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MissingRequestValueException;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
-import javax.validation.ConstraintViolationException;
+import javax.validation.ValidationException;
 
 /**
  * 请求异常处理
@@ -20,48 +22,55 @@ import javax.validation.ConstraintViolationException;
 @RestControllerAdvice
 public class ControllerExceptionHandler
 {
-    @ExceptionHandler(HttpMessageNotReadableException.class)
-    public ResponseEntity<Object> httpMessageNotReadableExceptionValidatorHandler(HttpMessageNotReadableException e)
+    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
+    public ResponseEntity<Object> httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e)
     {
-        log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", e.getMessage());
+        log.warn(e.getMessage());
+        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(R.error().message(e.getMessage()));
+    }
+
+    @ExceptionHandler(MissingRequestValueException.class)
+    public ResponseEntity<Object> missingRequestValueExceptionHandler(MissingRequestValueException e)
+    {
+        log.warn(e.getMessage());
         return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
     }
 
-    @ExceptionHandler(BindException.class)
-    public ResponseEntity<Object> bindExceptionValidatorHandler(BindException e)
+    @ExceptionHandler(HttpMessageConversionException.class)
+    public ResponseEntity<Object> httpMessageConversionExceptionHandler(HttpMessageConversionException e)
     {
-        FieldError fieldError = e.getBindingResult().getFieldError();
-        String message = "";
-        if (fieldError != null)
-        {
-            message = fieldError.getDefaultMessage();
-            log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
-        }
-        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
+        log.warn(e.getMessage());
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
     }
 
-    @ExceptionHandler(ConstraintViolationException.class)
-    public ResponseEntity<Object> constraintViolationExceptionValidatorHandler(ConstraintViolationException e)
+    @ExceptionHandler(TypeMismatchException.class)
+    public ResponseEntity<Object> typeMismatchExceptionHandler(TypeMismatchException e)
     {
-        String message = e.getConstraintViolations().iterator().next().getMessage();
-        log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
-        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
+        log.warn(e.getMessage());
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
     }
 
-    @ExceptionHandler(MethodArgumentNotValidException.class)
-    public ResponseEntity<Object> methodArgumentNotValidExceptionValidatorHandler(MethodArgumentNotValidException e)
+    @ExceptionHandler(BindException.class)
+    public ResponseEntity<Object> bindExceptionHandler(BindException e)
     {
         FieldError fieldError = e.getBindingResult().getFieldError();
         String message = "";
         if (fieldError != null)
         {
             message = fieldError.getDefaultMessage();
-            log.warn("╭( ′• o •′ )╭☞ 请求参数校验未通过: {}", message);
         }
+        log.warn(message);
         return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(message));
     }
-    
-    @ExceptionHandler({Exception.class})
+
+    @ExceptionHandler(ValidationException.class)
+    public ResponseEntity<Object> validationExceptionHandler(ValidationException e)
+    {
+        log.warn(e.getMessage());
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(R.error().message(e.getMessage()));
+    }
+
+    @ExceptionHandler(Exception.class)
     public ResponseEntity<Object> exceptionHandler(Exception e)
     {
         log.error("╭( ′• o •′ )╭☞ 发生错误了 {}", e.getMessage(), e);

+ 13 - 36
src/main/java/com/nokia/config/LogAspectConfig.java

@@ -1,16 +1,13 @@
 package com.nokia.config;
 
-import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson2.JSON;
 import lombok.extern.slf4j.Slf4j;
-import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;
 import org.slf4j.MDC;
 import org.springframework.stereotype.Component;
-import org.springframework.util.CollectionUtils;
 import org.springframework.util.StopWatch;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -32,11 +29,6 @@ import java.util.UUID;
 @Slf4j
 public class LogAspectConfig
 {
-    @Pointcut("execution(* com.nokia..ControllerExceptionHandler.*ValidatorHandler(..))")
-    public void validatorExceptionHandlerPointcut()
-    {
-        // Pointcut
-    }
 
     @Pointcut("execution(public * com.nokia..*Controller.*(..))")
     public void controllerPointcut()
@@ -50,31 +42,27 @@ public class LogAspectConfig
         // Pointcut
     }
 
-    @Pointcut("controllerPointcut() || validatorExceptionHandlerPointcut()")
-    public void beforePointCut()
-    {
-        // Pointcut
-    }
-
     @Pointcut("controllerPointcut() || controllerExceptionHandlerPointcut()")
-    public void afterPointCut()
+    public void aroundPointCut()
     {
         // Pointcut
     }
 
-    @Before("beforePointCut()")
-    public void doBefore(JoinPoint joinPoint)
+    @Around("aroundPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable
     {
+        StopWatch stopWatch = new StopWatch();
+        stopWatch.start();
+        MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
+        Object result = point.proceed();
         if (attributes == null)
         {
-            return;
+            return result;
         }
+
         HttpServletRequest request = attributes.getRequest();
-        // 打印请求信息
-        log.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
-        // 打印请求参数
-        Object[] args = joinPoint.getArgs();
+        Object[] args = point.getArgs();
         List<Object> list = new ArrayList<>();
         for (Object arg : args) {
             if (arg instanceof ServletRequest
@@ -86,20 +74,9 @@ public class LogAspectConfig
             }
             list.add(arg);
         }
-        if (CollectionUtils.isEmpty(list)) {
-            return;
-        }
-        log.info("请求参数: {}", JSON.toJSONString(list));
-    }
-
-    @Around("afterPointCut()")
-    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
-    {
-        StopWatch stopWatch = new StopWatch();
-        stopWatch.start();
-        MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
-        Object result = proceedingJoinPoint.proceed();
         stopWatch.stop();
+        log.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
+        log.info("请求参数: {}", JSON.toJSONString(list));
         log.info("返回: {}", JSON.toJSONString(result));
         log.info("耗时:{} ms", stopWatch.getTotalTimeMillis());
         return result;

+ 25 - 0
src/main/java/com/nokia/config/ValidatorConfig.java

@@ -0,0 +1,25 @@
+package com.nokia.config;
+
+import org.hibernate.validator.HibernateValidator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+
+/**
+ * 校验配置
+ */
+@Configuration
+public class ValidatorConfig
+{
+    @Bean
+    public Validator validator()
+    {
+        try (ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class).configure()
+                .failFast(true).buildValidatorFactory()) {
+            return validatorFactory.getValidator();
+        }
+    }
+}

+ 20 - 0
src/main/java/com/nokia/controller/TestController.java

@@ -0,0 +1,20 @@
+package com.nokia.controller;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Slf4j
+@RequestMapping("/test")
+@RestController
+public class TestController {
+    /**
+     * 短信告警测试
+     */
+    @GetMapping("/alert")
+    public Object alert() {
+        log.error("短信告警测试");
+        return "ok";
+    }
+}

+ 1 - 1
src/main/java/com/nokia/controller/VerificationController.java

@@ -102,7 +102,7 @@ public class VerificationController {
                             ClientEnum.APP.value);
                     return R.ok().data(vo);
                 default:
-                    return R.error().message("当前允许的system为: liucheng/fenxi/daping/yuce/heidianku/volte_t2/luyin");
+                    return R.error().message("当前允许的system为: liucheng/fenxi/daping/yuce/heidianku/volte_t2/luyin/app");
             }
         }
     }

+ 6 - 1
src/main/java/com/nokia/dao/UserDao.java

@@ -108,7 +108,12 @@ public interface UserDao extends BaseMapper<User> {
                         + " VALUES(#{loginName}, #{userName}, #{phone}, #{email}, #{cityId}, #{areaId}, #{provinceId}, #{org})")
         int insert(User user);
 
-        @Select("select af.id, af.name, af.url from sqmdb_rpt.acl_function af inner join sqmdb_rpt.acl_user_function auf on af.id = auf.function_id where auf.user_id = #{userId} and (af.client = 0 or af.client = #{client}) order by af.id")
+        @Select("select af.id, af.name, af.url, af.app_icon, af.priority from sqmdb_rpt.acl_function af " +
+                "inner join sqmdb_rpt.acl_user_function auf " +
+                "on af.id = auf.function_id " +
+                "where auf.user_id = #{userId} " +
+                "and (af.client = 0 or af.client = #{client}) " +
+                "order by af.priority desc")
         List<AppVerificationVO> listUserFunctions(Integer userId, Integer client);
 
         @Select("select * from sqmdb_rpt.acl_user order by user_id")

+ 2 - 0
src/main/java/com/nokia/vo/AppVerificationVO.java

@@ -11,4 +11,6 @@ public class AppVerificationVO {
     private Integer id;
     private String name;
     private String url;
+    private String appIcon;
+    private Integer priority;
 }