Spring拦截器通过Stream拦截请求体后Controller无法通过@RequestBody获取的解决方案

x33g5p2x  于2022-03-06 转载在 Spring  
字(2.9k)|赞(0)|评价(0)|浏览(518)

核心:javax.servlet.http.HttpServletRequestWrapper

WebMvcConfig:

  1. @Configuration
  2. public class WebMvcConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addInterceptors(InterceptorRegistry registry) {
  5. registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
  6. }
  7. @Bean
  8. public FilterRegistrationBean<MyFilter> filterRegistrationBean() {
  9. FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<>();
  10. registration.setFilter(new MyFilter());
  11. registration.addUrlPatterns("/*");
  12. registration.setName("MyFilter");
  13. registration.setOrder(1);
  14. return registration;
  15. }
  16. }

MyController:

  1. @RestController("/")
  2. public class MyController {
  3. @PostMapping("/")
  4. public String index(@RequestBody String json) {
  5. System.out.println(json);
  6. return json;
  7. }
  8. }

MyInterceptor:

  1. public class MyInterceptor implements HandlerInterceptor {
  2. @Override
  3. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  4. if ("POST".equalsIgnoreCase(request.getMethod())) {
  5. RequestWrapper myRequestWrapper = new RequestWrapper(request);
  6. String body = myRequestWrapper.getBody();
  7. System.out.println("拦截器拦截到的请求体: " + body);
  8. }
  9. return true;
  10. }
  11. }

RequestWrapper:

  1. public class RequestWrapper extends HttpServletRequestWrapper {
  2. private final String body;
  3. public RequestWrapper(HttpServletRequest request) throws IOException {
  4. super(request);
  5. StringBuilder stringBuilder = new StringBuilder();
  6. BufferedReader bufferedReader = null;
  7. try {
  8. InputStream inputStream = request.getInputStream();
  9. if (inputStream != null) {
  10. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  11. char[] charBuffer = new char[128];
  12. int bytesRead;
  13. while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
  14. stringBuilder.append(charBuffer, 0, bytesRead);
  15. }
  16. }
  17. } finally {
  18. if (bufferedReader != null) {
  19. bufferedReader.close();
  20. }
  21. }
  22. body = stringBuilder.toString();
  23. }
  24. @Override
  25. public ServletInputStream getInputStream() {
  26. final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
  27. return new ServletInputStream() {
  28. @Override
  29. public boolean isFinished() {
  30. return false;
  31. }
  32. @Override
  33. public boolean isReady() {
  34. return false;
  35. }
  36. @Override
  37. public void setReadListener(ReadListener readListener) {
  38. }
  39. @Override
  40. public int read() {
  41. return byteArrayInputStream.read();
  42. }
  43. };
  44. }
  45. @Override
  46. public BufferedReader getReader() {
  47. return new BufferedReader(new InputStreamReader(this.getInputStream()));
  48. }
  49. public String getBody() {
  50. return this.body;
  51. }
  52. }

MyFilter:

  1. public class MyFilter implements Filter {
  2. @Override
  3. public void init(FilterConfig filterConfig) throws ServletException {
  4. Filter.super.init(filterConfig);
  5. }
  6. @Override
  7. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  8. ServletRequest requestWrapper = null;
  9. if(request instanceof HttpServletRequest) {
  10. requestWrapper = new RequestWrapper((HttpServletRequest) request);
  11. }
  12. if(requestWrapper == null) {
  13. chain.doFilter(request, response);
  14. } else {
  15. chain.doFilter(requestWrapper, response);
  16. }
  17. }
  18. }

相关文章