axios请求标头中未设置cookie

x759pob2  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(290)

1.我有sprin Boot 应用程序作为我的后端和vue js在前端
1.两者都在我的本地主机上运行,但端口不同
1.我正在使用axios对后端服务器进行api调用
1.我的登录api工作得很好,并使用设置的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
  }

这是我用“withCredentials:true”创建的axios示例

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

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

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

注意:这在postman中有效,因为postman会自动在请求标头中添加cookie

c6ubokkw

c6ubokkw1#

这看起来像是axios的已知问题。
axios.defaults.withCredentials = true
https://github.com/axios/axios/issues/587#issuecomment-275890961

相关问题