Symfony 4 - 使用 LexikJWTAuthenticationBundle 找不到 JWT
Symfony 4 - JWT not found with LexikJWTAuthenticationBundle
下午好,
我尝试在我的项目中使用 LexikJWTAuthenticationBundle,但我遇到了未生成令牌的问题。我已经在 var/jwt 目录中设置了私钥和 public 密钥。
API returns 当我尝试使用登录路由时的响应:
{
"code": 401,
"message": "JWT Token not found"
}
Apache 虚拟主机:
<VirtualHost *:80>
ServerName ypostirixi
DocumentRoot "/var/www/ypostirixi/public"
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</VirtualHost>
.htaccess 文件在 public 目录中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?p= [QSA,L]
</IfModule>
security.yaml 安全性:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
doctrine_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api_doc:
pattern: ^/api/doc
security: false
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
pattern: ^/
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
provider: doctrine_provider
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
我希望能成功使用登录路由并在其他路由上生成有效令牌。
您不允许匿名访问任何防火墙。您应该在主防火墙中添加匿名选项。
main:
pattern: ^/
stateless: true
anonymous: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
provider: doctrine_provider
- 可能您忘记在防火墙或登录参数部分(电子邮件作为用户名)配置登录防火墙..
用这个检查
1 config/packages/security.yaml
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
form_login:
check_path: /api/login_check
username_parameter: email
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/, role: IS_AUTHENTICATED_FULLY }
2 config/routes.yaml
api_login_check:
path: /api/login_check
3 用curl测试
X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"email":"user@example.com","password":"pass"}'
注意:如果不适合您,可能是您跳过了配置步骤,或者您没有正确配置包,您必须查看文档 https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation
感谢您的帮助。
我发现了有关此升级的问题,但我有解决方案。
在 lexik_jwt_authentication.yaml 文件中:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: '%env(JWT_TTL)%'
token_extractors:
authorization_header:
enabled: true
prefix: '%env(JWT_TOKEN_PREFIX)%'
name: Authorization
user_identity_field: email
下午好,
我尝试在我的项目中使用 LexikJWTAuthenticationBundle,但我遇到了未生成令牌的问题。我已经在 var/jwt 目录中设置了私钥和 public 密钥。
API returns 当我尝试使用登录路由时的响应:
{
"code": 401,
"message": "JWT Token not found"
}
Apache 虚拟主机:
<VirtualHost *:80>
ServerName ypostirixi
DocumentRoot "/var/www/ypostirixi/public"
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</VirtualHost>
.htaccess 文件在 public 目录中:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) index.php?p= [QSA,L]
</IfModule>
security.yaml 安全性:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
doctrine_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api_doc:
pattern: ^/api/doc
security: false
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
pattern: ^/
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
provider: doctrine_provider
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
我希望能成功使用登录路由并在其他路由上生成有效令牌。
您不允许匿名访问任何防火墙。您应该在主防火墙中添加匿名选项。
main:
pattern: ^/
stateless: true
anonymous: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
provider: doctrine_provider
- 可能您忘记在防火墙或登录参数部分(电子邮件作为用户名)配置登录防火墙..
用这个检查
1 config/packages/security.yaml
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
form_login:
check_path: /api/login_check
username_parameter: email
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/, role: IS_AUTHENTICATED_FULLY }
2 config/routes.yaml
api_login_check:
path: /api/login_check
3 用curl测试
X POST -H "Content-Type: application/json" http://localhost/api/login_check -d '{"email":"user@example.com","password":"pass"}'
注意:如果不适合您,可能是您跳过了配置步骤,或者您没有正确配置包,您必须查看文档 https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation
感谢您的帮助。
我发现了有关此升级的问题,但我有解决方案。
在 lexik_jwt_authentication.yaml 文件中:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: '%env(JWT_TTL)%'
token_extractors:
authorization_header:
enabled: true
prefix: '%env(JWT_TOKEN_PREFIX)%'
name: Authorization
user_identity_field: email