php JWTEncodeFailureException:无法从给定配置创建签名的JWT

j9per5c4  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(126)

我在项目symfony中启动单元测试时发现了这个错误
Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEencodeFailureException:无法从给定配置创建签名的JWT。
应用程序正在工作,但在单元测试中,我发现了这个错误,两个键公共和私有存在于配置/jwt文件夹
.env.测试:

KERNEL_CLASS='App\Kernel'

.env:

APP_SECRET=hello

###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PASSPHRASE=hellohello
###< lexik/jwt-authentication-bundle ###

当我在.env.test中添加配置lexikjwtauthentification时,也会出现错误

n8ghc7c1

n8ghc7c11#

1.为UnitTest生成SSL密钥
1.将密钥存储在文件夹中
1.在phpunit.xml.dist中配置键
来自现有项目配置的示例:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="bin/.phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
>
    <php>
        <!-- Other config stuff -->
        
        <!-- ###+ lexik/jwt-authentication-bundle ### -->
        <env name="JWT_SECRET_KEY" value="%kernel.project_dir%/tests/Fixtures/jwt/private.pem"/>
        <env name="JWT_PUBLIC_KEY" value="%kernel.project_dir%/tests/Fixtures/jwt/public.pem"/>
        <env name="JWT_PASSPHRASE" value="your-passphrase"/>
        <!-- ###- lexik/jwt-authentication-bundle ### -->
        
        <!-- Other config stuff -->
    </php>
    <!-- Other config stuff -->
</phpunit>
# config/packages/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)%'
    # Other config stuff

相关问题