如何在Java Config中注册SaltSource(无xml)

时间:2014-04-10 17:22:10

标签: spring spring-security

我正在设置一个不使用xml的新Web应用程序(没有web.xml,没有spring.xml)。我几乎一切都工作,除了我无法弄清楚如何注册SaltSource。我需要用Java等效替换以下内容。

<authentication-manager>
  <authentication-provider user-service-ref="authService" >
   <password-encoder hash="sha" ref="myPasswordEncoder">
    <salt-source user-property="salt"/>
   </password-encoder>
  </authentication-provider>
</authentication-manager>

到目前为止,我已经用Java了。

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    ReflectionSaltSource rss = new ReflectionSaltSource();
    rss.setUserPropertyToUse("salt");

    auth.userDetailsService(authService).passwordEncoder(new MyPasswordEncoder());
    // How do I set the saltSource down in DaoAuthenticationProvider
}

那么如何注册SaltSource以便它最终出现在DaoAuthenticationProvider中(就像xml过去一样)?

1 个答案:

答案 0 :(得分:12)

我通过以下方式开始工作:

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    ReflectionSaltSource rss = new ReflectionSaltSource();
    rss.setUserPropertyToUse("salt");
    DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    provider.setSaltSource(rss);
    provider.setUserDetailsService(authService);
    provider.setPasswordEncoder(new MyPasswordEncoder());
    auth.authenticationProvider(provider);
}