jwt的簡單應用

2016-09-20 21:50:00
hainuo
原創 1410
摘要: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基本處理到此結束
發錶評論
玖 加 零 =
評論通過審核後顯示。