1. 在SpringBootApplication上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
    2. 新建Filter过滤器 继承GenericFilterBean并加入注解@WebFilter(filterName = “wmTokenFilter”,urlPatterns = “/*”)
      1. @WebFilter(filterName = "wmTokenFilter",urlPatterns = "/*")
      2. @Log4j2
      3. public class WmTokenFilter extends GenericFilterBean {
      4. @Override
      5. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      6. HttpServletRequest request = (HttpServletRequest) servletRequest;
      7. HttpServletResponse response = (HttpServletResponse) servletResponse;
      8. //得到header中的信息
      9. String userId = request.getHeader("userId");
      10. if(userId != null){
      11. WmUser wmUser = new WmUser();
      12. wmUser.setId(Integer.valueOf(userId));
      13. WmThreadLocalUtils.setUser(wmUser);
      14. }
      15. filterChain.doFilter(request,response);
      16. }
      17. }`