轉載他人博客地址:https://blog.csdn.net/hz_blog/article/details/8426625
Acegi 對於密碼提供三種方式:明文及不采用任何加密方式、MD5加密方式、哈希算法加密方式。
只需要在DAO的認證管理器中分別加入以下對應配置:
第一種:不使用任何加密方式的配置
<bean id="daoAuthenticationProvider"
class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="userDetailsService" />
<!-- 明文加密,不使用任何加密算法, 在不指定該配置的情況下,Acegi默認采用的就是明文加密 -->
<!-- <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.PlaintextPasswordEncoder">
<property name="ignorePasswordCase" value="true"></property> </bean> </property> -->
</bean>
第二種:MD5方式加密
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService" /> <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"> <!-- false 表示:生成32位的Hex版, 這也是encodeHashAsBase64的, Acegi 默認配置; true 表示:生成24位的Base64版 --> <property name="encodeHashAsBase64" value="false" /> </bean> </property> </bean>
第三種:使用MD5加密,並添加全局加密鹽
Java代碼
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService" /> <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"> <property name="encodeHashAsBase64" value="false" /> </bean> </property> <!-- 對密碼加密算法中使用特定的加密鹽及種子 --> <property name="saltSource"> <bean class="org.acegisecurity.providers.dao.salt.SystemWideSaltSource"> <property name="systemWideSalt" value="acegisalt" /> </bean> </property> </bean>
第四種:使用MD5加密,並添加動態加密鹽
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService" /> <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"> <property name="encodeHashAsBase64" value="false" /> </bean> </property> <!-- 對密碼加密算法中使用特定的加密鹽及種子 --> <property name="saltSource"> <!-- 通過動態的加密鹽進行加密,該配置通過用戶名提供加密鹽, 通過UserDetails的getUsername()方式 --> <bean class="org.acegisecurity.providers.dao.salt.ReflectionSaltSource"> <property name="userPropertyToUse" value="getUsername" /> </bean> </property> </bean>
第五種:使用哈希算法加密,加密強度為256
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService" /> <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.ShaPasswordEncoder"> <constructor-arg value="256" /> <property name="encodeHashAsBase64" value="false" /> </bean> </property> </bean>
第六種:使用哈希算法加密,加密強度為SHA-256
<bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"> <property name="userDetailsService" ref="userDetailsService" /> <property name="passwordEncoder"> <bean class="org.acegisecurity.providers.encoding.ShaPasswordEncoder"> <constructor-arg value="SHA-256" /> <property name="encodeHashAsBase64" value="false" /> </bean> </property> </bean>
上述配置只是在Acegi通過表單提交的用戶認證信息中的密碼做各種加密操作。而我們存儲用戶密碼的時候,可以通過一下程序完成用戶密碼操作:
package org.hz.test; import java.security.NoSuchAlgorithmException; import org.springframework.security.authentication.encoding.Md5PasswordEncoder; import org.springframework.security.authentication.encoding.ShaPasswordEncoder; public class MD5Test { public static void md5() { Md5PasswordEncoder md5 = new Md5PasswordEncoder(); // false 表示:生成32位的Hex版, 這也是encodeHashAsBase64的, Acegi 默認配置; true 表示:生成24位的Base64版 md5.setEncodeHashAsBase64(false); String pwd = md5.encodePassword("1234", null); System.out.println("MD5: " + pwd + " len=" + pwd.length()); } public static void sha_256() throws NoSuchAlgorithmException { ShaPasswordEncoder sha = new ShaPasswordEncoder(256); sha.setEncodeHashAsBase64(true); String pwd = sha.encodePassword("1234", null); System.out.println("哈希算法 256: " + pwd + " len=" + pwd.length()); } public static void sha_SHA_256() { ShaPasswordEncoder sha = new ShaPasswordEncoder(); sha.setEncodeHashAsBase64(false); String pwd = sha.encodePassword("1234", null); System.out.println("哈希算法 SHA-256: " + pwd + " len=" + pwd.length()); } public static void md5_SystemWideSaltSource () { Md5PasswordEncoder md5 = new Md5PasswordEncoder(); md5.setEncodeHashAsBase64(false); // 使用動態加密鹽的只需要在注冊用戶的時候將第二個參數換成用戶名即可 String pwd = md5.encodePassword("1234", "acegisalt"); System.out.println("MD5 SystemWideSaltSource: " + pwd + " len=" + pwd.length()); } public static void main(String[] args) throws NoSuchAlgorithmException { md5(); // 使用簡單的MD5加密方式 sha_256(); // 使用256的哈希算法(SHA)加密 sha_SHA_256(); // 使用SHA-256的哈希算法(SHA)加密 md5_SystemWideSaltSource(); // 使用MD5再加全局加密鹽加密的方式加密 } }
