我正在开发一个使用django-rest-framework-simplejwt的JWTAuthentication的django rest api应用程序。由于使用的是RSA算法,因此需要设置签名和验证密钥。
下面的实现对我来说很有效。
SIMPLE_JWT = {
'SIGNING_KEY': open('jwtRS256.key').read() if os.path.isfile('./jwtRS256.key') else None,
'VERIFYING_KEY': open('jwtRS256.key.pub').read() if os.path.isfile('./jwtRS256.key.pub') else None,
}在升级到django 3并运行py -Wa manage.py test之后。这是正在显示的一些警告消息。
D:\path\to\settings.py:398: ResourceWarning: unclosed file <_io.TextIOWrapper name='jwtRS256.key' mode='r' encoding='cp1252'>
'SIGNING_KEY': open('jwtRS256.key').read() if os.path.isfile('./jwtRS256.key') else None,
ResourceWarning: Enable tracemalloc to get the object allocation traceback
D:\path\to\\settings.py:399: ResourceWarning: unclosed file <_io.TextIOWrapper name='jwtRS256.key.pub' mode='r' encoding='cp1252'>
'VERIFYING_KEY': open('jwtRS256.key.pub').read() if os.path.isfile('./jwtRS256.key.pub') else None,
ResourceWarning: Enable tracemalloc to get the object allocation traceback我尝试了一种替代方案来解决这个问题,但它似乎在验证用户身份时破坏了应用程序。这是尝试的解决方案。
def get_file(file_url):
if os.path.isfile(file_url):
with open(file_url) as f:
return f
return None
SIMPLE_JWT = {
'SIGNING_KEY': get_file('./jwtRS256.key'),
'VERIFYING_KEY': get_file('./jwtRS256.key.pub')
}当尝试登录并使用TypeError: Expecting a PEM-formatted key.返回500时,这不起作用
发布于 2021-02-04 04:32:49
是我的错。我忘了看文件了。
def get_file(file_url):
if os.path.isfile(file_url):
with open(file_url) as f:
return f.read()
return None发布于 2021-02-04 04:43:55
你可以这样试一下
def get_file(file_url):
file_path = os.path.join(settings.MEDIA_ROOT, file_url)
if os.path.exists(file_path):
with open(file_path,'rb') as f:
return f.read()
return Nonehttps://stackoverflow.com/questions/66033298
复制相似问题