配置参考
扩展包配置
使用 RSA/ECDSA
1 2 3 4 5 6 7 8 9 10 11
# config/packages/lexik_jwt_authentication.yaml
#...
lexik_jwt_authentication:
secret_key: '%kernel.project_dir%/config/jwt/private.pem' # path to the secret key OR raw secret key, required for creating tokens
public_key: '%kernel.project_dir%/config/jwt/public.pem' # path to the public key OR raw public key, required for verifying tokens
pass_phrase: 'yourpassphrase' # required for creating tokens
# Additional public keys are used to verify signature of incoming tokens, if the key provided in "public_key" configuration node doesn't verify the token
additional_public_keys:
- '%kernel.project_dir%/config/jwt/public1.pem'
- '%kernel.project_dir%/config/jwt/public2.pem'
- '%kernel.project_dir%/config/jwt/public3.pem'
使用 HMAC
1 2 3 4
# config/packages/lexik_jwt_authentication.yaml
#...
lexik_jwt_authentication:
secret_key: yoursecret
完整默认配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
# config/packages/lexik_jwt_authentication.yaml
# ...
lexik_jwt_authentication:
secret_key: ~
public_key: ~
pass_phrase: ~
token_ttl: 3600 # token TTL in seconds, defaults to 1 hour
clock_skew: 0
allow_no_expiration: false # set to true to allow tokens without exp claim
# token encoding/decoding settings
encoder:
# token encoder/decoder service - default implementation based on the lcobucci/jwt library
service: lexik_jwt_authentication.encoder.lcobucci
# encryption algorithm used by the encoder service
signature_algorithm: RS256
# token extraction settings
token_extractors:
# look for a token as Authorization Header
authorization_header:
enabled: true
prefix: Bearer
name: Authorization
# check token in a cookie
cookie:
enabled: false
name: BEARER
# check token in query string parameter
query_parameter:
enabled: false
name: bearer
# check token in a cookie
split_cookie:
enabled: false
cookies:
- jwt_hp
- jwt_s
# remove the token from the response body when using cookies
remove_token_from_body_when_cookies_used: true
# invalidate the token on logout by storing it in the cache
blocklist_token:
enabled: true
cache: cache.app
编码器配置
service
默认为 lexik_jwt_authentication.encoder.lcobucci
,它基于 Lcobucci/JWT 库。
为了获得具有更高加密支持的更高级令牌编码,请参阅 Spomky-Labs/lexik-jose-bridge,它基于优秀的 web-token/jwt-framework 库。
要创建您自己的编码器服务,请参阅 JWT 编码器服务自定义章节。
signature_algorithm
默认编码器服务支持的加密算法之一。
- HS256, HS384, HS512 (HMAC)
- RS256, RS384, RS512 (RSA)
- ES256, ES384, ES512 (ECDSA)
自动生成 Cookie
现在,当 cookie 令牌提取器启用 #753 时,您可以自动生成安全且 httpOnly 的 Cookie。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
token_extractors:
cookie:
enabled: true
name: BEARER
# ...
set_cookies:
BEARER: ~
# Full config with defaults:
# BEARER:
# lifetime: null (defaults to token ttl)
# samesite: lax
# path: /
# domain: null (null means automatically set by symfony)
# secure: true (default to true)
# httpOnly: true
# partitioned: false
自动生成拆分 Cookie
您还可以自动生成拆分 Cookie。这种方法的优点在 这篇文章中有所介绍。
将签名 cookie (jwt_s) 的生命周期设置为 0 以创建会话 cookie。
请注意,某些浏览器不支持 SameSite 属性 某些浏览器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
token_extractors:
split_cookie:
enabled: true
cookies:
- jwt_hp
- jwt_s
set_cookies:
jwt_hp:
lifetime: null
samesite: strict
path: /
domain: null
httpOnly: false
partitioned: false # Only for Symfony 6.4 or higher
split:
- header
- payload
jwt_s:
lifetime: 0
samesite: strict
path: /
domain: null
httpOnly: true
partitioned: false # Only for Symfony 6.4 or higher
split:
- signature
使用 Cookie 时将令牌保留在正文中
使用 Cookie 时,响应默认为空正文和结果代码 204。可以修改此行为。
请注意,这会使先前提到文章中的一个要求失效,即“JavaScript/前端永远不应访问完整的 JWT”。
1
remove_token_from_body_when_cookies_used: false
安全配置
1 2 3 4 5 6 7 8 9 10 11 12 13
# config/packages/security.yaml
security:
enable_authenticator_manager: true # Only for Symfony 5.4
firewalls:
api:
# ...
jwt: ~ # enables the jwt authenticator
# Full config with defaults:
# jwt:
# provider: null (you can put provider here or just ignore this config)
# authenticator: lexik_jwt_authentication.security.jwt_authenticator (default jwt authenticator)
# ...
验证器
有关在您的应用程序中使用自定义验证器的更多详细信息,请参阅 扩展 JWT 验证器。
无数据库用户提供器
对于无数据库身份验证(即信任 JWT 数据而不是从数据库重新加载用户),请参阅 “无数据库用户提供器”。
本作品,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。