参考:https://www.jianshu.com/p/69c6fba08c92

原因:
spring拦截器读取body后,controller里面的body就为空了,所以我们要重写request即可。

1、重写RequestWrapper

  1. import javax.servlet.ReadListener;
  2. import javax.servlet.ServletInputStream;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletRequestWrapper;
  5. import java.io.*;
  6. public class RequestWrapper extends HttpServletRequestWrapper {
  7. private final String body;
  8. public RequestWrapper(HttpServletRequest request) {
  9. super(request);
  10. StringBuilder stringBuilder = new StringBuilder();
  11. BufferedReader bufferedReader = null;
  12. InputStream inputStream = null;
  13. try {
  14. inputStream = request.getInputStream();
  15. if (inputStream != null) {
  16. bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  17. char[] charBuffer = new char[128];
  18. int bytesRead = -1;
  19. while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
  20. stringBuilder.append(charBuffer, 0, bytesRead);
  21. }
  22. } else {
  23. stringBuilder.append("");
  24. }
  25. } catch (IOException ex) {
  26. } finally {
  27. if (inputStream != null) {
  28. try {
  29. inputStream.close();
  30. }
  31. catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. if (bufferedReader != null) {
  36. try {
  37. bufferedReader.close();
  38. }
  39. catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }
  44. body = stringBuilder.toString();
  45. }
  46. @Override
  47. public ServletInputStream getInputStream() throws IOException {
  48. final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
  49. ServletInputStream servletInputStream = new ServletInputStream() {
  50. @Override
  51. public boolean isFinished() {
  52. return false;
  53. }
  54. @Override
  55. public boolean isReady() {
  56. return false;
  57. }
  58. @Override
  59. public void setReadListener(ReadListener readListener) {
  60. }
  61. @Override
  62. public int read() throws IOException {
  63. return byteArrayInputStream.read();
  64. }
  65. };
  66. return servletInputStream;
  67. }
  68. @Override
  69. public BufferedReader getReader() throws IOException {
  70. return new BufferedReader(new InputStreamReader(this.getInputStream()));
  71. }
  72. public String getBody() {
  73. return this.body;
  74. }
  75. }

2、拦截器使用新request

  1. package com.miniprogram.web.douyin.interceptor;
  2. import com.alibaba.fastjson.JSON;
  3. import com.miniprogram.api.douyin.user.req.DyuserReq;
  4. import com.miniprogram.common.auth.VisitLimitCount;
  5. import com.miniprogram.common.cache.RedisCache;
  6. import com.miniprogram.common.config.InterceptorConfigMap;
  7. import com.miniprogram.common.config.InterceptorUrlConfig;
  8. import com.miniprogram.common.douyin.SearchEngineMapConstants;
  9. import com.miniprogram.common.response.Response;
  10. import com.miniprogram.common.session.*;
  11. import com.miniprogram.common.utils.DateUtil;
  12. import com.miniprogram.dao.common.UserLoginEntity.Users;
  13. import com.miniprogram.service.douyin.users.UsersService;
  14. import com.miniprogram.web.douyin.config.RequestWrapper;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.BeanUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Component;
  20. import org.springframework.web.servlet.ModelAndView;
  21. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.BufferedReader;
  25. import java.io.InputStreamReader;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. import java.util.Map;
  30. @Component("authSecurityInterceptor")
  31. public class AuthSecurityInterceptor extends HandlerInterceptorAdapter {
  32. private Logger logger = LoggerFactory.getLogger(AuthSecurityInterceptor.class);
  33. @Autowired
  34. private UsersService douYinUsersService;
  35. @Autowired
  36. private RedisCache redisCache;
  37. @Autowired
  38. private VisitLimitCount visitLimitCount;
  39. @Override
  40. public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
  41. try {
  42. RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
  43. String body = requestWrapper.getBody();
  44. System.out.println(body);
  45. return true;
  46. }catch (Exception e){
  47. logger.error("权限判断出错",e);
  48. }
  49. return false;
  50. }
  51. @Override
  52. public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
  53. }
  54. @Override
  55. public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
  56. }
  57. }

3、filter替换request

  1. import com.miniprogram.web.douyin.config.RequestWrapper;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import javax.servlet.http.HttpServletRequest;
  5. import java.io.IOException;
  6. @Component
  7. @WebFilter(urlPatterns = "/*",filterName = "channelFilter")
  8. public class ChannelFilter implements Filter {
  9. @Override
  10. public void init(FilterConfig filterConfig) throws ServletException {
  11. }
  12. @Override
  13. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  14. ServletRequest requestWrapper = null;
  15. if(servletRequest instanceof HttpServletRequest) {
  16. requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
  17. }
  18. if(requestWrapper == null) {
  19. filterChain.doFilter(servletRequest, servletResponse);
  20. } else {
  21. filterChain.doFilter(requestWrapper, servletResponse);
  22. }
  23. }
  24. @Override
  25. public void destroy() {
  26. }
  27. }

4、注册filter

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.web.servlet.MultipartConfigFactory;
  4. import org.springframework.boot.web.servlet.ServletComponentScan;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.ComponentScan;
  7. import org.springframework.context.annotation.Configuration;
  8. @SpringBootApplication
  9. @ServletComponentScan //注册过滤器注解
  10. @Configuration
  11. public class WebApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(WebApplication.class, args);
  14. }
  15. }