简体   繁体   English

自动为用户设置密码-无需设计

[英]Set password for user automatically - without Devise

I'm beginner with Rails 5, i would appreciate your help. 我是Rails 5的初学者,非常感谢您的帮助。

I have an user model, where obviously save a password for the user for a possible login. 我有一个用户模型,其中显然为该用户保存了一个密码以进行可能的登录。

The thing is i want to set the password automatically based on a text_field called identification_number. 事情是我想基于一个称为Identification_numbertext_field自动设置密码

Everything I've read is about doing it with Devise gem, but I'm not using it and also don't want to. 我读过的所有内容都是关于使用Devise gem进行的,但是我没有使用它,也不想这样做。

Once again, thanks for your help. 再次感谢您的帮助。

The password field is just another string field called :identification_number for your case. 对于您的情况,密码字段只是另一个名为:identification_number字符串字段。

But , saving password in the DB as plain strings is highly unrecommended. 但是 ,强烈建议不要在数据库中将密码另存为纯字符串。

There are a lot of security issues if you save the passwords as plain strings on your database. 如果将密码以纯字符串形式保存在数据库中,则会存在很多安全问题。

Some of them are that: 其中一些是:

  • You have full access on the passwords of your users 您拥有用户密码的完全访问权限
  • If someone, somehow manages to access your database they will also have full access to the passwords of your users. 如果有人以某种方式设法访问您的数据库,他们还将具有对用户密码的完全访问权限。

In order to avoid these issues, most of the applications save the password strings as encrypted strings with some kind of salt for enhanced entropy. 为了避免这些问题,大多数应用程序将密码字符串另存为带有某种盐的加密字符串,以增强熵。

With a quick google search I found some relevant blog posts that can help you build the password encryption from scratch, such as: 通过Google的快速搜索,我发现了一些相关的博客文章,可以帮助您从头开始构建密码加密,例如:

Without using a gem: https://www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-i/ 不使用gem: https//www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-i/

Using some gems: http://railscasts.com/episodes/250-authentication-from-scratch?view=asciicast 使用一些宝石: http: //railscasts.com/episodes/250-authentication-from-scratch?view= asciicast

Apart from that, the password is not an identification_number. 除此之外,密码不是身份证号码。 I would not use that name. 我不会使用这个名字。 The password is not used to identify the user. 密码不用于标识用户。 The id is most of the time the identification number. id通常是标识号。 Better just call it :password . 最好叫它:password Also, it does not need to be a text field, it shouldn't be that long. 另外,它不必是文本字段,也不必那么长。

You can use bcrypt gem for implementing the secure password. 您可以使用bcrypt gem来实现安全密码。

The bcrypt ruby gem provides you with has_secure_password method. bcrypt红宝石gem为您提供has_secure_password方法。 The has_secure_password method encrypts passwords by hashing and salting the passwords and generate 'password_digest'. has_secure_password方法通过对密码进行哈希处理和加盐处理来加密密码,并生成“ password_digest”。

you can refer this link for more info 您可以参考此链接以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM