axios增加的自定义header,后端request取不到

前台采用axios提交数据,采用拦截方式设置header信息

// http request 拦截器
axios.interceptors.request.use(
  config => {
    config.headers.common['MUserAgent'] = userAgent
    if (store.state.token) {
      config.headers.common['MToken'] = store.state.token
    }
    if (store.state.uid) {
      config.headers.common['UID'] = store.state.uid
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })

后台spring mvc,采用filter进行权限过滤,但发现request取不到相应的header

String token = request.getHeader("MToken");

已设置了跨域

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); // or *
        // response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, MUserAgent, MToken, UID"); // or *
        chain.doFilter(req, res);
    }
阅读 14.5k
评论
    3 个回答

    可以了,前端没问题,是后台过滤器(处理用户登录的,AuthFilter)问题,现在的处理方式是不走这个过滤器(AuthFilter)。

      • 7.2k
      config.headers['MToken'] = store.state.token
        • 2
        • 新人请关照

        @yangjiandong 你好 我也碰到了相同的问题 请问你们最后过滤器是怎么处理的 感谢

        该答案已被忽略,原因:

          撰写回答

          登录后参与交流、获取后续更新提醒