如果在dumps
method in signing.py
仔細觀察,你會發現它接受一個關鍵的簽名密鑰:
如果關鍵是沒有,settings.SECRET_KEY來代替。
def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer, compress=False):
"""
Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
None, settings.SECRET_KEY is used instead.
If compress is True (not the default) checks if compressing using zlib can
save some space. Prepends a '.' to signify compression. This is included
in the signature, to protect against zip bombs.
Salt can be used to namespace the hash, so that a signed string is
only valid for a given namespace. Leaving this at the default
value or re-using a salt value across different parts of your
application without good cause is a security risk.
The serializer is expected to return a bytestring.
"""
因此,所有你需要做的就是繞過每次不同的密鑰:
SECRET_KEY = "abc"
print signing.dumps("value", key=SECRET_KEY)
SECRET_KEY = "123"
print signing.dumps("value", key=SECRET_KEY)
也就是說,這看起來像一個壞主意,我因爲你沒有使用默認簽名鍵。如果您確實需要簽署文本,請使用Signer class
創建並實例化新對象,並使用它,如Signer(key="NEW KEY")
。
您還可以使用:signer = Signer('my-other-secret')來設置密鑰 – jbiz