Răsfoiți Sursa

Merge remote-tracking branch 'origin/master' into V1.0.1

qizai 1 an în urmă
părinte
comite
647c60f43e
20 a modificat fișierele cu 153 adăugiri și 94 ștergeri
  1. 0 8
      framework-base/pom.xml
  2. 31 17
      framework-base/src/main/java/com/chelvc/framework/base/interceptor/GlobalExceptionInterceptor.java
  3. 1 1
      framework-boot/pom.xml
  4. 46 15
      framework-common/src/main/java/com/chelvc/framework/common/util/ObjectUtils.java
  5. 17 19
      framework-database/src/main/java/com/chelvc/framework/database/interceptor/DatabaseExceptionInterceptor.java
  6. 0 12
      framework-dependencies/pom.xml
  7. 5 0
      framework-location/src/main/java/com/chelvc/framework/location/Address.java
  8. 8 2
      framework-location/src/main/java/com/chelvc/framework/location/support/JuheMobileHandler.java
  9. 2 1
      framework-location/src/main/java/com/chelvc/framework/location/support/TencentLocationHandler.java
  10. 5 0
      framework-location/src/main/java/com/chelvc/framework/location/support/TencentLocationResponse.java
  11. 8 6
      framework-oauth/src/main/java/com/chelvc/framework/oauth/interceptor/OauthExceptionInterceptor.java
  12. 1 1
      framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/config/RocketMQConfigurer.java
  13. 0 1
      framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/MultipleRocketMQListenerContainer.java
  14. 2 1
      framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/RocketMQListener.java
  15. 0 1
      framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/RocketMQListenerContainer.java
  16. 0 1
      framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/SingleRocketMQListenerContainer.java
  17. 10 0
      framework-security/src/main/java/com/chelvc/framework/security/config/SecurityProperties.java
  18. 4 1
      framework-security/src/main/java/com/chelvc/framework/security/interceptor/RequestSecurityInterceptor.java
  19. 8 6
      framework-security/src/main/java/com/chelvc/framework/security/interceptor/SecurityExceptionInterceptor.java
  20. 5 1
      framework-security/src/main/java/com/chelvc/framework/security/interceptor/SignatureValidateInterceptor.java

+ 0 - 8
framework-base/pom.xml

@@ -68,13 +68,5 @@
             <artifactId>framework-common</artifactId>
             <version>${framework-common.version}</version>
         </dependency>
-        <dependency>
-            <groupId>org.apache.skywalking</groupId>
-            <artifactId>apm-toolkit-trace</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.skywalking</groupId>
-            <artifactId>apm-toolkit-logback-1.x</artifactId>
-        </dependency>
     </dependencies>
 </project>

+ 31 - 17
framework-base/src/main/java/com/chelvc/framework/base/interceptor/GlobalExceptionInterceptor.java

@@ -142,18 +142,19 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
     /**
      * 将异常转换成结果对象
      *
-     * @param e 异常对象
+     * @param request Http请求对象
+     * @param e       异常对象
      * @return 结果对象
      */
-    private Result<?> error2result(Throwable e) {
+    private Result<?> error2result(HttpServletRequest request, Throwable e) {
         if (e instanceof CompletionException) {
-            return this.error2result(e.getCause());
+            return this.error2result(request, e.getCause());
         } else if (e instanceof HttpRequestMethodNotSupportedException) {
-            return Result.build(HttpStatus.METHOD_NOT_ALLOWED);
+            return Result.build(HttpStatus.METHOD_NOT_ALLOWED, null, e.getMessage());
         } else if (e instanceof HttpMediaTypeNotAcceptableException) {
-            return Result.build(HttpStatus.NOT_ACCEPTABLE);
+            return Result.build(HttpStatus.NOT_ACCEPTABLE, null, e.getMessage());
         } else if (e instanceof HttpMediaTypeNotSupportedException) {
-            return Result.build(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
+            return Result.build(HttpStatus.UNSUPPORTED_MEDIA_TYPE, null, e.getMessage());
         } else if (e instanceof HttpMessageNotReadableException) {
             Map<String, String> errors = this.format((HttpMessageNotReadableException) e);
             return CollectionUtils.isEmpty(errors) ? Result.build(HttpStatus.BAD_REQUEST) :
@@ -183,16 +184,26 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
             return Result.build(HttpStatus.PRECONDITION_FAILED, ImmutableMap.of(
                     ((MissingServletRequestPartException) e).getRequestPartName(), e.getMessage()
             ));
-        } else if (e instanceof ValidationException) {
+        } else if (e instanceof ValidationException || e instanceof TypeMismatchException
+                || e instanceof MultipartException || e instanceof HttpMessageConversionException
+                || e instanceof ServletRequestBindingException) {
             return Result.build(HttpStatus.BAD_REQUEST, null, e.getMessage());
-        } else if (e instanceof TypeMismatchException || e instanceof MultipartException
-                || e instanceof HttpMessageConversionException || e instanceof ServletRequestBindingException) {
-            return Result.build(HttpStatus.BAD_REQUEST);
-        } else if (e instanceof FrameworkException) {
-            FrameworkException exception = (FrameworkException) e;
-            return Result.build(exception.getStatus(), exception.getData(), exception.getMessage());
         }
-        return Result.build(HttpStatus.INTERNAL_SERVER_ERROR);
+
+        // 未知异常处理
+        Object data = null;
+        String message = e.getMessage();
+        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
+        if (e instanceof FrameworkException) {
+            data = ((FrameworkException) e).getData();
+            status = ((FrameworkException) e).getStatus();
+        }
+
+        // 如果是5xx异常且需要返回包装类型则屏蔽详细的异常信息
+        if (status.is5xxServerError() && SessionContextHolder.isResponseWrappingHeader(request)) {
+            message = status.getReasonPhrase();
+        }
+        return Result.build(status, data, message);
     }
 
     /**
@@ -222,16 +233,19 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
     /**
      * 异常方法
      *
-     * @param request Http请求对象
+     * @param request  Http请求对象
+     * @param response Http响应对象
      * @return 异常结果
      */
     @RequestMapping("${server.error.path:${error.path:/error}}")
-    public Result<?> error(HttpServletRequest request) {
+    public Result<?> error(HttpServletRequest request, HttpServletResponse response) {
         HttpStatus status = this.getStatus(request);
         Map<String, Object> attributes = this.getErrorAttributes(request, this.options);
         String uri = (String) attributes.get("path");
         String message = (String) attributes.get("message");
         log.warn(SessionContextHolder.getLoggingMessage(uri, request.getMethod(), status.value(), message));
+        response.setStatus(status.value());
+        SessionContextHolder.initializeResponseWrappingHeader(response);
         return Result.build(status, null, message);
     }
 
@@ -245,7 +259,7 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
      */
     @ExceptionHandler(Exception.class)
     public Result<?> exception(HttpServletRequest request, HttpServletResponse response, Exception exception) {
-        Result<?> result = this.error2result(exception);
+        Result<?> result = this.error2result(request, exception);
         HttpStatus status = HttpUtils.getStatus(result.getCode());
         this.log(request, status, exception);
         response.setStatus(status.value());

+ 1 - 1
framework-boot/pom.xml

@@ -69,7 +69,7 @@
                 <artifactId>apidoc-maven-plugin</artifactId>
                 <version>${apidoc-maven-plugin.version}</version>
                 <configuration>
-                    <includeHeaders>{String} Using 使用信息: NORMAL(常规使用)、NEWLY(当日首次使用)、INITIAL(历史首次使用),{String} Device 设备标识,{String} Channel 渠道来源,{String} Platform 平台标识: PC(PC)、IOS(苹果)、ANDROID(安卓),{String} Terminal 终端标识: WEB(Web)、APP(App)、APPLET(小程序),{String} Version 终端版本,{Long} Timestamp 请求时间戳,{String} Signature 签名信息,{String} Fingerprint 设备指纹,{String} Authorization 认证信息</includeHeaders>
+                    <includeHeaders>{String} using 使用信息: NORMAL(常规使用)、NEWLY(当日首次使用)、INITIAL(历史首次使用),{String} device 设备标识,{String} channel 渠道来源,{String} platform 平台标识: PC(PC)、IOS(苹果)、ANDROID(安卓),{String} terminal 终端标识: WEB(Web)、APP(App)、APPLET(小程序),{String} version 终端版本,{Long} timestamp 请求时间戳,{String} signature 签名信息,{String} fingerprint 设备指纹,{String} authorization 认证信息</includeHeaders>
                     <excludeClasses>com.chelvc.framework.base.interceptor.GlobalExceptionInterceptor</excludeClasses>
                     <analyserFactoryClass>com.chelvc.framework.base.apidoc.MethodAnalyserFactory</analyserFactoryClass>
                 </configuration>

+ 46 - 15
framework-common/src/main/java/com/chelvc/framework/common/util/ObjectUtils.java

@@ -353,7 +353,7 @@ public final class ObjectUtils {
      * 返回不为空的对象(如果第一个对象为空,则返回第二个对象)
      *
      * @param object   目标对象
-     * @param supplier 替换对象提供方法
+     * @param supplier 默认对象提供函数
      * @param <T>      对象类型泛型
      * @return 返回对象
      */
@@ -365,7 +365,7 @@ public final class ObjectUtils {
      * 返回不为空的对象(如果第一个对象为空,则返回第二个对象)
      *
      * @param object   目标对象
-     * @param function 目标对象方法
+     * @param function 对象处理函数
      * @param <T>      目标对象类型泛型
      * @param <R>      返回对象类型泛型
      * @return 返回对象
@@ -378,8 +378,8 @@ public final class ObjectUtils {
      * 返回不为空的对象(如果第一个对象为空,则返回第二个对象)
      *
      * @param object   目标对象
-     * @param function 目标对象方法
-     * @param supplier 替换对象提供方法
+     * @param function 对象处理函数
+     * @param supplier 默认对象提供函数
      * @param <T>      目标对象类型泛型
      * @param <R>      返回对象类型泛型
      * @return 返回对象
@@ -591,7 +591,7 @@ public final class ObjectUtils {
      * 判断数组是否为空,如果数组为空则调用适配函数并返回
      *
      * @param array    对象数组
-     * @param supplier 对象数组适配函数
+     * @param supplier 默认数组提供函数
      * @param <T>      对象类型
      * @return 对象数组
      */
@@ -602,21 +602,35 @@ public final class ObjectUtils {
     /**
      * 判断数组是否为空,如果集合非空则使用适配函数
      *
-     * @param array   对象数组
-     * @param adapter 对象数组适配函数
-     * @param <T>     对象类型
-     * @param <R>     目标类型
+     * @param array    对象数组
+     * @param function 数组处理函数
+     * @param <T>      对象类型
+     * @param <R>      目标类型
+     * @return 对象数组
+     */
+    public static <T, R> R[] ifEmpty(T[] array, @NonNull Function<T[], R[]> function) {
+        return isEmpty(array) ? null : function.apply(array);
+    }
+
+    /**
+     * 判断数组是否为空,如果集合非空则使用适配函数
+     *
+     * @param array    对象数组
+     * @param function 数组处理函数
+     * @param supplier 默认数组提供函数
+     * @param <T>      对象类型
+     * @param <R>      目标类型
      * @return 对象数组
      */
-    public static <T, R> R[] ifEmpty(T[] array, @NonNull Function<T[], R[]> adapter) {
-        return isEmpty(array) ? null : adapter.apply(array);
+    public static <T, R> R[] ifEmpty(T[] array, @NonNull Function<T[], R[]> function, @NonNull Supplier<R[]> supplier) {
+        return isEmpty(array) ? supplier.get() : ifEmpty(function.apply(array), supplier);
     }
 
     /**
      * 判断集合是否为空,如果集合为空则调用适配函数并返回
      *
      * @param collection 对象集合
-     * @param supplier   对象集合适配函数
+     * @param supplier   默认集合提供函数
      * @param <T>        对象类型
      * @param <C>        集合类型
      * @return 对象集合
@@ -629,14 +643,31 @@ public final class ObjectUtils {
      * 判断集合是否为空,如果集合非空则使用适配函数
      *
      * @param collection 对象集合
-     * @param adapter    对象集合适配函数
+     * @param function   集合处理函数
+     * @param <T>        对象类型
+     * @param <R>        目标类型
+     * @param <C>        集合类型
+     * @return 对象集合
+     */
+    public static <T, R, C extends Collection<T>> List<R> ifEmpty(C collection,
+                                                                  @NonNull Function<C, List<R>> function) {
+        return isEmpty(collection) ? Collections.emptyList() : function.apply(collection);
+    }
+
+    /**
+     * 判断集合是否为空,如果集合非空则使用适配函数
+     *
+     * @param collection 对象集合
+     * @param function   集合处理函数
+     * @param supplier   默认集合提供函数
      * @param <T>        对象类型
      * @param <R>        目标类型
      * @param <C>        集合类型
      * @return 对象集合
      */
-    public static <T, R, C extends Collection<T>> List<R> ifEmpty(C collection, @NonNull Function<C, List<R>> adapter) {
-        return isEmpty(collection) ? Collections.emptyList() : adapter.apply(collection);
+    public static <T, R, C extends Collection<T>> List<R> ifEmpty(C collection, @NonNull Function<C, List<R>> function,
+                                                                  @NonNull Supplier<List<R>> supplier) {
+        return isEmpty(collection) ? supplier.get() : ifEmpty(function.apply(collection), supplier);
     }
 
     /**

+ 17 - 19
framework-database/src/main/java/com/chelvc/framework/database/interceptor/DatabaseExceptionInterceptor.java

@@ -29,41 +29,39 @@ public class DatabaseExceptionInterceptor {
     /**
      * 参数异常转换
      *
-     * @param response  请求响应对象
-     * @param exception 异常对象
+     * @param request  Http请求对象
+     * @param response Http响应对象
+     * @param e        异常对象
      * @return 请求结果
      */
     @ResponseStatus(HttpStatus.PRECONDITION_FAILED)
     @ExceptionHandler(ParameterInvalidException.class)
-    public Result<?> exception(HttpServletRequest request, HttpServletResponse response,
-                               ParameterInvalidException exception) {
-        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.PRECONDITION_FAILED, exception));
-        Result<?> result = Result.build(HttpStatus.PRECONDITION_FAILED, exception.getData());
-        response.setStatus(result.getCode());
+    public Result<?> exception(HttpServletRequest request, HttpServletResponse response, ParameterInvalidException e) {
+        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.PRECONDITION_FAILED, e));
         SessionContextHolder.initializeResponseWrappingHeader(response);
-        return result;
+        return Result.build(HttpStatus.PRECONDITION_FAILED, e.getData());
     }
 
     /**
      * 数据持久化异常转换
      *
-     * @param response  请求响应对象
-     * @param exception 异常对象
+     * @param request  Http请求对象
+     * @param response Http响应对象
+     * @param e        异常对象
      * @return 请求结果
      */
     @ExceptionHandler(MyBatisSystemException.class)
-    public Result<?> exception(HttpServletRequest request, HttpServletResponse response,
-                               MyBatisSystemException exception) {
-        ParameterInvalidException parameterInvalidException =
-                ErrorUtils.lookup(exception, ParameterInvalidException.class);
+    public Result<?> exception(HttpServletRequest request, HttpServletResponse response, MyBatisSystemException e) {
+        ParameterInvalidException parameterInvalidException = ErrorUtils.lookup(e, ParameterInvalidException.class);
         if (parameterInvalidException != null) {
+            response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
             return this.exception(request, response, parameterInvalidException);
         }
-        String message = SessionContextHolder.getLoggingMessage(request, HttpStatus.INTERNAL_SERVER_ERROR, exception);
-        log.error(message, exception);
-        Result<?> result = Result.build(HttpStatus.INTERNAL_SERVER_ERROR);
-        response.setStatus(result.getCode());
+        log.error(SessionContextHolder.getLoggingMessage(request, HttpStatus.INTERNAL_SERVER_ERROR, e), e);
+        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
         SessionContextHolder.initializeResponseWrappingHeader(response);
-        return result;
+        boolean wrapping = SessionContextHolder.isResponseWrappingHeader(request);
+        String message = wrapping ? HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase() : e.getMessage();
+        return Result.build(HttpStatus.INTERNAL_SERVER_ERROR, null, message);
     }
 }

+ 0 - 12
framework-dependencies/pom.xml

@@ -51,8 +51,6 @@
         <mybatis-plus-boot-starter.version>3.4.2</mybatis-plus-boot-starter.version>
         <dynamic-datasource-spring-boot-starter.version>3.3.2</dynamic-datasource-spring-boot-starter.version>
 
-        <skywalking-trace.version>8.11.0</skywalking-trace.version>
-        <skywalking-logback.version>8.11.0</skywalking-logback.version>
         <dubbo-spring-boot-starter.version>3.0.12</dubbo-spring-boot-starter.version>
         <nacos-spring-context.version>0.3.6</nacos-spring-context.version>
         <nacos-config-spring-boot-starter.version>0.2.1</nacos-config-spring-boot-starter.version>
@@ -240,16 +238,6 @@
                 <artifactId>spring-security-oauth2</artifactId>
                 <version>${spring-security-oauth2.version}</version>
             </dependency>
-            <dependency>
-                <groupId>org.apache.skywalking</groupId>
-                <artifactId>apm-toolkit-trace</artifactId>
-                <version>${skywalking-trace.version}</version>
-            </dependency>
-            <dependency>
-                <groupId>org.apache.skywalking</groupId>
-                <artifactId>apm-toolkit-logback-1.x</artifactId>
-                <version>${skywalking-logback.version}</version>
-            </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-dependencies</artifactId>

+ 5 - 0
framework-location/src/main/java/com/chelvc/framework/location/Address.java

@@ -30,6 +30,11 @@ public class Address implements Serializable {
      */
     private String name;
 
+    /**
+     * 详细地址
+     */
+    private String address;
+
     /**
      * 经度
      */

+ 8 - 2
framework-location/src/main/java/com/chelvc/framework/location/support/JuheMobileHandler.java

@@ -2,6 +2,7 @@ package com.chelvc.framework.location.support;
 
 import java.time.Duration;
 import java.util.Date;
+import java.util.Objects;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -68,8 +69,13 @@ public class JuheMobileHandler implements MobileHandler {
         }
         String province = ObjectUtils.ifNull(response.getResult(), JuheLocationResponse.Position::getProvince);
         String city = ObjectUtils.ifNull(response.getResult(), JuheLocationResponse.Position::getCity);
-        String address = Stream.of(province, city).filter(StringUtils::nonEmpty).collect(Collectors.joining());
-        return StringUtils.ifEmpty(address, (String) null);
+        if (StringUtils.isEmpty(province) && StringUtils.isEmpty(city)) {
+            return null;
+        } else if (Objects.equals(province, city)) {
+            // 针对直辖市省、市名称相同的地址只返回市名称
+            return city;
+        }
+        return Stream.of(province, city).filter(StringUtils::nonEmpty).collect(Collectors.joining());
     }
 
     @Override

+ 2 - 1
framework-location/src/main/java/com/chelvc/framework/location/support/TencentLocationHandler.java

@@ -82,7 +82,8 @@ public class TencentLocationHandler implements LocationHandler {
         TencentLocationResponse.Location location = response.getResult().getLocation();
         Double longitude = ObjectUtils.ifNull(location, TencentLocationResponse.Location::getLongitude);
         Double latitude = ObjectUtils.ifNull(location, TencentLocationResponse.Location::getLatitude);
-        Address address = Address.builder().id(region.getCode()).longitude(longitude).latitude(latitude).build();
+        Address address = Address.builder().id(region.getCode()).longitude(longitude).latitude(latitude)
+                .address(response.getResult().getAddress()).build();
         address.setName(Stream.of(position.getProvince(), position.getCity(), position.getDistrict())
                 .filter(StringUtils::nonEmpty).collect(Collectors.joining("·")
                 ));

+ 5 - 0
framework-location/src/main/java/com/chelvc/framework/location/support/TencentLocationResponse.java

@@ -42,6 +42,11 @@ public class TencentLocationResponse implements Serializable {
     @NoArgsConstructor
     @AllArgsConstructor
     public static class Result implements Serializable {
+        /**
+         * 详细地址
+         */
+        private String address;
+
         /**
          * 定位信息
          */

+ 8 - 6
framework-oauth/src/main/java/com/chelvc/framework/oauth/interceptor/OauthExceptionInterceptor.java

@@ -27,16 +27,18 @@ public class OauthExceptionInterceptor {
     /**
      * 认证异常转换
      *
-     * @param response  请求响应对象
-     * @param exception 异常对象
+     * @param request  Http请求对象
+     * @param response Http响应对象
+     * @param e        异常对象
      * @return 请求结果
      */
     @ResponseStatus(HttpStatus.UNAUTHORIZED)
     @ExceptionHandler(BadCredentialsException.class)
-    public Result<?> exception(HttpServletRequest request, HttpServletResponse response,
-                               BadCredentialsException exception) {
-        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.UNAUTHORIZED, exception));
+    public Result<?> exception(HttpServletRequest request, HttpServletResponse response, BadCredentialsException e) {
+        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.UNAUTHORIZED, e));
         SessionContextHolder.initializeResponseWrappingHeader(response);
-        return Result.build(HttpStatus.UNAUTHORIZED);
+        boolean wrapping = SessionContextHolder.isResponseWrappingHeader(request);
+        String message = wrapping ? HttpStatus.UNAUTHORIZED.getReasonPhrase() : e.getMessage();
+        return Result.build(HttpStatus.UNAUTHORIZED, null, message);
     }
 }

+ 1 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/config/RocketMQConfigurer.java

@@ -5,9 +5,9 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import com.chelvc.framework.rocketmq.annotation.RocketMQConsumer;
 import com.chelvc.framework.rocketmq.consumer.MultipleRocketMQListenerContainer;
+import com.chelvc.framework.rocketmq.consumer.RocketMQListener;
 import com.chelvc.framework.rocketmq.consumer.RocketMQListenerContainer;
 import com.chelvc.framework.rocketmq.consumer.SingleRocketMQListenerContainer;
-import com.chelvc.framework.rocketmq.interceptor.RocketMQListener;
 import com.chelvc.framework.rocketmq.producer.DefaultRocketMQProducerFactory;
 import com.chelvc.framework.rocketmq.producer.RocketMQProducerFactory;
 import lombok.RequiredArgsConstructor;

+ 0 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/MultipleRocketMQListenerContainer.java

@@ -13,7 +13,6 @@ import com.chelvc.framework.common.util.ThreadUtils;
 import com.chelvc.framework.rocketmq.annotation.RocketMQConsumer;
 import com.chelvc.framework.rocketmq.config.RocketMQProperties;
 import com.chelvc.framework.rocketmq.context.RocketMQContextHolder;
-import com.chelvc.framework.rocketmq.interceptor.RocketMQListener;
 import lombok.NonNull;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.rocketmq.client.apis.ClientConfiguration;

+ 2 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/interceptor/RocketMQListener.java → framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/RocketMQListener.java

@@ -1,10 +1,11 @@
-package com.chelvc.framework.rocketmq.interceptor;
+package com.chelvc.framework.rocketmq.consumer;
 
 import java.util.List;
 
 /**
  * RocketMQ消息消费监听接口
  *
+ * @param <T> 消息类型
  * @author Woody
  * @date 2023/1/1
  */

+ 0 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/RocketMQListenerContainer.java

@@ -2,7 +2,6 @@ package com.chelvc.framework.rocketmq.consumer;
 
 import com.chelvc.framework.rocketmq.annotation.RocketMQConsumer;
 import com.chelvc.framework.rocketmq.config.RocketMQProperties;
-import com.chelvc.framework.rocketmq.interceptor.RocketMQListener;
 import org.apache.rocketmq.client.apis.ClientConfiguration;
 import org.apache.rocketmq.client.apis.ClientServiceProvider;
 import org.springframework.beans.factory.DisposableBean;

+ 0 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/consumer/SingleRocketMQListenerContainer.java

@@ -9,7 +9,6 @@ import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.rocketmq.annotation.RocketMQConsumer;
 import com.chelvc.framework.rocketmq.config.RocketMQProperties;
 import com.chelvc.framework.rocketmq.context.RocketMQContextHolder;
-import com.chelvc.framework.rocketmq.interceptor.RocketMQListener;
 import lombok.NonNull;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.tuple.Pair;

+ 10 - 0
framework-security/src/main/java/com/chelvc/framework/security/config/SecurityProperties.java

@@ -44,6 +44,11 @@ public class SecurityProperties {
          */
         private String[] permit;
 
+        /**
+         * 必须校验的资源地址,多个地址使用","号隔开
+         */
+        private String[] require;
+
         /**
          * 允许的请求耗时(毫秒)
          */
@@ -59,5 +64,10 @@ public class SecurityProperties {
          * 放行资源地址,多个地址使用","号隔开
          */
         private String[] permit;
+
+        /**
+         * 必须校验的资源地址,多个地址使用","号隔开
+         */
+        private String[] require;
     }
 }

+ 4 - 1
framework-security/src/main/java/com/chelvc/framework/security/interceptor/RequestSecurityInterceptor.java

@@ -13,6 +13,7 @@ import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.model.Result;
 import com.chelvc.framework.base.util.HttpUtils;
 import com.chelvc.framework.base.util.SpringUtils;
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.security.config.SecurityProperties;
 import com.chelvc.framework.security.context.SecurityContextHolder;
@@ -46,8 +47,10 @@ public class RequestSecurityInterceptor implements Filter {
         }
 
         // 判断是否需要放行
+        String uri = HttpUtils.getRequestURI(request);
         SecurityProperties.Request configuration = SecurityContextHolder.getProperties().getRequest();
-        if (SpringUtils.isPath(HttpUtils.getRequestURI(request), configuration.getPermit())) {
+        if (SpringUtils.isPath(uri, configuration.getPermit()) || (ObjectUtils.nonEmpty(configuration.getRequire())
+                && !SpringUtils.isPath(uri, configuration.getRequire()))) {
             return true;
         }
 

+ 8 - 6
framework-security/src/main/java/com/chelvc/framework/security/interceptor/SecurityExceptionInterceptor.java

@@ -27,16 +27,18 @@ public class SecurityExceptionInterceptor {
     /**
      * 拒绝访问异常转换
      *
-     * @param response  请求响应对象
-     * @param exception 异常对象
+     * @param request  Http请求对象
+     * @param response Http响应对象
+     * @param e        异常对象
      * @return 请求结果
      */
     @ResponseStatus(HttpStatus.FORBIDDEN)
     @ExceptionHandler(AccessDeniedException.class)
-    public Result<?> exception(HttpServletRequest request, HttpServletResponse response,
-                               AccessDeniedException exception) {
-        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.FORBIDDEN, exception));
+    public Result<?> exception(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) {
+        log.warn(SessionContextHolder.getLoggingMessage(request, HttpStatus.FORBIDDEN, e));
         SessionContextHolder.initializeResponseWrappingHeader(response);
-        return Result.build(HttpStatus.FORBIDDEN);
+        boolean wrapping = SessionContextHolder.isResponseWrappingHeader(request);
+        String message = wrapping ? HttpStatus.FORBIDDEN.getReasonPhrase() : e.getMessage();
+        return Result.build(HttpStatus.FORBIDDEN, null, message);
     }
 }

+ 5 - 1
framework-security/src/main/java/com/chelvc/framework/security/interceptor/SignatureValidateInterceptor.java

@@ -15,6 +15,7 @@ import com.chelvc.framework.base.model.Result;
 import com.chelvc.framework.base.util.HttpUtils;
 import com.chelvc.framework.base.util.SpringUtils;
 import com.chelvc.framework.common.util.CodecUtils;
+import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.security.config.SecurityProperties;
 import com.chelvc.framework.security.context.SecurityContextHolder;
 import lombok.extern.slf4j.Slf4j;
@@ -47,8 +48,11 @@ public class SignatureValidateInterceptor implements Filter {
         }
 
         // 判断请求是否需要放行
+        String uri = HttpUtils.getRequestURI(request);
         SecurityProperties properties = SecurityContextHolder.getProperties();
-        if (SpringUtils.isPath(HttpUtils.getRequestURI(request), properties.getSignature().getPermit())) {
+        SecurityProperties.Signature configuration = properties.getSignature();
+        if (SpringUtils.isPath(uri, configuration.getPermit()) || (ObjectUtils.nonEmpty(configuration.getRequire())
+                && !SpringUtils.isPath(uri, configuration.getRequire()))) {
             return true;
         }