以前咱們都是使用MD5
Md5PasswordEncoder
或者SHA
ShaPasswordEncoder
的哈希算法進行密碼加密,在spring security中依然使用只要指定使用自定義加密算法就行,如今推薦spring使用的BCrypt
BCryptPasswordEncoder
,一種基於隨機生成salt的根據強大的哈希加密算法。
首先咱們使用spring提供的加密方法對密碼 123456 進行加密:
一、使用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
二、使用
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,不然字符串會被截斷。
1、使用MD5加密算法:
spring security已經廢棄了
org
.
springframework
.
security
.
authentication
.
encoding.
PasswordEncoder接口,推薦使用
org
.
springframework
.
security
.
crypto
.
password.
PasswordEncoder接口
這裏須要自定義。
一、創建自定義密碼加密實現類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");
}
}
二、在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加密。
2、使用BCrypt加密算法:
一、只須要在
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 = ?");
}