Browse Source

【升级】增加一个json请求参数的临时缓存功能

fengshuonan 4 years ago
parent
commit
39a50c0c6b

+ 89 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/param/RequestParamContext.java

@ -0,0 +1,89 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.core.context.param;
import cn.hutool.core.lang.Dict;
/**
 * 临时保存http请求的参数
 * <p>
 * 可以保存@RequestBody的可以保存parameter方式传参的
 *
 * @author fengshuonan
 * @date 2020/8/20
 */
public class RequestParamContext {
    private static final ThreadLocal<Dict> CONTEXT_HOLDER = new ThreadLocal<>();
    /**
     * 保存请求参数
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void set(Dict requestParam) {
        CONTEXT_HOLDER.set(requestParam);
    }
    /**
     * 保存请求参数
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void setObject(Object requestParam) {
        if (requestParam == null) {
            return;
        }
        if (requestParam instanceof Dict) {
            CONTEXT_HOLDER.set((Dict) requestParam);
        } else {
            CONTEXT_HOLDER.set(Dict.parse(requestParam));
        }
    }
    /**
     * 获取请求参数
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static Dict get() {
        return CONTEXT_HOLDER.get();
    }
    /**
     * 清除请求参数
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void clear() {
        CONTEXT_HOLDER.remove();
    }
}

+ 37 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/web/GunsRequestResponseBodyMethodProcessor.java

@ -0,0 +1,37 @@
package cn.stylefeng.guns.core.web;
import cn.stylefeng.guns.core.context.param.RequestParamContext;
import org.springframework.core.MethodParameter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import java.util.List;
/**
 * 拓展原有RequestResponseBodyMethodProcessor,只为缓存临时参数
 *
 * @author fengshuonan
 * @date 2020/8/21 20:51
 */
public class GunsRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {
    public GunsRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
        super(converters);
    }
    @Override
    public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
        Object paramResult = super.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        // 临时缓存一下@RequestBody注解上的参数
        RequestParamContext.setObject(paramResult);
        return paramResult;
    }
}

+ 4 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/web/package-info.java

@ -0,0 +1,4 @@
package cn.stylefeng.guns.core.web;
/**
 * 此包存放的对spring mvc的一些拓展
 */

+ 31 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/config/WebMvcConfig.java

@ -24,6 +24,7 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
 */
package cn.stylefeng.guns.sys.config;
import cn.stylefeng.guns.core.web.GunsRequestResponseBodyMethodProcessor;
import cn.stylefeng.guns.sys.core.error.GunsErrorAttributes;
import cn.stylefeng.guns.sys.core.filter.RequestNoFilter;
import cn.stylefeng.guns.sys.core.filter.xss.XssFilter;
@ -34,8 +35,16 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
 * web配置
@ -122,4 +131,26 @@ public class WebMvcConfig implements WebMvcConfigurer {
        return new GunsValidator();
    }
    /**
     * 自定义的GunsRequestResponseBodyMethodProcessor,放在所有resolvers之前
     *
     * @author fengshuonan
     * @date 2020/8/21 21:09
     */
    @Configuration
    public static class MethodArgumentResolver {
        @Resource
        private RequestMappingHandlerAdapter adapter;
        @PostConstruct
        public void injectSelfMethodArgumentResolver() {
            List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
            argumentResolvers.add(new GunsRequestResponseBodyMethodProcessor(adapter.getMessageConverters()));
            argumentResolvers.addAll(Objects.requireNonNull(adapter.getArgumentResolvers()));
            adapter.setArgumentResolvers(argumentResolvers);
        }
    }
}

+ 20 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/core/listener/RemoveRequestParamListener.java

@ -0,0 +1,20 @@
package cn.stylefeng.guns.sys.core.listener;
import cn.stylefeng.guns.core.context.param.RequestParamContext;
import org.springframework.context.ApplicationListener;
import org.springframework.web.context.support.ServletRequestHandledEvent;
/**
 * 用来清除临时缓存的@RequestBody的请求参数
 *
 * @author fengshuonan
 * @date 2020/8/21 21:14
 */
public class RemoveRequestParamListener implements ApplicationListener<ServletRequestHandledEvent> {
    @Override
    public void onApplicationEvent(ServletRequestHandledEvent event) {
        RequestParamContext.clear();
    }
}

+ 2 - 1
guns-base-support/guns-system/src/main/resources/META-INF/spring.factories

@ -1,3 +1,4 @@
org.springframework.context.ApplicationListener=\
cn.stylefeng.guns.sys.core.listener.ConstantsInitListener,\
cn.stylefeng.guns.sys.core.listener.TimerTaskRunListener
cn.stylefeng.guns.sys.core.listener.TimerTaskRunListener,\
cn.stylefeng.guns.sys.core.listener.RemoveRequestParamListener