首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Spring Security加密对策(转载)

2014-01-08 
Spring Security加密策略(转载)转载自:http://blog.csdn.net/hz_blog/article/details/8426625?reload?Ace

Spring Security加密策略(转载)

转载自:http://blog.csdn.net/hz_blog/article/details/8426625?reload

?

Acegi 对于密码提供三种方式:明文及不采用任何加密方式、MD5加密方式、哈希算法加密方式。
只需要在DAO的认证管理器中分别加入以下对应配置:

第一种:不使用任何加密方式的配置


[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"??????class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">??
  2. ????<property?name="userDetailsService"?ref="userDetailsService"?/>????
  3. ????<!--?明文加密,不使用任何加密算法,?在不指定该配置的情况下,Acegi默认采用的就是明文加密?-->??????<!--?<property?name="passwordEncoder">?<bean?class="org.acegisecurity.providers.encoding.PlaintextPasswordEncoder">???
  4. ????????<property?name="ignorePasswordCase"?value="true"></property>?</bean>?</property>?-->??</bean>??


第二种:MD5方式加密

[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"?class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">?????????<property?name="userDetailsService"?ref="userDetailsService"?/>?????
  2. ????????<property?name="passwordEncoder">?????
  3. ????????<bean?class="org.acegisecurity.providers.encoding.Md5PasswordEncoder">?????????????????<!--?false?表示:生成32位的Hex版,?这也是encodeHashAsBase64的,?Acegi?默认配置;?true??表示:生成24位的Base64版?-->?????
  4. ????????????<property?name="encodeHashAsBase64"?value="false"?/>?????????????</bean>?????
  5. ????</property>?????</bean>??????


第三种:使用MD5加密,并添加全局加密盐

Java代码[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"?class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">?????????<property?name="userDetailsService"?ref="userDetailsService"?/>?????
  2. ????????<property?name="passwordEncoder">?????
  3. ????????<bean?class="org.acegisecurity.providers.encoding.Md5PasswordEncoder">?????????????????<property?name="encodeHashAsBase64"?value="false"?/>?????
  4. ????????</bean>?????????</property>?????
  5. ????????<!--?对密码加密算法中使用特定的加密盐及种子?-->?????
  6. ????<property?name="saltSource">?????????????<bean?class="org.acegisecurity.providers.dao.salt.SystemWideSaltSource">?????
  7. ????????????<property?name="systemWideSalt"?value="acegisalt"?/>?????????????</bean>?????
  8. ????</property>?????</bean>????


第四种:使用MD5加密,并添加动态加密盐

[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"?class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">?????????<property?name="userDetailsService"?ref="userDetailsService"?/>?????
  2. ????????<property?name="passwordEncoder">?????
  3. ????????<bean?class="org.acegisecurity.providers.encoding.Md5PasswordEncoder">?????????????????<property?name="encodeHashAsBase64"?value="false"?/>?????
  4. ????????</bean>?????????</property>?????
  5. ????????<!--?对密码加密算法中使用特定的加密盐及种子?-->?????
  6. ????<property?name="saltSource">?????????????<!--?通过动态的加密盐进行加密,该配置通过用户名提供加密盐,?通过UserDetails的getUsername()方式?-->?????
  7. ?????????<bean?class="org.acegisecurity.providers.dao.salt.ReflectionSaltSource">?????????????????<property?name="userPropertyToUse"?value="getUsername"?/>?????
  8. ?????????</bean>?????????</property>?????
  9. </bean>???

?

第五种:使用哈希算法加密,加密强度为256

[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"?class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">?????????<property?name="userDetailsService"?ref="userDetailsService"?/>?????
  2. ????????<property?name="passwordEncoder">?????
  3. ????????<bean?class="org.acegisecurity.providers.encoding.ShaPasswordEncoder">?????????????????<constructor-arg?value="256"?/>?????
  4. ????????????<property?name="encodeHashAsBase64"?value="false"?/>?????????????</bean>?????
  5. ????</property>?????</bean>????

第六种:使用哈希算法加密,加密强度为SHA-256

[html] view plaincopy
  1. <bean?id="daoAuthenticationProvider"?class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">?????????<property?name="userDetailsService"?ref="userDetailsService"?/>?????
  2. ????????<property?name="passwordEncoder">?????
  3. ????????<bean?class="org.acegisecurity.providers.encoding.ShaPasswordEncoder">?????????????????<constructor-arg?value="SHA-256"?/>?????
  4. ????????????<property?name="encodeHashAsBase64"?value="false"?/>?????????????</bean>?????
  5. ????</property>?????</bean>????


上述配置只是在Acegi通过表单提交的用户认证信息中的密码做各种加密操作。而我们存储用户密码的时候,可以通过一下程序完成用户密码操作:

[java] view plaincopy
  1. package?org.hz.test;????
  2. import?java.security.NoSuchAlgorithmException;????
  3. import?org.springframework.security.authentication.encoding.Md5PasswordEncoder;??import?org.springframework.security.authentication.encoding.ShaPasswordEncoder;??
  4. ??public?class?MD5Test?{??
  5. ????public?static?void?md5()?{?????????????Md5PasswordEncoder?md5?=?new?Md5PasswordEncoder();?????
  6. ????????//?false?表示:生成32位的Hex版,?这也是encodeHashAsBase64的,?Acegi?默认配置;?true??表示:生成24位的Base64版?????????????md5.setEncodeHashAsBase64(false);?????
  7. ????????String?pwd?=?md5.encodePassword("1234",?null);?????????????System.out.println("MD5:?"?+?pwd?+?"?len="?+?pwd.length());?????
  8. ????}??????public?static?void?sha_256()?throws?NoSuchAlgorithmException?{???????
  9. ????????ShaPasswordEncoder?sha?=?new?ShaPasswordEncoder(256);?????????????sha.setEncodeHashAsBase64(true);?????
  10. ????????String?pwd?=?sha.encodePassword("1234",?null);?????????????System.out.println("哈希算法?256:?"?+?pwd?+?"?len="?+?pwd.length());?????
  11. ????}??????????????
  12. ????????????public?static?void?sha_SHA_256()?{?????
  13. ????????ShaPasswordEncoder?sha?=?new?ShaPasswordEncoder();?????????????sha.setEncodeHashAsBase64(false);?????
  14. ????????String?pwd?=?sha.encodePassword("1234",?null);??????????????System.out.println("哈希算法?SHA-256:?"?+?pwd?+?"?len="?+?pwd.length());?????
  15. ????}??????????????
  16. ????????????public?static?void?md5_SystemWideSaltSource?()?{?????
  17. ????????Md5PasswordEncoder?md5?=?new?Md5PasswordEncoder();?????????????md5.setEncodeHashAsBase64(false);?????
  18. ?????????????????????//?使用动态加密盐的只需要在注册用户的时候将第二个参数换成用户名即可?????
  19. ????????String?pwd?=?md5.encodePassword("1234",?"acegisalt");?????????????System.out.println("MD5?SystemWideSaltSource:?"?+?pwd?+?"?len="?+?pwd.length());?????
  20. ????}?????????public?static?void?main(String[]?args)?throws?NoSuchAlgorithmException?{??
  21. ????????md5();?//?使用简单的MD5加密方式???????????????
  22. ????????sha_256();?//?使用256的哈希算法(SHA)加密??????????????????
  23. ????????sha_SHA_256();?//?使用SHA-256的哈希算法(SHA)加密??????????????????
  24. ????????md5_SystemWideSaltSource();?//?使用MD5再加全局加密盐加密的方式加密??????????}??
  25. ??????}?
    转载自:http://blog.csdn.net/hz_blog/article/details/8426625?reload

热点排行