轉載他人博客地址:https://blog.csdn.net/hz_blog/article/details/8426625java
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方式加密
spring
<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加密,並添加全局加密鹽
ide
<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>
第五種:使用哈希算法加密,加密強度爲256spa
<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
.net
<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經過表單提交的用戶認證信息中的密碼作各類加密操做。而咱們存儲用戶密碼的時候,能夠經過一下程序完成用戶密碼操做:
code
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再加全局加密鹽加密的方式加密 } }