1
  1. I am having sprin boot application as my back end and vue js in the front end
  2. Both are running in my localhost but with different ports
  3. I am using axios to make api calls to my back end server
  4. My login api works perfetcly and resturns the response with set cookie header

But cookie value not being set in the request header and all other api fails with authentication problem because cookie value (session id) not present in the header
sample response header with set-cookie on authentication

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

Below is my proxy configuration in vue.config.js

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

This is my axios instance creation with "withCredentials:true"

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

Below is my web secuirty configuration in the server side


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

Note:This works in the postman because post man automatically adds the cookie in the request header

Saradha
  • 147
  • 3
  • 16

1 Answers1

0

This looks like a known issue with axios. Try setting the default value like this

axios.defaults.withCredentials = true

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

Gowthaman
  • 1,262
  • 1
  • 9
  • 15
  • tried adding above property before creating axios instance still i am facing the same issue – Saradha May 12 '20 at 16:59
  • what errors are you getting in your console? Also this thread might be useful https://stackoverflow.com/questions/43002444/make-axios-send-cookies-in-its-requests-automatically – Gowthaman May 12 '20 at 18:04