未在axios请求标头中设置cookie

时间:2020-05-12 14:49:41

标签: spring-boot vue.js axios cross-domain setcookie

  1. 我将sprin引导应用程序作为后端,并将vue js作为 前端
  2. 两者都在我的本地主机上运行,​​但端口不同
  3. 我正在使用axios对后端服务器进行api调用
  4. 我的登录api可以正常运行并返回带有set的响应 Cookie标头

但是未在请求标头中设置cookie值,所有其他api均失败,并出现身份验证问题,因为标头中不存在cookie值(会话ID)
带有身份验证的set-cookie的示例响应标头

HTTP/1.1 200 OK
X-Powered-By: Express
set-cookie: JSESSIONID=C6A3DE7E13C60F33D777875DB610EED2; Path=/a; HttpOnly
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
x-frame-options: DENY
content-type: application/json;charset=UTF-8
transfer-encoding: chunked
date: Tue, 12 May 2020 15:20:17 GMT
connection: close

以下是我在vue.config.js中的代理配置

 devServer: {
    proxy: {
      "/": {
        target: "http://localhost:8080/abc",
        secure: false,
        onProxyReq: function(request) {
          request.setHeader("origin", "http://localhost:8080/abc");
        }
      }
    },
    disableHostCheck: true
  }

这是我的axios实例创建,带有“ withCredentials:true”

  withCredentials: true,
  baseURL: "/",
  headers: { "Content-Type": "application/json" }
});

下面是我在服务器端的网络安全配置


        httpSecurity
                .csrf().disable()
                .httpBasic().disable()
                .authorizeRequests()
                .antMatchers("/auth/login").permitAll()
                .antMatchers("/auth/reset-password").authenticated()
                .anyRequest().authenticated();
    }

注意:这在邮递员中有效,因为邮递员会自动将cookie添加到请求标头中

1 个答案:

答案 0 :(得分:-1)

这看起来像是axios的已知问题。尝试像这样设置默认值

axios.defaults.withCredentials = true

https://github.com/axios/axios/issues/587#issuecomment-275890961