如何增加 Laravel Sanctum 令牌长度
How can increase Laravel Sanctum token length
我得到一个令牌,它有这样的 40 个字符的随机字符串...
1|z5F4SMYZRxLo8ScUYMmnExdYtQoJ8eaftQevF0Pa
我想要这样的 240 个字符的随机字符串...
1|nvbqwunxjyxujtnqnqcxzhintxyswmsjdunbmuhkifbncgrucxchzirgkybtbcadrjjtjunroewmpidxwiobcvimbolzcjlmeddvusgqzmcakffyzqllbzihnvbqwunxjyxujtnqnqcxzhintxyswmsjdunbmuhkifbncgrucxchzirgkybtbcadrjjtjunroewmpidxwiobcvimbolzcjlmeddvusgqzmcakffyzqllbzih
请帮助我如何增加 Laravel Sanctum Token
的长度
打开文件:app/Models/User.php
然后
检查是否使用了这个文件Laravel\Sanctum\HasApiTokens
trait
覆盖方法:createToken
像这样:
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Sanctum\NewAccessToken;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Create a new personal access token for the user.
*
* @param string $name
* @param array $abilities
* @return \Laravel\Sanctum\NewAccessToken
*/
public function createToken(string $name, array $abilities = ['*'])
{
$token = $this->tokens()->create([
'name' => $name,
'token' => hash('sha256', $plainTextToken = Str::random(240)),
'abilities' => $abilities,
]);
return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken);
}
}
您可以使用:
$user->createToken('your name token')->plainTextToken;
我得到一个令牌,它有这样的 40 个字符的随机字符串...
1|z5F4SMYZRxLo8ScUYMmnExdYtQoJ8eaftQevF0Pa
我想要这样的 240 个字符的随机字符串...
1|nvbqwunxjyxujtnqnqcxzhintxyswmsjdunbmuhkifbncgrucxchzirgkybtbcadrjjtjunroewmpidxwiobcvimbolzcjlmeddvusgqzmcakffyzqllbzihnvbqwunxjyxujtnqnqcxzhintxyswmsjdunbmuhkifbncgrucxchzirgkybtbcadrjjtjunroewmpidxwiobcvimbolzcjlmeddvusgqzmcakffyzqllbzih
请帮助我如何增加 Laravel Sanctum Token
的长度打开文件:
app/Models/User.php
然后检查是否使用了这个文件
Laravel\Sanctum\HasApiTokens
trait覆盖方法:
createToken
像这样:use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Str; use Laravel\Sanctum\HasApiTokens; use Laravel\Sanctum\NewAccessToken; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * Create a new personal access token for the user. * * @param string $name * @param array $abilities * @return \Laravel\Sanctum\NewAccessToken */ public function createToken(string $name, array $abilities = ['*']) { $token = $this->tokens()->create([ 'name' => $name, 'token' => hash('sha256', $plainTextToken = Str::random(240)), 'abilities' => $abilities, ]); return new NewAccessToken($token, $token->getKey().'|'.$plainTextToken); } }
您可以使用:
$user->createToken('your name token')->plainTextToken;