1

使用 django rest 框架和简单的 jwt 身份验证和 RSA 算法时出现错误。

当我使用 HS 算法时,我的代码运行良好,但是当我将算法更改为 RS 时,出现以下错误:

无法反序列化关键数据。数据可能格式不正确,或者可能使用不受支持的算法加密。

我的设置:

SIMPLE_JWT = {
    'ALGORITHM': 'RS512',
    'SIGNING_KEY' : 'ssh-rsa MIIBPAIBAAJBALZ+WuuTIol2cwEALcqn+/d0AER+sX269KVZt7sdl9C0QcspNHvHGYBWLoYIV5i72fsINX4V5IvkqsIn83O4jQMCAwEAAQJBAJ8E/Y73GAo2V8IQeNZ1iH646x7EUz9e8J1Az3PSNp7ZZ4tNjEhyA817qQGT9nfvRPXqIKkKFVe0THfmWmbK5cECIQD4M/qhoT2n99iIJwJq2DhbVvqx74hal+ocuboSwDZuIwIhALw58q4+YZlg79fGc2PyK8MUQLIx/i+O3bK7moMCf6OhAiEA04E/15IWf1clzsgnODMuuy9AjHaJJGIGHxpppObkuy8CIGqjwhRqD02gmAH90x5K8/RAIy9SF5rGLGC43R9gaQRBAiEAoLxLZuvXosXy6XR67ODCgZI7yB1XXVIwB73LxWXrnkk=',

    'VERIFYING_KEY' : 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALZ+WuuTIol2cwEALcqn+/d0AER+sX269KVZt7sdl9C0QcspNHvHGYBWLoYIV5i72fsINX4V5IvkqsIn83O4jQMCAwEAAQ=='
}

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ]
}

视图和其他使用Djoser 包

Request POST | http://localhost:8000/api/jwt/create/

 username : userExample,
 password : **********

注意:使用 Postman 进行测试

4

1 回答 1

1

它是固定的,我只需要更改签名密钥和验证密钥字符串

设置.py

SIMPLE_JWT = {
        'SIGNING_KEY' : '''-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCpjDCwMjok1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