找不到 Symfony 5 JWT 令牌
Symfony 5 JWT Token not found
我正在使用带有 apache 服务器的 docker 图像,我正在尝试 运行 JWT 身份验证 在 Symfony 5 上使用 LexikJWTAuthenticationBundle
。我遵循了官方文档 LexikJWTAuthenticationDocs 这是我所做的 :
我运行命令:php bin/console lexik:jwt:generate-keypair
这给了我一个 public 和 private 密钥对。我确保私钥未加密,正如我在 post 答案中看到的那样:.
这里是security.yaml
的配置:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
# used to reload user from session & other features (e.g. switch_user)
database:
entity:
class: App\Entity\User
property: email
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
这是我的 routes.yaml
:
api_login_check:
path: /api/login_check
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 time to live 1 hour
token_ttl: 3600
.env
:
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=test
我使用所有这些配置 postman 从用户那里检索令牌:
postman get token
并且使用此令牌,我现在正尝试从 api api/users
的每个用户获取
postman get users
但响应是 401 状态 和 JWT Token not found
我的 Apache 配置是:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
这可能与 apache 配置有关,正如我在很多 Stack Overflow 问题中看到的那样,但我已经尝试了很多,但即使是 LexikJWTAuthenticationBundle :
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=
为什么我可以创建令牌但不能使用他访问数据?
这与 .htaccess
或 000-default.conf
无关,您必须更改 conf in : /etc/apache2/apache2.conf
并在文件末尾添加 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=
。
但是我没有找到任何资源来解释为什么 apache 删除授权 header
我正在使用带有 apache 服务器的 docker 图像,我正在尝试 运行 JWT 身份验证 在 Symfony 5 上使用 LexikJWTAuthenticationBundle
。我遵循了官方文档 LexikJWTAuthenticationDocs 这是我所做的 :
我运行命令:php bin/console lexik:jwt:generate-keypair
这给了我一个 public 和 private 密钥对。我确保私钥未加密,正如我在 post 答案中看到的那样:
这里是security.yaml
的配置:
security:
encoders:
App\Entity\User:
algorithm: bcrypt
providers:
# used to reload user from session & other features (e.g. switch_user)
database:
entity:
class: App\Entity\User
property: email
firewalls:
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
这是我的 routes.yaml
:
api_login_check:
path: /api/login_check
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 time to live 1 hour
token_ttl: 3600
.env
:
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=test
我使用所有这些配置 postman 从用户那里检索令牌:
postman get token
并且使用此令牌,我现在正尝试从 api api/users
postman get users
但响应是 401 状态 和 JWT Token not found
我的 Apache 配置是:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
这可能与 apache 配置有关,正如我在很多 Stack Overflow 问题中看到的那样,但我已经尝试了很多,但即使是 LexikJWTAuthenticationBundle :
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=
为什么我可以创建令牌但不能使用他访问数据?
这与 .htaccess
或 000-default.conf
无关,您必须更改 conf in : /etc/apache2/apache2.conf
并在文件末尾添加 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=
。
但是我没有找到任何资源来解释为什么 apache 删除授权 header