superman66 / vue-axios-github Goto Github PK
View Code? Open in Web Editor NEWVue 全家桶 + axios 前端实现登录拦截、登出、拦截器等功能
Home Page: http://superman66.github.io/vue-axios-github/
License: MIT License
Vue 全家桶 + axios 前端实现登录拦截、登出、拦截器等功能
Home Page: http://superman66.github.io/vue-axios-github/
License: MIT License
{ path: '/parent', name: 'parent', component: Parent, meta: { requireAuth: true }, children: [ { name: 'child', path: 'child', component: Child } ] }
当访问/parent/child的时候并没有执行权限验证,所以建议在beforeEach中的判断由to.meta.requireAuth改为to.matched.some(r => r.meta.requireAuth),这样的话父路由设置requireAuth后子路由就不用设置了
个人愚见,错误之处还请指正😝
我用了axios,也是跟着楼主配置axios,java怎么设置跨域请求,每次跨域请求都报405!!感觉很苦逼啊!
axios默认是get请求,如果我想post请求该怎么弄呢?
// http response 拦截器 axios.interceptors.response.use( response => { return response; }, error => { if (error.response) { switch (error.response.status) { case 401: // 返回 401 清除token信息并跳转到登录页面 store.commit(types.LOGOUT); router.replace({ path: 'login', query: {redirect: router.currentRoute.fullPath} }) } } return Promise.reject(error.response.data) // 返回接口返回的错误信息 });
error.response undefined
我想实现登陆的功能,使用封装过的axios请求后台实现登陆:
this.axios.get('/api/login', {
username:self.ruleForm.username,
password:self.ruleForm.password
}
后台并不能收到请求中的用户名和密码,能否告知要怎么写呢
这个token 是sessionId 吧
每次http请求, cookie都会带上sessionId
验证没通过 接口也能返回401
如果http头的authorization 带上 sid,
cookie 的sid 后台是不是就不用判断了,
那要后台修改判断方式吗, 会不会降低安全性, 因为cookie字段可以用httpOnly保护起来,
而存储在vuex 或者 sessionStorage中, 给了xss机会 ;
if (store.state.token) { // 通过vuex state获取当前的token是否存在 next(); }
所以应该要放到sessionStorage或者localStorage里吧?
axios的interceptors全局的拦截器所以的请求都会被处理,如果我有一个请求不想用到这个拦截器想自定义回调的话,需要怎么做呢
生成 Github Personal Token 后,使用 Github Personal Token 登录,无法登录,自动重定向到了 https://github.com/settings/tokens/new
入口main.js中import了http,而http中import了router,router中import了repository,repository中import了http
localStrorage
里面有没有 token 重新给state赋值 tokenhttps://github.com/superman66/vue-axios-github/blob/master/src/router.js#L37
不能直接在 Line 48 这样判断吗?
if (localStrorage.getItem('token')) {
next();
}
Line 37的做法其实就是每次重载页面把 token
存在了 state
里面,是为了方便存在 state 里面的token 以后使用 ?
localstorage这边是永久存储的,网页关闭的时候你这边是怎么处理掉localstorage中的token。
用sessionstorage会不会好点
因为都是团队合作写项目,这一块权限的配置,都是leader在做。学习了
axios
里面本来就是用promise
进行封装的,为什么还要重新在promise
里面再封装一次呢?
错误提示为:Cannot set property axios of #<Vue$3> which has only a getter at eval ,都是按照readme文档来搭建的。然后提示这个错误
问题产生:
我在登录的时候如你一样去把token,user放入了state中,我设想的情况时,既然store挂载的是一个vue实例,那么不论什么情况,只要不重建vue实例,store中的数据就不该丢失。但是我就是在刷新的时候,store中的数据会清空。这和我预期不符。参考你的代码,有一段是刷新之后重新获取token的代码。(router.js文件中38行),想必你能为我解答这个疑惑!
按照这个思路
是通过判断vuex里面的token是否存在来允许是否进入该路由.
我当时的需求是通过判断权限.可以说也是从vuex里面拿的数据
但有个问题就是
当按了F5刷新.
这时候进入router.forEach是比 app.beforeCreated要早执行的
这时候是不存在store里面的相关信息.所以就直接跳去login.
也是说每次按F5都要重新登陆一遍.
不知道你的会不会有这种情况.
build后dist目录下只存在js文件。图标、html等静态资源没有复制进去。
http.js里找不到fetch,请问fetch是封装在axios的吗
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.