什么是Spring Security
Spring Security是一个功能强大且高度可定制的身份验证和访问控制的框架。
Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型。它可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security场景启动器,进行少量的配置,即可实现强大的安全管理。
如何使用Spring Security
核心涉及到的类以及注解:
- WebSecurityConfigurerAdapter:自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- HttpSecurity:请求授权策略
- @EnableWebSecurity:开启WebSecurity模式
Spring Security的两个主要目标是“认证”和“授权(访问控制)”
- 认证(Authentication)
- 授权(Authorization)
注意这两个概念是通用的,而不是只在Spring Security中存在
使用步骤:
1. 引入Spring Security场景启动器
org.springframework.boot
spring-boot-starter-security
2. 编写Spring Security配置类,这个类需要继承WebSecurityConfigurerAdapter,并且加上@EnableWebSecurity注解开启WebSecurity模式
注意,整个请求授权的过程是以链式编程的方式来进行配置的。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}
3. 配置请求授权规则。重写configure方法,注意,该方法有多个重载方法,选择参数为HttpSecurity的即可
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只有对应有权限的人才能访问
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//开启记住我功能。自定义接收前端的参数
http.rememberMe().rememberMeParameter("remember");
}
其中http.authorizeRequests()表示请求授权
antMatchers()方法是选择请求地址
后面的permitAll()表示所有人都可以访问
hasRole()方法是配置访问该请求路径所需要的权限
此时,上面配置的访问路径都设置上了权限校验功能
此方法中还可以配置登录页功能:
我们可以开启登录功能,当没有登录时,访问需要进行权限校验的地址,就会跳转到登录页。还可以自定义登录页
http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");
其中formLogin() 开启登录页功能
后面两项可以省略,即使用底层自带的登录页,我们也可以自定义登录页:
- loginPage("/toLogin") 表示跳转到登录页的请求地址
- loginProcessingUrl("/login") 即为登录页提交的地址,此地址无需自己手动实现。
配置注销功能:
http.csrf().disable();//关闭csrf功能
http.logout().logoutSuccessUrl("/");
注销功能开启后,Security底层帮我们实现了注销功能,默认访问/logout即可。
其中logoutSuccessUrl("/")表示注销成功后跳转的请求地址
开启记住我功能:
//开启记住我功能。自定义接收前端的参数
http.rememberMe().rememberMeParameter("remember");
rememberMeParameter("")可以接收自定义参数
开启记住我功能后,用户选中并完成登录,就会在浏览器中存放一个保存了相关信息的cookie,这个cookie默认保留14天。
4.配置认证用户规则,重写方法名同样为configure,请求参数为AuthenticationManagerBuilder。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhiqian").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2")
.and().withUser("root").password(new BCryptPasswordEncoder().encode("1234")).roles("vip1","vip2","vip3")
.and().withUser("guest").password(new BCryptPasswordEncoder().encode("1234")).roles("vip1");
}
inMemoryAuthentication()表明用户从内存中读。这些数据正常情况应该在数据库中获取。这里因为没有连接数据库,所以在内存中自定义了一些用户来完成测试。
从数据库中获取用户使用jdbcAuthentication()方法即可
其中passwordEncoder(new BCryptPasswordEncoder()) 表示对密码的硬编码,用于对明文密码的加密。注意在SpringBoot2.2及以上版本,需要对密码进行加密。
其中new BCryptPasswordEncoder()是加密的一种方式,加密的方式有很多种。spring security 官方推荐的是BCrypt。
其中roles("")表示为用户设置的权限。
以上两个方法重写完成后,Spring Security的核心功能就已经配置好了。
完整的配置类代码
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//请求授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只有对应有权限的人才能访问
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限会跳到登录页面,开启登录功能
//登录页设置
http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");
//注销,开启注销功能,并跳到首页
http.csrf().disable();//关闭csrf功能
http.logout().logoutSuccessUrl("/");
//开启记住我功能。自定义接收前端的参数
http.rememberMe().rememberMeParameter("remember");
}
//认证用户规则
//密码编码:PasswordEncoder,SpringBoot2.2及以上,需要对密码进行加密
//在Spring Security 5.0+ 新增了很多的加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication()表明用户从内存中读。这些数据正常情况应该在数据库中获取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhiqian").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2")
.and().withUser("root").password(new BCryptPasswordEncoder().encode("1234")).roles("vip1","vip2","vip3")
.and().withUser("guest").password(new BCryptPasswordEncoder().encode("1234")).roles("vip1");
}
}