數(shù)據(jù)庫賬號密碼加密詳解及實例
數(shù)據(jù)庫中經(jīng)常有對數(shù)據(jù)庫賬號密碼的加密,但是碰到一個問題,在使用UserService對密碼進行加密的時候,spring security 也是需要進行同步配置的,因為spring security 中驗證的加密方式是單獨配置的。如下:
authentication-manager>
authentication-provider user-service-ref="userDetailService">
password-encoder ref="passwordEncoder" />
/authentication-provider>
/authentication-manager>
beans:bean class="com.sapphire.security.MyPasswordEncoder" id="passwordEncoder">
beans:constructor-arg value="md5">/beans:constructor-arg>
/beans:bean>
如上述配置文件所示,passwordEncoder才是在spring security對賬號加密校驗的地方。
spring security在攔截之后,會首先對用戶進行查找,通過自己定義的userDetailService來找到對應的用戶,然后由框架進行密碼的匹配驗證。
從userDetailService得到user以后,就會進入到DaoAuthenticationProvider中,這是框架中定義的 ,然后跳入其中的authenticate方法中。
該方法會進行兩個檢查,分別是
* preAuthenticationChecks : 主要進行的是對用戶是否過期等信息的校驗,調(diào)用的方法在userDetail中有定義的。
* additionalAuthenticationChecks : 這個就是用戶名密碼驗證的過程了。
而PasswordEncoder是我們xml中注入的bean,所以了,我們調(diào)用的則是我們自己完成的passwordEncoder
public class MyPasswordEncoder extends MessageDigestPasswordEncoder {
public MyPasswordEncoder(String algorithm) {
super(algorithm);
}
@Override
public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
return encPass.equals(DigestUtils.md5DigestAsHex(rawPass.getBytes()));
}
}
這是我對其實現(xiàn)的一個簡單版本,調(diào)用的就是spring自帶的加密算法,很簡單了,當然也可以使用復雜的加密方法,這個就靠自己了
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!