jwt的简单应用

2016-09-20 21:50:00
hainuo
原创 1413
摘要:jwt
#0x01 服务端支持```composer require lcobucci/jwt 4.0.x-dev -vvv --prefer-dist```#0x02 vue 支持```npm install vue-authimport VueAuth form 'vue-auth';Vue.use(VueAuth);```#0x03 服务端处理```phpuse Lcobucci\JWT\Configuration;funciotn auth(){ $token = $config->createBuilder() ->setIssuer('http://example.com')// Configures the issuer (iss claim) ->setAudience('http://example.org')// Configures the audience (aud claim) ->setId('4f1g23a12aa', true)// Configures the id (jti claim), replicating as a header item ->setIssuedAt(time())// Configures the time that the token was issue (iat claim) ->setNotBefore(time() + 60)// Configures the time that the token can be used (nbf claim) ->setExpiration(time() + 3600)// Configures the expiration time of the token (exp claim) ->set('uid', 1)// Configures a new claim, called "uid" ->sign($signer, 'testing')// creates a signature using "testing" as key ->getToken(); // Retrieves the generated token $this->json(['token' => $token->__toString()]);}```#0x04 客户端处理```this.$http.post( 'http://localhost:8081/Login/index', { username: this.username, password: this.password, } ).then((response) => { console.log(response.data); this.$auth.setToken(response.data.token);//获取到数据先写入到token中 console.log(this.$auth.getToken());//查看token是否写入成功 const userdata = {//构造用户验证的其他信息包括用户的个人信息等 一般从服务器传来其权限信息 remember: this.remember, username: this.username, }; if (this.$auth.isAuthenticated()) { //校对token是否正确放置别人伪造token this.$auth.setUserData(userdata); this.formShow = 'hide'; this.showRight = true; } console.log(this.$auth.hasToken()); }).then((message) => { console.log(message); });```#0x05基本处理到此结束
发表评论
叁 减 叁 =
评论通过审核后显示。