spring security使用哈希加密的密码

之前我们都是使用MD5  Md5PasswordEncoder   或者SHA  ShaPasswordEncoder   的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密算法就行,现在推荐spring使用的BCrypt  BCryptPasswordEncoder ,一种基于随机生成salt的根据强大的哈希加密算法。
首先我们使用spring提供的加密方法对密码 123456 进行加密:
1、使用MD5加密:
复制代码
package com.petter.util;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
/**
*
@author hongxf
*
@since 2017-04-11 10:52
*/
public class MD5EncoderGenerator {
public static void main(String[] args) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
System.out.println(encoder.encodePassword(
"123456", "hongxf"));
}
}
复制代码
修改数据库中的用户hxf密码为 7cbdf569746dd62484eb25a55b7df2dc
2、使用 BCrypt加密:
复制代码
package com.petter.util;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
*
@author hongxf
*
@since 2017-04-10 10:01
*/
public class PasswordEncoderGenerator {
public static void main(String[] args) {
String password
= "123456";
BCryptPasswordEncoder passwordEncoder
= new BCryptPasswordEncoder();
String hashedPassword
= passwordEncoder.encode(password);
System.out.println(hashedPassword);
}
}
复制代码
修改数据库中的用户hxf密码为 $2a$10$f0DEGrkIpYyzcFrf/fTMSOAKl1Y/XHpKaijWdfiWnOOzGTEs8diLi
这里需要注意,保证数据库密码字段的长度为60或者大于60,否则字符串会被截断。
 
一、使用MD5加密算法:
spring security已经废弃了 org . springframework . security . authentication . encoding. PasswordEncoder接口,推荐使用 org . springframework . security . crypto . password. PasswordEncoder接口
这里需要自定义。
1、建立自定义密码加密实现类CustomPasswordEncoder
复制代码
package com.petter.config;import org.springframework.security.authentication.encoding.Md5PasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;/** * @author hongxf * @since 2017-04-11 10:39 */public class CustomPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
return encoder.encodePassword(rawPassword.toString(), "hongxf");
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
return encoder.isPasswordValid(encodedPassword, rawPassword.toString(), "hongxf");
}
}
复制代码
2、在SecurityConfig中进行配置
复制代码
@Bean    
public PasswordEncoder passwordEncoder(){
return new CustomPasswordEncoder();
//return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
复制代码
直接把自定义的类设置即可。
同样适用于SHA加密。
二、使用BCrypt加密算法:
1、只需要在 SecurityConfig中进行配置
复制代码
@Bean    
public PasswordEncoder passwordEncoder(){
//return new CustomPasswordEncoder();
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
复制代码
 
PS:如果使用的是 jdbcAuthentication,安装如下配置即可
复制代码
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder(); }
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
.usersByUsernameQuery(
"select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username = ?");
}
复制代码
启动测试即可
 
 
 
之前我们都是使用MD5  Md5PasswordEncoder   或者SHA  ShaPasswordEncoder   的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密算法就行,现在推荐spring使用的BCrypt  BCryptPasswordEncoder ,一种基于随机生成salt的根据强大的哈希加密算法。
首先我们使用spring提供的加密方法对密码 123456 进行加密:
1、使用MD5加密:
复制代码
package com.petter.util;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
/**
*
@author hongxf
*
@since 2017-04-11 10:52
*/
public class MD5EncoderGenerator {
public static void main(String[] args) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
System.out.println(encoder.encodePassword(
"123456", "hongxf"));
}
}
复制代码
修改数据库中的用户hxf密码为 7cbdf569746dd62484eb25a55b7df2dc
2、使用 BCrypt加密:
复制代码
package com.petter.util;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
*
@author hongxf
*
@since 2017-04-10 10:01
*/
public class PasswordEncoderGenerator {
public static void main(String[] args) {
String password
= "123456";
BCryptPasswordEncoder passwordEncoder
= new BCryptPasswordEncoder();
String hashedPassword
= passwordEncoder.encode(password);
System.out.println(hashedPassword);
}
}
复制代码
修改数据库中的用户hxf密码为 $2a$10$f0DEGrkIpYyzcFrf/fTMSOAKl1Y/XHpKaijWdfiWnOOzGTEs8diLi
这里需要注意,保证数据库密码字段的长度为60或者大于60,否则字符串会被截断。
 
一、使用MD5加密算法:
spring security已经废弃了 org . springframework . security . authentication . encoding. PasswordEncoder接口,推荐使用 org . springframework . security . crypto . password. PasswordEncoder接口
这里需要自定义。
1、建立自定义密码加密实现类CustomPasswordEncoder
复制代码
package com.petter.config;import org.springframework.security.authentication.encoding.Md5PasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;/** * @author hongxf * @since 2017-04-11 10:39 */public class CustomPasswordEncoder implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
return encoder.encodePassword(rawPassword.toString(), "hongxf");
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
Md5PasswordEncoder encoder
= new Md5PasswordEncoder();
return encoder.isPasswordValid(encodedPassword, rawPassword.toString(), "hongxf");
}
}
复制代码
2、在SecurityConfig中进行配置
复制代码
@Bean    
public PasswordEncoder passwordEncoder(){
return new CustomPasswordEncoder();
//return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
复制代码
直接把自定义的类设置即可。
同样适用于SHA加密。
二、使用BCrypt加密算法:
1、只需要在 SecurityConfig中进行配置
复制代码
@Bean    
public PasswordEncoder passwordEncoder(){
//return new CustomPasswordEncoder();
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
authenticationProvider.setPasswordEncoder(passwordEncoder());
auth.authenticationProvider(authenticationProvider);
}
复制代码
 
PS:如果使用的是 jdbcAuthentication,安装如下配置即可
复制代码
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder(); }
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder())
.usersByUsernameQuery(
"select username,password, enabled from users where username = ?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username = ?");
}
复制代码
启动测试即可
 
 
 

猜你喜欢

转载自www.cnblogs.com/jpfss/p/9046257.html
今日推荐