匹配密码与前端或后端?

Vic*_*uez 5 javascript python validation client-side

有没有人有关于检查匹配密码的行业标准或最佳做法的任何信息(例如Gmail的"密码不匹配"反馈")?它是后端,前端还是客户端流程?还是完全?基于其他因素?

以下是我使用的代码示例(Python with Bottle)来注册用户.代码有效,但我不确定是否应该从后端提供flash消息(返回"密码不匹配")或者使用像JS这样的东西会更好吗?我知道有一些脚本可以验证这一点,但它们都是JS.我的问题不是如何使用JS,但这是首选的方法.

@route('/suser', method='POST')
def sign_suser():
    cemail = request.forms.get('semail')
    cpassword1 = request.forms.get('spass1')
    cpassword2 = request.forms.get('spass2')
    ctype = request.forms.get('stype')
    if cpassword1 != cpassword2:
        return "<p>Passwords do not match</p>"
    else:
        pwhash = crypt(cpassword1)
        connection = sqlite3.connect("whatever.db")
        cursor_v = connection.cursor()
        cursor_v.execute("insert into users (cemail, cpassword, atype) values (?,?,?)", (cemail,pwhash,ctype))
        connection.commit()
        cursor_v.close()
        info = {'status': 'User Added',
                'type': 'success'}
        return template('whatever',info)
Run Code Online (Sandbox Code Playgroud)

Dar*_*rum 17

在注册期间检查两个密码字段是否匹配应该完全由客户端逻辑完成.它是为了防止用户错误地在其密码中插入拼写错误而提供的.服务器端检查是没有意义的,因为您的客户端会阻止它,如果您的用户是精通技术的个人,使用curl做所有事情,那么如果他们陷入困境就会对他们进行检查.

此外,我将扩展您关于最佳做法的问题.如果没有用户首先通过链接进行验证(通常是发送到他们的电子邮件),则不应立即将用户保存在数据库中.记住:永远不要相信用户提供的任何东西.