双token时refreshtoken过期后不自动返回登录页,还需要进行刷新才能将vuex内的refresh_token清除掉是什么原因
时间: 2024-06-11 17:05:33 浏览: 15
这可能是因为您的代码中没有正确处理refresh token过期的情况。当refresh token过期时,您的应用程序应该自动将用户重定向到登录页,并清除vuex中的refresh token。如果没有正确处理refresh token过期的情况,您的应用程序可能会出现不稳定的行为,例如无法正确获取新的access token,导致用户无法访问受保护的资源。因此,您需要为refresh token过期的情况编写相应的代码以确保应用程序的稳定性和安全性。
相关问题
为什么refreshtoken过期了,发送请求不清空refreshtoken,刷新浏览器可以清空
这可能是因为在浏览器中刷新页面会导致浏览器清除所有已保存的cookie,包括存储refresh token的cookie。而发送请求时并不会自动清空refresh token,因为这需要在代码中进行明确的操作。如果refresh token过期,服务器将无法使用它来生成新的access token,因此需要在客户端重新获取新的access token和refresh token。
jwt的token刷新,通过refreshToken刷新后,axios调用触发刷新的token的接口会返回失败,导致页面加载数据未空,怎么处理使其正常?
这种情况可能是因为在刷新token的过程中,axios调用接口的token还没有更新为新的token。你可以在axios拦截器中进行处理,判断响应状态码是否为401(即token失效),如果是则在刷新token后重新发送之前的请求。
具体实现步骤如下:
1. 在axios的请求拦截器中,为所有请求添加token。
```javascript
axios.interceptors.request.use(
config => {
const token = localStorage.getItem('token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
error => {
return Promise.reject(error)
}
)
```
2. 在axios的响应拦截器中,判断响应状态码是否为401,如果是则刷新token并重新发送之前的请求。
```javascript
axios.interceptors.response.use(
response => {
return response
},
error => {
const originalRequest = error.config
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true
return axios.post('/api/refresh_token', { refreshToken: localStorage.getItem('refreshToken') })
.then(response => {
const token = response.data.token
const refreshToken = response.data.refreshToken
localStorage.setItem('token', token)
localStorage.setItem('refreshToken', refreshToken)
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
return axios(originalRequest)
})
}
return Promise.reject(error)
}
)
```
这样做的原理是,在刷新token后,重新发送之前的请求,此时axios会自动添加新的token,并且保证了请求不会丢失。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)