在axios请求头中没有设置cookie。

问题描述 投票:0回答:1
  1. 我的后台是spring boot应用,前端是vue js。
  2. 两者都在我的本地主机上运行,但端口不同。
  3. 我使用axios对我的后端服务器进行api调用。
  4. 我的登录api工作得很好,并返回带有setcookie头的响应。

但是,在请求头中没有设置cookie值,其他所有的api都会因为cookie值(session id)不在头中而导致验证失败。

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();
    }

这在postman中是可行的,因为postman会在请求头中自动添加cookie。

spring-boot vue.js axios cross-domain setcookie
1个回答
0
投票

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

axios.defaults.withCredentials = true

https:/github.comaxiosaxiosissues587#issuecomment-275890961。

© www.soinside.com 2019 - 2024. All rights reserved.