简体   繁体   English

如何在安全性xml中配置BCryptPasswordEncoder

[英]How to configure BCryptPasswordEncoder in security xml

I read every API and documentation of spring security but i cant find how to configure the BCryptPasswordEncoder strength parameter in the spring security bean xml. 我阅读了Spring Security的所有API和文档,但找不到如何在Spring Security bean xml中配置BCryptPasswordEncoder强度参数。

trying to do somthing like: BCryptPasswordEncoder(int strength); 尝试做类似的事情: BCryptPasswordEncoder(int strength);

My security.xml: 我的security.xml:

<bean id="bCryptPasswordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />


<security:authentication-manager>
    <security:authentication-provider
        user-service-ref="userDetailsServiceImpl">
        <security:password-encoder ref="bCryptPasswordEncoder" />
    </security:authentication-provider>
</security:authentication-manager>  

For this you would use Spring's constructor dependency injection on the BCryptPasswordEncoder. 为此,您可以在BCryptPasswordEncoder上使用Spring的构造函数依赖项注入

<bean id="bCryptPasswordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    <constructor-arg value="100"/>
</bean>

<security:authentication-manager>
    <security:authentication-provider
        user-service-ref="userDetailsServiceImpl">
        <security:password-encoder ref="bCryptPasswordEncoder" />
    </security:authentication-provider>
</security:authentication-manager>

As of Spring 3.1 you can make this more concise using the c-namespace . 从Spring 3.1开始,您可以使用c-namespace使其更加简洁。 For example: 例如:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="bCryptPasswordEncoder"
          class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"
          c:strength="100"/>

    <security:authentication-manager>
        <security:authentication-provider
            user-service-ref="userDetailsServiceImpl">
            <security:password-encoder ref="bCryptPasswordEncoder" />
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

You will notice that in this example 您会注意到在此示例中

  • There is a new xmlns:c declaration 有一个新的xmlns:c声明
  • the value after c: in bCryptPasswordEncoder corresponds to the constructor argument name. bCryptPasswordEncoder中c:之后的值对应于构造函数参数名称。 Alternatively you could use c:_0 to specify the index. 或者,您可以使用c:_0指定索引。

See the previous link for more details on the c-namespace. 有关c-命名空间的更多详细信息,请参见上一个链接。

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

相关问题 解码 Spring 安全 BCryptPasswordEncoder - Decoding Spring security BCryptPasswordEncoder BCryptPasswordEncoder Spring Security未定义 - BCryptPasswordEncoder Spring Security not defining Spring Security - BcryptPasswordEncoder - Spring Security - BcryptPasswordEncoder 如何使用批注(无xml)配置spring-security? - How to configure spring-security with annotations (no xml)? Spring Security 和 BCryptPasswordEncoder 用于注册和登录 - Spring Security and BCryptPasswordEncoder for registration and login Spring Security BCryptPasswordEncoder已插入但不匹配 - Spring Security BCryptPasswordEncoder Inserted But Not Match 如何使用org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder解密存储在mongodb中的密码? - How to decrypt password which is stored in mongodb using org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder? Spring Security 5:找不到 BCryptPasswordEncoder 类型的 Beans - Spring Security 5 : No Beans of type BCryptPasswordEncoder found 如何配置spring security - How to configure spring security 使用JPA的Spring安全性。如何配置applicationContext-security.XML文件?(使用DaoAuthenticationProvider) - Spring security using JPA. How to configure applicationContext-security.XML file?(using DaoAuthenticationProvider)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM