shish 6 éve
szülő
commit
1b2a6364cb

+ 5 - 1
app/mobile/controllers/AuthController.php

@@ -134,13 +134,17 @@ class AuthController extends PublicController
 				}
 			}
 		}
-		//这个登陆没有经过父类方法配置全局变量,这里重新设置一次
+		//这个的基类没有设置统一的全局变量,这里进行设置一次
 		Yii::$app->params['userId'] = $user['id'];
 		Yii::$app->params['user'] = $user;
 		Yii::$app->params['merchantId'] = $merchantId;
 		Yii::$app->params['merchant'] = $merchant;
 		$host = Yii::$app->request->getHostInfo();
+		//模拟生成token
+		$token = rand(111,9999);
+		$suffixUrl = strpos($suffixUrl,'?') === false ? $suffixUrl.'?token='.$token :  $suffixUrl.'&token='.$token;
 		$url = $host . '/' . $suffixUrl;
+		echo $url;die;
 		$this->redirect($url);
 	}
 

+ 87 - 3
app/mobile/controllers/MainController.php

@@ -1,19 +1,103 @@
 <?php
+
 namespace mobile\controllers;
 
 use biz\user\services\UserService;
 use common\components\token;
 use Yii;
+use yii\db\Exception;
 use yii\web\Controller;
 
-class MainController extends PublicController{
+use Lcobucci\JWT\Configuration;
+use Lcobucci\JWT\Signer\Hmac\Sha256;
+use Lcobucci\JWT\Signer\Key;
+use Lcobucci\JWT\Token\Plain;
 
+class MainController extends PublicController
+{
+	
 	public $enableCsrfValidation = false;
-
+	
 	public function actionIndex()
 	{
 		$host = Yii::$app->request->getHostInfo();
-		return $this->renderPartial('index',['host'=>$host]);
+		return $this->renderPartial('index', ['host' => $host]);
 	}
+	
+	public function actionJwt()
+	{
+		
+		$secretKey = Yii::$app->params['secretKey'];
+		$key = new Key($secretKey);
+		$sha = new Sha256();
+		$config = Configuration::forSymmetricSigner($sha, $key);
 
+		$now   = new \DateTimeImmutable();
+		/*
+		$token = $config->createBuilder()
+			// Configures the issuer (iss claim)
+			->issuedBy('http://example.com')
+			// Configures the audience (aud claim)
+			->permittedFor('http://example.org')
+			// Configures the id (jti claim)
+			->identifiedBy('4f1g23a12aa')
+			// Configures the time that the token was issue (iat claim)
+			->issuedAt($now)
+			// Configures the time that the token can be used (nbf claim)
+			->canOnlyBeUsedAfter($now->modify('+1 minute'))
+			// Configures the expiration time of the token (exp claim)
+			->expiresAt($now->modify('+1 hour'))
+			// Configures a new claim, called "uid"
+			->withClaim('uid', 1)
+			// Configures a new header, called "foo"
+			->withHeader('foo', 'bar')
+			// Builds a new token
+			->getToken($config->getSigner(), $config->getSigningKey());
+		*/
+		
+		$token = $config->createBuilder()
+			->issuedBy('http://example.com')
+			->withClaim('uid', 1)
+			->withHeader('foo', 'bar')
+			->getToken($config->getSigner(), $config->getSigningKey());
+		
+		$token->headers(); // Retrieves the token headers
+		$token->claims(); // Retrieves the token claims
+		echo "<pre>";
+		echo $token->headers()->get('foo'); // will print "bar"
+		echo "<br/>";
+		echo $token->claims()->get('iss'); // will print "http://example.com"
+		echo "<br/>";
+		echo $token->claims()->get('uid'); // will print "1"
+		echo "<br/>";
+		echo $token; // The string representation of the object is a JWT string
+		
+		$config->setValidator();
+		
+
+			$token = $config->getParser()->parse(
+				'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImZvbyI6ImJhciJ9.'
+				. 'eyJpc3MiOiJodHRwOi8vZXhhbXBsZS5jb20iLCJ1aWQiOjF9.'
+				. '-FXkiIJQ2WXeSfh1ReUIgWDTn6EXxMbOzYL7fFricCo'
+			);
+		
+		
+		
+		
+		
+		try {
+			$config->getValidator()->assert($token);
+		} catch (InvalidToken $e) {
+			// list of constraints violation exceptions:
+			var_dump($e->violations());
+		}
+		
+		
+		
+		$token->headers(); // Retrieves the token headers
+		$r = $token->claims(); // Retrieves the token claims
+		print_r($r);die;
+		
+	}
+	
 }

+ 3 - 1
common/config/params.php

@@ -44,4 +44,6 @@ return [
 	'threeHourExpireTime' => $threeHourExpireTime,
 	'oneDayExpireTime' => $oneDayExpireTime,
 	'threeDayExpireTime' => $threeDayExpireTime,
-];
+	
+	'secretKey' => '6XKnj81qheU2wI3#C^LpNy~a8Sok#2019',
+];