I use the mentions software stack above and I need to encrypt password before save into database. I also need to decrypt password because when someone will change password he she needs to give in the old password and then the new onw twice and I need to check the old password. I have searched a lot but I still not sure what is the right way to do this. I have found this link Encrypting but are there other hints to do this? I also not sure if maybe MongoDB provides something to protect passwords.
相关问题
- MongoDB can not create unique sparse index (duplic
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
You should not be "encrypting" the password at all. I know this sounds counter-intuitive. But there is zero reason you system should need to decrypt the password. To do so would open your database to a hacker, because if you store your decryption password in your codes/server a hacker can steal that information.
The correct process is to
hash
the password. A hash is a one-way (cannot be decypted back to the original text) process. The current standard would be to use SHA256 to hash your password. Here is a basic flow-chart:ea71c25a7a602246b4c39824b855678894a96f43bb9b71319c39700a1e045222
ea71c25a7a602246b4c39824b855678894a96f43bb9b71319c39700a1e045222
) in your database.When a user logs in you take the password he just submitted and hash it. If he enters the same password it will hash out to the same value in your database.
When a user goes to change passwords you hash the "enter your old password" to verify the old password still matches, if it does you hash the "enter your new password" and save it.
One thing I did not mention in my example is
salt
. This is something you must use in your system as it protects your data fromrainbow table
exploits. But that is for another discussion.Hope this helps :)
First read Steven Carlson´s answer about password hashing.
The good thing is that Spring Security will do this for you. Spring Security 3.2 introduced the new
org.springframework.security.crypto.password.PasswordEncoder
interface and some implementations:BCryptPasswordEncoder
,StandardPasswordEncoder
(andNoOpPasswordEncoder
).Important: Do not confuse
org.springframework.security.
crypto.password
.PasswordEncoder
with the old deprecatedorg.springframework.security.
authentication.encoding
.PasswordEncoder
The interface (and therefore the implementations) has the two methods you need:
public String encode(CharSequence rawPassword)
public boolean matches(CharSequence rawPassword, String encodedPassword)
I recommend to use
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
. TheBCryptPasswordEncoder
(in contrast to theStandardPasswordEncoder
) use an salt that is different for each password (but not global like the one fromStandardPasswordEncoder
). When you encode a raw password (public String encode(CharSequence rawPassword)
) then the returned encoded password is not just the encoded password, it also contains some meta information about the used hash-algorithm, the used salt and of course the encoded password.