springboot增加过滤器后中文乱码

avatar
作者
猴君
阅读量:2

记录一下小问题

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));     } } 
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() {      } }  
    @Bean     public FilterRegistrationBean repeatableWrapperFilter() {         FilterRegistrationBean<RepeatableWrapperFilter> registrationBean = new FilterRegistrationBean();         registrationBean.setFilter(new RepeatableWrapperFilter());         registrationBean.addUrlPatterns("/*");         registrationBean.setOrder(1);         registrationBean.setAsyncSupported(true);         return registrationBean;     } 
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 -jar -Dfile.encoding=UTF-8 jar包 

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

<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 过滤器 ,这个过滤器优先级要高于新增的过滤器

@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; } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!