【发布时间】:2021-10-31 12:09:14
【问题描述】:
我正在使用带有 apache 服务器的 docker 映像,我正在尝试使用 Symfony 5 在 Symfony 5 上运行 JWT 身份验证 LexikJWTAuthenticationBundle。我遵循了官方文档LexikJWTAuthenticationDocs 这是我所做的:
我运行了命令:php bin/console lexik:jwt:generate-keypair
这给了我一个 public 和 private 密钥对。我确保私钥未加密,正如我在此帖子答案中看到的那样:Encrypted private key。
这里是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
通过所有这些配置,我使用邮递员从用户那里检索令牌:
使用这个令牌,我现在正尝试从api/users 的每个用户的 api 获取
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=$1
为什么我可以创建令牌但不能使用他访问数据?
【问题讨论】:
-
我在这里分享我的答案来解决这个问题:stackoverflow.com/a/70787257/3556984