简体   繁体   English

Angular.js:在HTTP请求标头上设置授权

[英]Angularjs: set Authorization on http request header

in Angularjs i have a login page and if a user succesfully sign in i set the Authorization attribute as default on subsequent http request header: 在Angularjs中,我有一个登录页面,如果用户成功登录,则在后续的HTTP请求标头上将Authorization属性设置为默认值:

$http.defaults.headers.common.Authorization = 'Bearer ' + data.access_token;

So, anytime i make a request, in the header is present the Authorization parameter and all works. 因此,每当我发出请求时,标题中都会显示Authorization参数,并且所有工作正常。 The problem is that if i refresh the page, in the header request isn't present the Authorization parameter and the request fail. 问题是,如果我刷新页面,则标题请求中不存在Authorization参数,并且请求失败。

Why isn't present the Authorization parameter if i set it as default in the header? 如果在标题中将其设置为默认值,为什么不显示Authorization参数?

When you refresh the page all the javascript memory data are gone. 当您刷新页面时,所有的javascript内存数据都消失了。 You will need to store the bearer token in the browser local storage and get it back from the localstorage and again set it in your common header on page reload. 您将需要将承载令牌存储在浏览器本地存储中,并从本地存储中取回它,然后再次在页面重新加载时将其设置在通用标头中。

You can check the localstorage working here 您可以在这里查看本地存储

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM