您可以编写自己的认证后端使用原始密码:
from django.contrib.auth import backends
from django.contrib.auth.models import User
class RawPasswordUser(User):
class Meta:
proxy = True
def set_password(self, raw_password):
# default implementation made a hash from raw_password,
# we don't want this
self.password = raw_password
def check_password(self, raw_password):
# same here, don't make hash out of raw_password
return self.password == raw_password
class ModelBackend(backends.ModelBackend):
def authenticate(self, username=None, password=None):
try:
user = RawPasswordUser.objects.get(username=username)
if user.check_password(password):
return user
except RawPasswordUser.DoesNotExist:
return None
def get_user(self, user_id):
try:
return RawPasswordUser.objects.get(pk=user_id)
except RawPasswordUser.DoesNotExist:
return None
在设置文件:
AUTHENTICATION_BACKENDS = (
# ModelBackend from project_root/auth/backends.py
'auth.backends.ModelBackend',
)
现在,当你authenticate
用户在你的意见,你会得到RawPasswordUser
实例。 login_required
修饰符同样适用,request.user
将指向代理类。
有关详细信息,请参见documentation。
对于Django 1.5+ there is also an option用自定义模型替换默认用户模型,但为了保留现有用户,您必须以某种方式迁移它们,请参阅this question。
其实你不能保持用户密码不变。
通过以下格式默认的Django存储密码:
算法$迭代$盐$哈希
这意味着:
,我看到的最简单的解决办法是保持目前的Django的行为,被Tadeck的意见建议,增加对散列客户端,并强制用户更改他们的密码。
好吧,这不是一个真正的解决方案,因为攻击者可以截取消化的密码并直接使用它们,但是您提到了它的问题更新。既然你不关心安全性,你也可以在JavaScript中检出public key encryption。
通过Tadeck提出的另一种解决方案是使用OAuth般的服务,这可能看起来有点像这样:
def index(request):
access_token = request.REQUEST.get('token', None)
if not access_token:
return redirect('login')
# Custom authentication backend that accepts a token
# and searches for a user with that token in database.
user = authenticate(access_token)
if not user:
return redirect('login')
return render(...)
def auth(request):
''' This ajax-view has to be encrypted with SSL.'''
# Normal Django authentication.
user = authenticate(request.POST['username'], request.POST['password'])
# Authentication failed
if user is None:
return json.dumps({'error': '...'})
# generate, save and return token in json response
token = UserToken(user=user, value=generate_token())
# token.expires_at = datetime.now() + timedelta(days=1)
token.save()
return json.dumps({'token': token.value})
的攻击仍然可以拦截访问令牌,但它是一个有点比拦截更好密码哈希。
欢迎来到Stack Overflow。请[尝试使用更礼貌的语言](http://meta.stackexchange.com/questions/22232/are-expletives-allowed-on-se-sites/22233#22233)。 – nnnnnn 2013-05-02 13:30:51
谢谢。不好意思。我在过去的33个小时里没有睡觉,并且因为一个并非由我的错误而导致的问题而让我头部受伤。 – GhaghaSibil 2013-05-02 13:38:03