The client part for uicosp/jwt-cas-server
一个基于 JWT 实现「单点登陆」的 CAS,Central Authentication System 系统。, (*1)
本项目依赖于 Laravel。, (*2)
用户只需在 Server 端登陆一次,获得 token
后便可用该令牌访问系统中的任意 Clients。, (*3)
[注意] 此项目为该系统的客户端实现,服务端请移步 https://github.com/uicosp/jwt-cas-server, (*4)
Client 端提供一个校验 token
的中间件, (*5)
Uicosp\JwtCasClient\Middleware\VerifyCasToken
, (*6)
该中间件会验证每次请求中携带的 token
的合法性。校验失败将返回错误信息给前端。校验通过则将解密后的 token
注入到 $request
中。可通过 $request['verified_token']
获取。verified_token
示例如下:, (*7)
array:6 [ "sub" => 11 "iss" => "http://user.dev/jwt/login" "iat" => 1482998888 "exp" => 1483002488 "nbf" => 1482998888 "jti" => "e148091d51ece1fb1cf77cc14d317298" ]
composer require "uicosp/jwt-cas-client"
, (*8)
本项目依赖 typmon/jwt-auth,请添加, (*9)
Tymon\JWTAuth\Providers\JWTAuthServiceProvider::class
,
Uicosp\JwtCasClient\CasServiceProvider::class,
, (*10)
到 config/app.php
的 providers
数组。, (*11)
在 .env
文件中设置 CAS_JWT_SECRET
, (*12)
运行命令 php artisan vendor:publish --provider="Uicosp\JwtCasClient\CasServiceProvider"
, 将在 config
目录下新增 jwt-cas-client.php
配置文件。, (*13)
修改, (*14)
'jwt_secret' => env('CAS_JWT_SECRET', 'please-change-me'),
, (*15)
将 Uicosp\JwtCasClient\Middleware\VerifyCasToken::class
添加到 app/Http/Kernel.php
文件中。例如:, (*16)
/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, ... // 将中间件添加到路由中间件,按需调用 'auth.cas' => \Uicosp\JwtCasClient\Middleware\VerifyCasToken::class, ];
然后在需要的地方调用,例如在 routes/web.php
:, (*17)
Route::get('/', function () { return view('welcome')->middleware('auth.cas'); });