spring-security 自定义的密码编码器bean
2011-07-20 15:20
288 查看
上一篇也是介绍security 关于密码方面的,后来文档提过,可以通过
这个实现总体分三步:
1 、弄一个自己的加密类 这里给出java里的MD5加密算法,copy的。别的还有base64(好像jdk自己就有这个加密算法)
2. 创建 一个spring的bean 并且把MD5这个类注入,并且写出判断的logic(我是用注解方式注入的)。
3. 将这个密码编辑bean注入到你的 authentication-manager 并且指定盐值。
小弟也是刚学,不懂的可以参考,哪里不对的也希望提出意见,:)s
password-encoder的
ref属性指定一个自定义的密码编码器bean。这应该包含application context中一个bean的名字,它应该是Spring Security的
PasswordEncoder接口的一个实例。
这个实现总体分三步:
1 、弄一个自己的加密类 这里给出java里的MD5加密算法,copy的。别的还有base64(好像jdk自己就有这个加密算法)
package com.xinma.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.springframework.stereotype.Component; @Component("MD5") public class MD5 { public String str; public String md5s(String plainText) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } str = buf.toString(); //System.out.println("result: " + buf.toString());// 32位的加密 return buf.toString(); // System.out.println("result: " + buf.toString().substring(8, // 24));// 16位的加密 } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return "error"; } } }
2. 创建 一个spring的bean 并且把MD5这个类注入,并且写出判断的logic(我是用注解方式注入的)。
package com.xinma.util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.authentication.encoding.PasswordEncoder; import org.springframework.stereotype.Component; @Component("MyPasswordEncode") public class MyPasswordEncode implements PasswordEncoder { private MD5 md5; @Autowired public void setMd5(@Qualifier("MD5") MD5 md5) { this.md5 = md5; } /** * 这个方法供下面的方法isPasswordValid 调用 是用来进行盐加密的 * */ public String encodePassword(String rawPass, Object salt) { String salted = rawPass + "{" + salt.toString() + "}"; System.out.println(salted); return md5.md5s(salted); } /** * 这里便是当用户输入用户名密码后security调用的你自己的密码编辑器的方法 encPass 应该是数据库中的值 rawPass 是你输入的密码值 * salt 盐值 * */ public boolean isPasswordValid(String encPass, String rawPass, Object salt) { if (encPass.equals(encodePassword(rawPass, salt))) { return true; } return false; } }
3. 将这个密码编辑bean注入到你的 authentication-manager 并且指定盐值。
<authentication-manager> <authentication-provider user-service-ref='myUserDetailsService'> <password-encoder ref="MyPasswordEncode"><salt-source user-property="username"/></password-encoder> </authentication-provider> </authentication-manager> <b:bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> <b:property name="dataSource" ref="dataS" /> </b:bean>
小弟也是刚学,不懂的可以参考,哪里不对的也希望提出意见,:)s
相关文章推荐
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- No bean named 'springSecurityFilterChain' is defined
- 管理Spring容器中的自定义Bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- (精)Spring IOC核心源码学习III:bean标签和自定义标签实现原理
- spring-security认证过程的分析及自定义登录
- Spring Boot Security 学习笔记-根据登陆人动态配置权限-密码加密验证
- spring实战-Spring-security自定义登入登出、防csrf攻击及视图保护
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
- springBoot+springSecurity验证密码MD5加密
- Spring(10)——bean作用范围(二)——自定义scope
- 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
- Spring Bean包装类案例之自定义PropertyEditor
- springboot + security 自定义登陆校验Filter
- 【Java.Spring.Core】【IoC】自定义bean nature
- Spring管理bean的原理自定义实现
- 自定义SpringContextHolder获取bean实例
- Spring 自定义属性编辑(CustomEditorConfigurer)和类型转换器(ConversionServiceFactoryBean)一起配置问题