1、引入SpringSecurity依赖
<!--security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、编写配置类
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
3、配置bean,放到 @SpringBootApplication 的文件中
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
4、实现加密与解密
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
//加密
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
//解密
bCryptPasswordEncoder.matches(password//传过来的密码,user.getPassword()//数据库查到的密码)