HttpServletRequestWrapper里的方法没有被调用

发布于 2022-09-01 22:48:27 字数 5210 浏览 11 评论 0

我有一系列接口,读写数据都需要加密/解密。写了一个filter,原始request用HttpServletRequestWrapper包装了一下,但是发现HttpServletRequestWrapper的getInputStream和getReader都没有被调用,导致数据到了controller依旧是加密状态,controller报告required parameter is missing

代码

HttpServletRequestWrapper子类

class ResettableStreamHttpServletRequest extends
        HttpServletRequestWrapper {

    private byte[] rawData;
    private HttpServletRequest request;
    private ResettableServletInputStream servletStream;

    public ResettableStreamHttpServletRequest(HttpServletRequest request) {
        super(request);
        this.request = request;
        this.servletStream = new ResettableServletInputStream();
    }


    public void resetInputStream(byte[] data) {
        servletStream.stream = new ByteArrayInputStream(data);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (rawData == null) {
            rawData = IOUtils.toByteArray(this.request.getReader());
            servletStream.stream = new ByteArrayInputStream(rawData);
        }
        return servletStream;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        if (rawData == null) {
            rawData = IOUtils.toByteArray(this.request.getReader());
            servletStream.stream = new ByteArrayInputStream(rawData);
        }
        return new BufferedReader(new InputStreamReader(servletStream));
    }


    private class ResettableServletInputStream extends ServletInputStream {

        private InputStream stream;

        @Override
        public int read() throws IOException {
            return stream.read();
        }
    }
}

doFilter代码

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest newRequest = new ResettableStreamHttpServletRequest((HttpServletRequest) request);
        ServletResponse newResponse = new EncryptedResponseWrapper((HttpServletResponse) response);

        String body = IOUtils.toString(newRequest.getInputStream());
        String plainText = crypt.decrypt(body);
        LOGGER.debug(plainText);
        ((ResettableStreamHttpServletRequest) newRequest).resetInputStream(plainText.getBytes("UTF-8"));

        chain.doFilter(newRequest, newResponse);

        if (((EncryptedResponseWrapper) newResponse).getStatus() != HttpStatus.OK.value()) {
            response.getWriter().write(newResponse.toString());
            return;
        }

        String text = newResponse.toString();
        if (text != null) {
            String respPlainText = newResponse.toString();
            LOGGER.debug(respPlainText);
            String encrypted = crypt.encrypt(respPlainText);
            response.getWriter().write(encrypted);
        }
    }

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <absolute-ordering>
        <name>EncryptFilter</name>
        <name>encodingFilter</name>
    </absolute-ordering>

    <filter>
        <filter-name>EncryptFilter</filter-name>
        <filter-class>com.yuexunit.micro.filter.EncryptFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>EncryptFilter</filter-name>
        <url-pattern>/user/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2022-09-08 22:48:28

我也有同样疑问,楼主解决了吗?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文