Mybatis拦截器中获取@RequestBody表单的值修改查询SQL

背景:

我们需要获取接口Controller中前端传入的Json对象参数值然后修改本次调用接口的查询sql语句。后台接收参数如果是表单数据的话,通过request.getParameterMap就可以全部获取到了,如果是json对象数据时,我们在过滤器或拦截器里通过request.getInputStream() 读取了request的输入流之后,请求走到controller层时就会报错,问题在于request的输入流只能读取一次不能重复读取。

1.示例:定义Controller查询UserList

c 复制代码
@PostMapping("/user/list")
public PageDataInfo<UserInfo> getUserList(@RequestBody ChkReq req) {
	PageUtils.startPage(req);
	return PageUtils.buildPageDataInfo(userInfoService.getUserList(req));
}

2.定义一个容器,将输入流存储到这个容器里面

c 复制代码
@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {

    /**
     * 存储body数据的容器
     */
    private final byte[] body;

    public RequestWrapper(HttpServletRequest request) {
        super(request);
        // 将body数据存储起来
        String bodyStr = getBodyString(request);
        body = bodyStr.getBytes(Charset.defaultCharset());
    }

    public String getBodyString(final ServletRequest request) {
        try {
            return cloneInputStreamString(request.getInputStream());
        } catch (IOException e) {
            log.error("", e);
            throw new RuntimeException(e);
        }
    }

    public String getBodyString() {
        final InputStream inputStream = new ByteArrayInputStream(body);
        return cloneInputStreamString(inputStream);
    }

    private String cloneInputStreamString(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.defaultCharset()));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            log.error("", e);
            throw new RuntimeException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("", e);
                }
            }
        }
        return sb.toString();
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {

        final ByteArrayInputStream inputStream = new ByteArrayInputStream(body);

        return new ServletInputStream() {
            @Override
            public int read() throws IOException {
                return inputStream.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }

}

3.我们要在过滤器中将原生的HttpServletRequest换成RequestWrapper对象

c 复制代码
public class ReplaceStreamFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
        filterChain.doFilter(requestWrapper, servletResponse);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

4.注册过滤器

c 复制代码
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(replaceStreamFilter());
        registration.addUrlPatterns("/*");
        registration.setName("streamFilter");
        return registration;
    }
    
    @Bean(name = "replaceStreamFilter")
    public Filter replaceStreamFilter() {
        return new ReplaceStreamFilter();
    }
}

5.然后我们可以在拦截器中获取json数据

c 复制代码
public class MyRequestInterceptor implements HandlerInterceptor {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if ("POST".equalsIgnoreCase(request.getMethod()) && request.getContentType() != null && request.getContentType().contains("application/json")) {
            /*try {
                byte[] requestBodyBytes = readRequestBody(request);
                String requestBody = new String(requestBodyBytes, StandardCharsets.UTF_8);*/
            try (BufferedReader reader = request.getReader()) {
                StringBuilder requestBody = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    requestBody.append(line);
                }

                // 将请求体转换为 ChkReq 对象
                ChkReq chkReq = objectMapper.readValue(requestBody.toString(), ChkReq.class);
                // 将 ChkReq 对象存储在 ServletRequestAttributes 中
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes != null) {
                    attributes.getRequest().setAttribute("ChkReq", chkReq);
                }
                // 继续处理请求
                return true;
            } catch (IOException e) {
                // 处理异常,例如返回错误响应
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                response.getWriter().write("Invalid JSON data");
                return false;
            }
        }
        // 如果不是 JSON 请求或者不是 POST 方法,则继续处理请求
        return true;
    }
}

6.注册拦截器

c 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyRequestInterceptor()).addPathPatterns("/**"); // 指定需要拦截的路径
    }
}

7.在Mybatis拦截器中获取request的值修改sql

c 复制代码
@Component
public class MyInterceptor implements InnerInterceptor {

    @SneakyThrows
    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        //InnerInterceptor.super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
        String sql = boundSql.getSql();
        System.out.println("sql更新之前:" + sql);
        //String condition = " name = '李四' " ;
        String condition = " 1 = 1 ";
        String name = null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            ChkReq chkReq = (ChkReq) attributes.getRequest().getAttribute("ChkReq");
            if (chkReq != null) {
                name = "name = '" + chkReq.getName() + "'";
            }
        }
        PluginUtils.MPBoundSql mpBs = PluginUtils.mpBoundSql(boundSql);
        Select select = (Select) CCJSqlParserUtil.parse(sql);
        PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
        final Expression expression = plainSelect.getWhere();
        final Expression envCondition = CCJSqlParserUtil.parseCondExpression(condition);
        final Expression envCondition2 = CCJSqlParserUtil.parseCondExpression(name);
        if (expression == null) {
            plainSelect.setWhere(envCondition);
            plainSelect.setWhere(envCondition2);
        } else {
            AndExpression andExpression = new AndExpression(expression, envCondition);
            AndExpression andExpression2 = new AndExpression(andExpression, envCondition2);
            plainSelect.setWhere(andExpression2);
        }
        mpBs.sql(plainSelect.toString());
        System.out.println("sql更新之后:" + plainSelect.toString());
    }

}
相关推荐
_半夏曲几秒前
修改Mysql 8 的密码
数据库·mysql
超栈8 分钟前
蓝桥杯-网络安全比赛题目-遗漏的压缩包
前端·网络·sql·安全·web安全·职场和发展·蓝桥杯
威哥爱编程9 分钟前
MongoDB面试专题33道解析
数据库·mongodb·面试
大霸王龙18 分钟前
django+postgresql
数据库·后端·python·postgresql·django
stars_User39 分钟前
MySQL详细安装教程
数据库·mysql
编程学无止境40 分钟前
第05章 MySQL目录结构、源码和常见问题汇总
数据库·mysql
程序猿进阶1 小时前
Redis 基础数据改造
java·开发语言·数据库·redis·后端·面试·架构
原机小子1 小时前
城镇保障性住房管理:SpringBoot系统解决方案
数据库·spring boot·php
shiran小坚果1 小时前
AWS RDS MySQL内存使用
数据库·mysql·云计算·database·aws
SelectDB1 小时前
8+ 典型分析场景,25+ 标杆案例,Apache Doris 和 SelectDB 精选案例集(2024版)电子版上线
大数据·数据库·数据分析