【问题标题】:How to add custom password encoder to Spring Security?如何将自定义密码编码器添加到 Spring Security?
【发布时间】:2012-09-12 04:09:26
【问题描述】:

我正在尝试将 Web 项目转换为 Spring。 我已经合并了 Spring Security,但是旧项目使用 SHA-1 转换为十六进制字符串来加密用户密码。

我想知道如何制作一个自定义密码编码器,让我能够准确指定我希望如何加密密码。

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    您可以直接使用它。 构造函数参数是 SHA Encoder 的强度

    <beans:bean id="passwordEncoder"
        class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
        <beans:constructor-arg index="0" value="1" />
    </beans:bean>
    

    【讨论】:

      【解决方案2】:

      当然,您可能不需要自定义编码器,因为使用 Security 的命名空间配置 &lt;password-encoder&gt; 非常容易:

      <password-encoder hash="sha" base64="true" />
      

      将此行放入您的 security-context.xml 中,它将使用 ShaPasswordEncoder 并启用 SHA-1 算法和 BASE64 编码。

      【讨论】:

      • 谢谢。在我的身份验证管理器标签中,我有&lt;security:password-encoder ref="passwordEncoder"/&gt;。我有豆子&lt;bean class="org.springframework.security.providers.encoding.ShaPasswordEncoder" id="passwordEncoder"/&gt;。但我一直收到错误消息说找不到org.springframework.security.encoding.ShaPasswordEncoder
      • 你使用什么版本的 Spring Security?
      • 在 3.1 中是 org.springframework.security.authentication.encoding.ShaPasswordEncoder
      猜你喜欢
      • 2011-12-01
      • 2012-01-17
      • 2014-04-23
      • 2017-03-14
      • 2023-03-09
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多