Skip to content

Commit a9d5178

Browse files
committed
同步主分支
1 parent 3416763 commit a9d5178

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ $ php think jwt:make
2121
* `notBefore` 时间前不能使用 默认生成后直接使用
2222
* `expiresAt` Token有效期(秒)
2323
* `signer` 加密算法
24+
* `type` 获取 Token 途径
2425
* `injectUser` 是否注入用户模型
2526
* `userModel` 用户模型
2627
* `hasLogged` 开启单点登录时,多点登录抛异常code = 50401
@@ -93,3 +94,58 @@ class UserController {
9394
}
9495

9596
```
97+
98+
6. 自动获取验证
99+
100+
支持以下方式自动获取
101+
102+
* `Bearer`
103+
* `Cookie`
104+
* `Url`
105+
106+
赋值方式
107+
108+
类型 | 途径 | 标识 |
109+
:-: | :-: | :-: |
110+
Bearer | Authorization | Bearer |
111+
Cookie | Cookie| token |
112+
Url | Request | token |
113+
114+
```php
115+
# config/jwt.php
116+
117+
<?php
118+
119+
return [
120+
121+
// ...其它配置
122+
'type' => 'Bearer',
123+
124+
// 'type' => 'Cookie',
125+
// 'type' => 'Url',
126+
];
127+
```
128+
129+
```php
130+
# UserController.php
131+
132+
use xiaodi\Facade\Jwt;
133+
134+
class User
135+
{
136+
public function index()
137+
{
138+
// 自动获取并验证
139+
try {
140+
Jwt::verify();
141+
} catch (HasLoggedException $e) {
142+
// 已在其它终端登录
143+
} catch (TokenAlreadyEexpired $e) {
144+
// Token已过期
145+
}
146+
147+
$uid = Jwt::userId();
148+
}
149+
150+
}
151+
```

src/BearerToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace xiaodi;
44

55
use think\App;
6-
use xiaodi\Exception\JwtException;
6+
use xiaodi\Exception\JWTException;
77

88
class BearerToken
99
{
@@ -19,7 +19,7 @@ public function getToken()
1919
$authorization = $this->request->header('authorization');
2020

2121
if (strpos($authorization, 'Bearer ') !== 0) {
22-
throw new JwtException('获取Token失败.');
22+
throw new JWTException('获取Token失败.');
2323
}
2424

2525
return substr($authorization, 7);

src/Facade/Jwt.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
/**
88
* @see \xiaodi\Jwt
9-
* @package xiaodi\facade
109
* @mixin \xiaodi\Jwt
1110
*/
1211
class Jwt extends Facade
1312
{
1413
/**
15-
* 获取当前Facade对应类名(或者已经绑定的容器对象标识)
16-
* @access protected
14+
* 获取当前Facade对应类名(或者已经绑定的容器对象标识).
15+
*
1716
* @return string
1817
*/
1918
protected static function getFacadeClass()

0 commit comments

Comments
 (0)