我目前正在编程一个网站使用Symfony版本6.2API Platform,此外还有数据库中的MongoDBDocker上的工作环境.

我正在try 添加通过JWT(LexikJWTAuthenticationBundle)连接的功能,我能够成功登录并获得令牌,但当我try 使用令牌访问其余页面时,响应总是如下所示:

{
    "code": 401,
    "message": "JWT Token not found"
}

我在搜索引擎中搜索了以下问题的原因,提供了许多解决方案(尽管不幸的是,在与最新版本的symfony相关的所有内容中都没有这些解决方案),最明显的是修改了环境并添加了以下代码:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

老实说,我不知道如何在Docker环境中更改这个命令,但当我在索引页面上使用以下代码判断令牌是否正在发送时,我得到了正确的答案,并且令牌正在发送

$token = getallheaders()['Authorization'];

print_r($token);
exit;

我在Docker上的vhost.conf:

<VirtualHost *:80>
    ServerName localhost
 
    DocumentRoot /var/www/project/public
    DirectoryIndex /index.php
 
    <Directory /var/www/project/public>
        AllowOverride None
        Order Allow,Deny
        Allow from All
 
        FallbackResource /index.php
    </Directory>
 
    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>
 
    # optionally disable the fallback resource for the asset directories
    # which will allow Apache to return a 404 error when files are
    # not found instead of passing the request to Symfony
    <Directory /var/www/project/public/bundles>
        FallbackResource disabled
    </Directory>
    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
 
    # optionally set the value of the environment variables used in the application
    #SetEnv APP_ENV prod
    #SetEnv APP_SECRET <app-secret-id>
    #SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
</VirtualHost>

我查阅了使用过的Bundle 包(LexikJWTAuthenticationBundle)的官方文档,但不幸的是,当我执行下一步时:

# config/packages/lexik_jwt_authentication.yaml
lexik_jwt_authentication:
    # ...
    api_platform:
        check_path: /api/login_check

我得到以下错误:

Unrecognized option "api_platform" under "lexik_jwt_authentication". Available options are "additional_public_keys", "allow_no_expiration", "clock_skew", "encoder", "pass_phrase", "private_key_path", "public_key", "public_key_path", "remove_token_from_body_when_cookies_used", "secret_key", "set_cookies", "token_extractors", "token_ttl", "user_id_claim", "user_identity_field".

有什么解决办法吗?

推荐答案

在将我的vhost代码添加到问题中后,我立即能够找到答案,因为我必须在此处添加以下编辑:

# optionally set the value of the environment variables used in the application
    #SetEnv APP_ENV prod
    #SetEnv APP_SECRET <app-secret-id>
    #SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</VirtualHost>

问题已成功解决

Mongodb相关问答推荐

MongoDB聚合:将文档列表转换为列表

如何限制/筛选子文档中的条目?

MongoDB合并按键更新值的对象数组

如何在 kubernetes 中获取分片 mongodb 的备份

如何在mongodb中级联删除文档?

如何在 2 个应用程序之间共享mongoose模型?

删除一对一和一对多引用 - Mongoose

node.js & express - 应用程序 struct 的全局模块和最佳实践

Java MongoDB FindOne 获取最后插入的记录

使用 Spring Boot >= 2.0.1.RELEASE 将 ZonedDateTime 保存到 MongoDB 时出现 CodecConfigurationException

删除嵌套文档数组中的嵌入文档

Golang/mgo:如何让 MongoDB 在字段中使用当前时间?

如何在 $lookup Mongodb 的 LocalField 中将字符串转换为 objectId

ZonedDateTime 与 MongoDB

try 解析序列化 JSON 字符串时处理 MongoDB 的 ISODate()

直接从 URL 查询字符串提供的 mongo 查询有多危险?

Ruby 按键值分组哈希

如何在java中删除mongodb集合中的所有文档

pre save和validate之间的mongoose区别?什么时候用哪一个?

如何从mongoose中的对象 ID 获取创建日期?