【问题标题】:Spring Security DB Authentication w/Hibernate and hashed passwords?带有休眠和散列密码的 Spring Security DB 身份验证?
【发布时间】:2011-02-13 02:31:40
【问题描述】:

我正在尝试设置 spring security 3 来针对我的 hibernate 3 数据库对用户进行身份验证。我只在数据库中存储密码的 sha1 哈希值(不是明文)。

我查看了thisthis,它们告诉我实现自己的UserDetailsS​​ervice。不幸的是,loadUserByUsername 吐出的 UserDetails 似乎需要明文密码,而我没有。

这通常是如何处理的? Spring Security 真的可以在这里做我需要的吗?我错过了什么吗?

【问题讨论】:

    标签: hibernate authentication hash passwords spring-security


    【解决方案1】:

    当您设置 UserDetailsS​​ervice 时,spring 使用它来加载用户,然后将它们与登录信息进行比较。这意味着,它会比较密码。 但是,您可以配置密码编码器:Doc: Adding a Password Encoder

    或者您只需编写自己的AuthenticationManager or AuthenticationProvider 来加载用户并确定用户是否成功登录。只需实现接口 AuthenticationProvider 并设置配置

    <authentication-manager>
      <authentication-provider ref="myAuthenticationProvider"/>
    </authentication-manager>
    
    <bean id="myAuthenticationProvider"
      class="stackoverflow.SuperduperMegaAuthenticationProvider">
    </bean>
    

    【讨论】:

      【解决方案2】:

      通常 Userdetails 包含一个散列密码,您只需配置 Spring Security 以使用正确的password encoder 对其进行身份验证。

       <password-encoder hash="md5"/>
      

      在 Stack Overflow 答案 @Spring Security 3 database authentication with Hibernate 中查找上述密码编码器行。

      在您的情况下,您应该将此行替换为:

      <password-encoder hash="sha"/>
      

      【讨论】:

        猜你喜欢
        • 2011-12-01
        • 1970-01-01
        • 2014-03-26
        • 2016-08-27
        • 2016-05-30
        • 2013-03-06
        • 2018-10-05
        • 2013-10-30
        • 2014-04-07
        相关资源
        最近更新 更多