服务器端请求的Vue nuxt错误“缺少授权标头”(flask_jwt_extended)

时间:2018-06-03 10:50:55

标签: flask vue.js http-headers authorization nuxt.js

亲爱的同事我需要你的帮助

我有一个vue nuxt应用程序,如果我从我的nuxt服务器发起(服务器端)请求(例如,当我重新加载页面时),则会收到以下错误消息:

flask_jwt_extended.exceptions.NoAuthorizationError: Missing Authorization Header

但是当我打印标题时,它看起来像这样:

Accept: application/json, text/plain, /
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4)AppleWebKit/537.36 (KHTML, like - Gecko) Chrome/66.0.3359.181 Safari/537.36
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-US;q=0.9,de-CH;q=0.8,de;q=0.7
Cookie: jwt= {{jwt Token -> deleted for better readability }}
Authorization: Bearer {{jwt Token -> deleted for better readability }}
Host: 127.0.0.1:5000

当我从用户客户端发起相同的请求时(例如,当我在没有重新加载的情况下导航)时,我没有收到任何错误。我注意到在这种情况下标题看起来略有不同:

Host: 127.0.0.1:5000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json, text/plain, /
Origin: http://localhost:3000
Authorization: Bearer {{jwt Token -> deleted for better readability }}
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4)AppleWebKit/537.36 (KHTML, like - Gecko) Chrome/66.0.3359.181 Safari/537.36
Referer: http://localhost:3000/dashboard
Accept-Encoding: gzip, deflate, br
Accept-Language: en,en-US;q=0.9,de-CH;q=0.8,de;q=0.7

你知道这些差异来自何处以及为什么我会收到错误,尽管auth-token似乎存在?

以防万一你想查看我的vue代码: 从我的check-auth中间件我发出以下动作:

initAuth(vuexContext, req) {
    let token;
    let expirationDate;
    if (req) {
      if (!req.headers.cookie) {
        return;
      }
      const jwtCookie = req.headers.cookie
        .split(";")
        .find(c => c.trim().startsWith("jwt="));
      if (!jwtCookie) {
        return;
      }
      token = jwtCookie.split("=")[1];
      expirationDate = req.headers.cookie
        .split(";")
        .find(c => c.trim().startsWith("expirationDate="))
        .split("=")[1];
    } else {
      token = localStorage.getItem("token");
      expirationDate = localStorage.getItem("tokenExpiration");
    }
    if (new Date().getTime() > +expirationDate || !token) {
      console.log("No token or invalid token");
      vuexContext.dispatch("logout");
      return;
    }
vuexContext.commit("setToken", token)
vuexContext.commit("setUser", decode(token).user_claims)
this.$axios.setToken(vuexContext.state.token, "Bearer")
;

},

非常感谢您的帮助! 感谢你并致以真诚的问候 西蒙

0 个答案:

没有答案