springboot增加过滤器后中文乱码

记录一下小问题

java 复制代码
public class RepeatableHttpServletWrapper extends HttpServletRequestWrapper {
    private  byte[] body;

    public RepeatableHttpServletWrapper(HttpServletRequest request) throws IOException  {
        super(request);
        request.setCharacterEncoding("UTF-8");
        BufferedReader reader = request.getReader();
        try (StringWriter writer = new StringWriter()) {
            int read;
            char[] buf = new char[1024];
            while ((read = reader.read(buf)) != -1) {
                writer.write(buf, 0, read);
            }
            this.body = writer.getBuffer().toString().getBytes(StandardCharsets.UTF_8);
        }
    }

    public String getBody(){
        return new String(body, StandardCharsets.UTF_8);
    }

    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }
            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() {
                return byteArrayInputStream.read();
            }
        };
    }
    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(this.getInputStream(), StandardCharsets.UTF_8));
    }
}
java 复制代码
public class RepeatableWrapperFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest.getCharacterEncoding() == null) {
            servletRequest.setCharacterEncoding("UTF-8");
        }
        ServletRequest requestWrapper = null;
        if(servletRequest instanceof HttpServletRequest) {
            requestWrapper = new RepeatableHttpServletWrapper((HttpServletRequest) servletRequest);
        }
        if(requestWrapper == null) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            filterChain.doFilter(requestWrapper, servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
java 复制代码
    @Bean
    public FilterRegistrationBean repeatableWrapperFilter() {
        FilterRegistrationBean<RepeatableWrapperFilter> registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new RepeatableWrapperFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        registrationBean.setAsyncSupported(true);
        return registrationBean;
    }
java 复制代码
public class DataAuthenticationInterceptor implements HandlerInterceptor {


    private DataAuthenticationService dataAuthenticationService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         RepeatableHttpServletWrapper repeatableRequest = new RepeatableHttpServletWrapper(request);
         String body = repeatableRequest.getBody();
    }
              

之前做的一个功能需要 通过拦截器前置处理post请求中body的信息,由于body在request中是以流存在的,读完就没有了,所以需要增加一个过滤器来进行前置处理,在处理时候本地是正常的发布linux后出现中文乱码情况

方法1:在启动jvm的参数中增加 -Dfile.encoding=UTF-8

java 复制代码
java -jar -Dfile.encoding=UTF-8 jar包

方法2:在xml文件的build标签中增加 jvm参数处理

xml 复制代码
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>${spring-boot.version}</version>
	<configuration>
		<fork>true</fork>
		<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
	</configuration>
</plugin>

方法3:处理读写是统一按照 UTF-8 处理

方法4: 增加 CharacterEncodingFilter 过滤器 ,这个过滤器优先级要高于新增的过滤器

java 复制代码
@Bean
public FilterRegistrationBean characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(filter);
    registrationBean.addUrlPatterns("/*");
    registrationBean.setOrder(0);
    return registrationBean;
}
相关推荐
天之涯上上3 分钟前
JAVA开发 在 Spring Boot 中集成 Swagger
java·开发语言·spring boot
2402_857583494 分钟前
“协同过滤技术实战”:网上书城系统的设计与实现
java·开发语言·vue.js·科技·mfc
白宇横流学长5 分钟前
基于SpringBoot的停车场管理系统设计与实现【源码+文档+部署讲解】
java·spring boot·后端
APP 肖提莫8 分钟前
MyBatis-Plus分页拦截器,源码的重构(重构total总数的计算逻辑)
java·前端·算法
kirito学长-Java9 分钟前
springboot/ssm太原学院商铺管理系统Java代码编写web在线购物商城
java·spring boot·后端
爱学习的白杨树10 分钟前
MyBatis的一级、二级缓存
java·开发语言·spring
Code成立21 分钟前
《Java核心技术I》Swing的网格包布局
java·开发语言·swing
中草药z26 分钟前
【Spring】深入解析 Spring 原理:Bean 的多方面剖析(源码阅读)
java·数据库·spring boot·spring·bean·源码阅读
信徒_34 分钟前
常用设计模式
java·单例模式·设计模式
神仙别闹40 分钟前
基于C#实现的(WinForm)模拟操作系统文件管理系统
java·git·ffmpeg