迁移8x8 JaaS集成代码至新服务器,生成签名JWT遇超时错误求助
生成签名JWT时触发执行超时错误
我正尝试将8x8的jaas服务集成到应用中,两年前按教程操作一切正常,迁移到新服务器后服务无法运行。查阅文档发现实现有变更,目前已完成基础代码部署,卡在JWT生成环节——这正是两年间变更的部分。
跳过三周调试,现在目标仅为生成签名JWT。我独立搭建了全新环境,用Composer引入依赖,直接复制了web-token库的示例代码,但运行时触发超时错误:
Maximum execution time of 30 seconds exceeded /brick/math/src/Internal/Calculator/NativeCalculator.php 406
所用代码
use Jose\Component\Core\AlgorithmManager; use Jose\Component\Core\JWK; use Jose\Component\Signature\Algorithm\RS256; use Jose\Component\Signature\JWSBuilder; $algorithmManager = new AlgorithmManager([ new RS256(), ]); $keyFile = dirname(__FILE__, 6) . "/sites/RsaPrivateKey.pk"; $contents = file_get_contents($keyFile); use Jose\Component\KeyManagement\JWKFactory; $key = JWKFactory::createFromKeyFile( $keyFile, // The filename null, // Secret if the key is encrypted, otherwise null [ 'use' => 'sig', // Additional parameters ] ); echo "done"; $jwsBuilder = new JWSBuilder($algorithmManager); // The payload we want to sign. The payload MUST be a string hence we use our JSON Converter. $payload = json_encode([ 'exp' => time() + 3600, 'iss' => 'My service', 'aud' => 'jitsi', ]); $jws = $jwsBuilder ->create() // We want to create a new JWS ->withPayload($payload) // We set the payload ->addSignature($key, ['alg' => 'RS256']) // We add a signature with a simple protected header ->build(); // We build it use Jose\Component\Signature\Serializer\CompactSerializer; $serializer = new CompactSerializer(); // The serializer $token = $serializer->serialize($jws, 0); // We serialize the signature at index 0 (we only have one signature).
问题解决方法
- 启用PHP的BCMath或GMP扩展:这是核心解决方案。RSA签名依赖大量大整数运算,纯PHP实现的计算速度极慢,很容易触发超时。
- Debian/Ubuntu系统:执行
sudo apt install php-bcmath或sudo apt install php-gmp,安装后重启PHP服务。 - CentOS/RHEL系统:执行
sudo yum install php-bcmath或sudo yum install php-gmp,安装后重启PHP服务。 - 验证扩展是否启用:运行
php -m命令,查看输出中是否包含bcmath或gmp。
- Debian/Ubuntu系统:执行
- 验证RSA私钥有效性:确保
RsaPrivateKey.pk是有效未损坏的RSA私钥,可通过openssl rsa -in RsaPrivateKey.pk -check命令验证。 - 临时调整执行时间(权宜之计):若暂时无法安装扩展,可在代码开头添加
ini_set('max_execution_time', 60);调高超时时间,但这只是临时方案,安装扩展才能彻底解决性能问题。
内容的提问来源于stack exchange,提问作者user1794918
相关产品推荐
相关产品推荐

