1 准备工作
任何应用考虑到安全,绝不能明文的方式保存密码。密码应该通过哈希算法进行加密。有很多标准的算法比如SHA或者MD5,结合salt(盐)是一个不错的选择。 Spring Security 提供了BCryptPasswordEncoder类,实现Spring的PasswordEncoder接口使用BCrypt强哈希方法来加密密码。
BCrypt强哈希方法 每次加密的结果都不一样。
(1)tensquare_user工程的pom引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
(2)添加配置类 (资源/工具类中提供)
我们在添加了spring security依赖后,所有的地址都被spring security所控制了,我们目前只是需要用到BCrypt密码加密的部分,所以我们要添加一个配置类,配置为所有地址都可以匿名访问。
/**
* 安全配置类
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
(3)配置bean
@Configuration//告诉项目这是个配置文件早点加载
@EnableWebSecurity //以实现Web安全性
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
2 密码加密
@Autowired
BCryptPasswordEncoder encoder;
public void add(Admin admin) {
admin.setId(idWorker.nextId()+""); //主键值
//密码加密
String newpassword = encoder.encode(admin.getPassword());//加密后的密码
admin.setPassword(newpassword);
adminDao.save(admin);
}
3 密码比较
/**
* 根据登陆名和密码查询
* @param loginname
* @param password
* @return
*/
public Admin findByLoginnameAndPassword(String loginname, String password){
Admin admin = adminDao.findByLoginname(loginname);
if( admin!=null && encoder.matches(password,admin.getPassword())){
return admin;
}else{
return null;
}
}
encoder.matches(CharSequence rawPassword, String encodedPassword)