1 BCrypt密码加密
Spring Security提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强哈希方法来加密密码。
1.1 引入SpringSecurity依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐security</artifactId>
</dependency>
1.2编写配置类
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
1.3配置bean
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
1.4进行加密解密在service实现类里面写
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
//加密
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
//解密
bCryptPasswordEncoder.matches(password//传过来的密码,user.getPassword()//数据库查到的密码)