关于使用vue-resource中使用interceptor涉及jwt_token的错误问题

使用inteceptor
拦截器可以在请求发送前和发送请求后做一些处理。


image

基本用法
Vue.http.interceptors.push((request, next) => {
// ...
// 请求发送前的处理逻辑
// ...next((response) => {
// ...
// 请求发送后的处理逻辑
// ...
// 根据请求的状态,response参数会返回给successCallback或errorCallbackreturn response
})
})

main.js代码段如下:

Vue.http.interceptors.push((request, next) => {
request.headers.Authorization = 'Bearer ' + window.localStorage.getItem('jwt_token')
next((response) => {
if (response.status === 401) {
var eid = 1
window.location.href = http://auth.demo.ecfex.org/?appId=${appId}&eid=${eid}
}
})
})

错误现象:

页面地址会自动添加关于jwt_token的相关信息,假如在这里加载页面前有读取操作并且刷新页面,则页面会不断刷新
http://1.ctts.local.ecfex.org:8080/auth?jwt_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlaWQiOiIxIiwidXNlcmlkIjoicGVuZ3poZW5zaGVuZyIsImlhdCI6MTQ2OTQzNjcwNn0.MwjY-Kl3R1YiM9xx5bTsj-YzC8Mk5lpE_O0LzbTCMps

错误原因:

request.headers.Authorization = 'Bearer ' + window.localStorage.getItem('jwt_token')

这条语句中的 'Bearer ' 不是 'Bearer' ,请注意末尾是有空格符的!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章