【问题标题】:how to authenticate user in spring security with password encryption如何使用密码加密在 Spring Security 中对用户进行身份验证
【发布时间】:2014-11-09 08:18:08
【问题描述】:

我在我的应用程序中使用spring security。在我的数据库中。密码是加密形式。所以在我发送密码时登录时,密码应该转换为加密形式,然后我应该能够比较密码即时发送和数据库中存在的密码。如果匹配,则应该成功登录。 这是我的 spring-security.xml

 <authentication-manager">
        <authentication-provider >
            <password-encoder ref="encoder"/>
                <jdbc-user-service data-source-ref="dataSource"
                    users-by-username-query="select email,password from user where email=?"
                     /> 
            </authentication-provider>
</authentication-manager>
<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>

这是我的用户表,其中包含电子邮件、密码、联系人编号、地址。 任何帮助我如何检查用户输入的密码的加密密码值以及如何检查它是否匹配?

【问题讨论】:

    标签: spring-mvc encryption spring-security


    【解决方案1】:

    您可以编写自己的 PasswordEncoder 将您的用户转移到 Spring Secure。

    @Component("PasswordEncoder")
    public class PasswordEncoderimpl implements PasswordEncoder{
        @Override
        public String encodePassword(String rawPass, Object salt) {
            //it is the algorithm the transfer password to encrypted password
        }
        @Override
        public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
            //encPass is the password in your database
            //rawPass is the password user entering 
            //then you can write it like
            return encPass.euqals(encodePassword(rawPass));
        }
    }
    

    那么你的 spring-security.xml 将是:

    <authentication-manager>
        <authentication-provider>   
          <password-encoder ref="PasswordEncoder">
          </password-encoder>
         </authentication-provider>
    </authentication-manager>
    

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 2020-01-18
      • 2011-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2020-04-16
      • 2020-08-17
      相关资源
      最近更新 更多