MainController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace mobile\controllers;
  3. use biz\user\services\UserService;
  4. use common\components\token;
  5. use Yii;
  6. use yii\db\Exception;
  7. use yii\web\Controller;
  8. use Lcobucci\JWT\Builder;
  9. use Lcobucci\JWT\Parser;
  10. use Lcobucci\JWT\ValidationData;
  11. use Lcobucci\JWT\Signer\Key;
  12. use Lcobucci\JWT\Signer\Hmac\Sha256;
  13. class MainController extends PublicController
  14. {
  15. public $enableCsrfValidation = false;
  16. public function actionIndex()
  17. {
  18. $host = Yii::$app->request->getHostInfo();
  19. $fullUrl = Yii::$app->request->url;
  20. $token = strpos($fullUrl, 'token') === false ? '' : 'hasToken';
  21. return $this->renderPartial('index', ['host' => $host, 'token' => $token]);
  22. }
  23. public function actionJwt()
  24. {
  25. $time = time();
  26. $signer = new Sha256();
  27. $key = new Key('testing');
  28. $token = (new Builder())->issuedBy('http://example.com')//发布者的url地址
  29. ->canOnlyBeUsedBy('http://example.org')//接受者的url地址
  30. ->identifiedBy('12358', true)//该jwt的唯一ID编号
  31. ->issuedAt($time)//该jwt的发布时间
  32. ->canOnlyBeUsedAfter($time + 60)//该jwt的使用时间不能早于该时间
  33. ->expiresAt($time + 3600)//该jwt销毁的时间
  34. ->with('uid', 1)
  35. ->set('username', 'lili')//设置一个变量
  36. ->sign($signer, $key)//设置一个变量,同set
  37. ->getToken();
  38. echo '<pre>';
  39. echo $token;
  40. echo '<br />';
  41. $token->getHeaders(); // Retrieves the token headers
  42. $token->getClaims(); // Retrieves the token claims
  43. echo '<br />';
  44. echo $token->getHeader('jti'); // will print "4f1g23a12aa"
  45. echo '<br />';
  46. echo $token->getClaim('iss'); // will print "http://example.com"
  47. $token = (new Parser())->parse((string)'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjEyMzU4In0.eyJpc3MiOiJodHRwOlwvXC9leGFtcGxlLmNvbSIsImF1ZCI6Imh0dHA6XC9cL2V4YW1wbGUub3JnIiwianRpIjoiMTIzNTgiLCJpYXQiOjE1NzQzNDk5OTYsIm5iZiI6MTU3NDM1MDA1NiwiZXhwIjoxNTc0MzUzNTk2LCJ1aWQiOjF9.6FtTHmMlqhI1BNoFJiJrmm1nXXtc5-u2tVt-A8s8IN8
  48. ');
  49. $token->getHeaders(); // Retrieves the token header
  50. $token->getClaims(); // Retrieves the token claims
  51. echo "<br />....<br />";
  52. echo $token->getHeader('jti'); // will print "4f1g23a12aa"
  53. echo '<br />';
  54. echo $token->getClaim('iss'); // will print "http://example.com"
  55. echo '<br />';
  56. echo $token->getClaim('uid'); // will print "1"
  57. echo '<br />';
  58. $data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp)
  59. $data->setIssuer('http://example.com');
  60. $data->setAudience('http://example.org');
  61. $data->setId('12358');
  62. //先验证私钥
  63. var_dump($token->verify($signer, $key));
  64. //失败,因为token在60秒后方可验证
  65. var_dump($token->validate($data));
  66. $data->setCurrentTime($time + 61); // changing the validation time to future
  67. // true
  68. var_dump($token->validate($data));
  69. $data->setCurrentTime(time() + 4000);
  70. //false,token过期
  71. var_dump($token->validate($data));
  72. }
  73. }