gpt4 book ai didi

python - 如何在 django 中重用原始用户的密码?

转载 作者:行者123 更新时间:2023-11-30 23:54:29 24 4
gpt4 key购买 nike

当用户登录时,他提供了他的姓名和原始密码,该密码经过哈希处理并与数据库的值进行比较。


def login(request):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
# user is active
auth.login(request, user)
# relink to right page
return HttpResponseRedirect("/account/loggedin/")
else:
# error page
return HttpResponseRedirect("/account/invalid/")

或者我可以使用:


@login_required
def index(request):
if request.user.is_authenticated():
return render_to_response('polls/index.html', {'sessionDic' : request.session})
else:
#some stuff

问题是:用户登录后,以下请求仅包含经过检查的 cookie,用户无需再次输入其凭据。

<小时/>

但是,我需要在每种方法中的 View 中拥有原始用户的密码才能登录 Linux 用户并以此用户身份执行一些 Linux 程序。例如,su 程序用于切换 ritgh linux 用户:


def ssh_command (user, password, command):
child = pexpect.spawn('su -l %s -c \'%s\'' % (user, command))
i = child.expect([pexpect.TIMEOUT, pexpect.EOF, 'Password: '])
if i == 0: # Timeout
print 'ERROR!'
print 'su can\'t be executed:'
print child.before, child.after
return None
if i == 1: # EOF
print 'ERROR'
print 'EOF error'
print child.before, child.after
return None
child.sendline(password)
return child

def main ():
user = 'test'
password = 'test'
child = ssh_command (user, password, 'curl habrahabr.ru | wc -c')
child.expect(pexpect.EOF)
print child.before
print child.after
print child.match

如何存储原始用户的密码并将其替换为所需的功能?

最佳答案

您可以将其存储在登录 View 功能的 session 数据中。至少这样它就会随着 session 而消失。另一种选择是将其存储在数据库字段中,如果某些黑客获得了数据库访问权限,那将是可怕的。至少,如果黑客在 session 中使用密码获取数据库访问权限,他们只会获得当前 session 的纯文本密码。确保适本地设置 session 超时,或鼓励用户注销并删除注销时的 session 数据。

关于python - 如何在 django 中重用原始用户的密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com