使用限制登录尝试将Bcrypt编码添加到Spring MVC Security

时间:2015-05-22 08:43:20

标签: java spring spring-mvc spring-security spring-bean

我有一个有限制登录尝试的工作项目我只需要添加Bcrypt密码加密。 我有以下豆子:

<beans:bean id="customUserDetailsService"
        class="com.setelog.spring.service.CustomUserDetailsService">
        <beans:property name="usersByUsernameQuery" value="select * from users where username = ?"/>
        <beans:property name="authoritiesByUsernameQuery" value="select username, role from user_roles where username =?" />
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>

    <beans:bean id="userDetailsDao" class="com.setelog.spring.dao.UserDetailsDaoImpl" >
        <beans:property name="dataSource" ref="dataSource" />
    </beans:bean>


    <beans:bean id="authenticationProvider"
        class="com.setelog.spring.handler.LimitLoginAuthenticationProvider">
        <beans:property name="userDetailsService" ref="customUserDetailsService" />
        <beans:property name="userDetailsDao" ref="userDetailsDao" />
    </beans:bean>

    <authentication-manager>
        <authentication-provider ref="authenticationProvider" />
    </authentication-manager>

我有一个项目,它有以下启用Bcrypt:

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailsService" >
        <password-encoder hash="bcrypt" />    
    </authentication-provider>
</authentication-manager>

但我似乎无法以这种方式工作。可能是因为“ref”而不是“authentication-provider”中的“user-service-ref”。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我设法解决了!我只需要将一个编码器bean添加到authenticationProvider:

<beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>


    <beans:bean id="authenticationProvider"
        class="com.setelog.spring.handler.LimitLoginAuthenticationProvider">
        <beans:property name="userDetailsService" ref="customUserDetailsService" />
        <beans:property name="userDetailsDao" ref="userDetailsDao" />
        <beans:property name="passwordEncoder" ref="encoder" />

    </beans:bean>