2013-12-10 66 views
0

我閱讀了spring security的每個API和文檔,但是我無法找到如何配置spring security xml中的BCryptPasswordEncoder強度參數。如何在安全xml中配置BCryptPasswordEncoder

試圖做財產以後,如:BCryptPasswordEncoder(int strength);

我的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> 

回答

1

對於這一點,你會在BCryptPasswordEncoder使用Spring's constructor dependency injection

<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> 

從Spring 3.1開始,你可以使這個更簡潔using the c-namespace。例如:

<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> 

你會發現,在這個例子中

  • 有一個新的xmlns:C聲明
  • C的值後:在bCryptPasswordEncoder對應構造函數的參數名稱。或者,您可以使用c:_0指定索引。

有關c-namespace的更多詳細信息,請參閱上一個鏈接。

+0

謝謝!我會試試看,並讓你知道 – lior

+0

你好!謝謝! – lior