第一步:导入依赖
org.springframework.boot spring‐boot‐starter‐security第二步:添加配置类
导入依赖后会控制所有的地址,可以进行访问鉴权,不过这里我们只是来加密的所以,让所有的接口都可以匿名访问
/**
- 安全配置类
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
第三步:配置bean
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
第四步使用
4.1注入:
@Autowired
BCryptPasswordEncoder encode;
加密:String newpassword = encoder.encode(admin.getPassword());
效验:encoder.matches(password,admin.getPassword());
password是从数据库里提取出来的加密后的密码,后者是从前端传来用户的输入
结果是true/false