- SpringSecurity安全框架
- 概述
- 作用
- 开发示例:
- 1. 新建项目
- 2. 引入依赖
- 3. 编写`SecurityConfig`类,实现认证,授权,注销
- 4. 菜单权限
- 5. 测试
跟随该b站视频练习
代码地址
Spring Security 是针对Spring项目的安全框架,也是Spring
Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入
spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理
记住几个类(通用):
-
WebSecurityConfigurerAdapter:自定义Security策略(我们需要继承这个类)
-
AuthenticationManagerBuilder:自定义认证策略
-
@EnableWebSecurity:开启WebSecurity模式
Spring Security的两个主要目标是 “认证” 和 “授权”(访问控制)
- “认证”(Authentication)
身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。
身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。
- “授权” (Authorization)
授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。
这个概念是通用的,而不是只在Spring Security 中存在
开发示例:1. 新建项目 2. 引入依赖@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.thymeleaf
thymeleaf-spring5
org.thymeleaf.extras
thymeleaf-extras-java8time
3. 编写SecurityConfig类,实现认证,授权,注销
- http.formLogin():没有权限默认跳回登录页面
- 源码中跳转登录页的说明:
The most basic configuration defaults to automatically generating a login page at the URL "/login", redirecting to "/login?error" for authentication failure.- 源码中认证示例:
protected void configure(AuthenticationManagerBuilder auth) throws Exception { * auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); * }
- 源码中注销示例:可删除内存保存的信息
- logoutUrl:指定的注销需要访问的地址
- logoutSuccessUrl:注销成功跳转的地址
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers(" @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { //授权 @Override protected void configure(HttpSecurity http) throws Exception { //首页所有人可访问,但是功能页需要权限分类 http.authorizeRequests().antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasAnyRole("vip2","vip1") .antMatchers("/level3/**").hasAnyRole("vip3","vip2","vip1"); //没有权限默认跳回登录页面 http.formLogin(); //防止网站工具(post,get) http.csrf().disable(); //开启注销功能,注销成功返回首页 http.logout().logoutSuccessUrl("/"); //开启记住我功能,就是cookie,默认保存两周 http.rememberMe(); } //认证 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //在内存读取用户认证(多个用户用”and“连接) auth.inMemoryAuthentication() .withUser("yeyu").password("1234").roles("vip2","vip3") .and() .withUser("yuese").password("5678").roles("vip1"); } }
- 可以去数据库中认证:auth.jdbcAuthentication(),官方文档示例:
@Autowired private DataSource dataSource; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSource) .withDefaultSchema() .withUser("user").password("password").roles("USER").and() .withUser("admin").password("password").roles("USER", "ADMIN"); }
- 可以在内存中认证:auth.inMemoryAuthentication()
注意:此处springboot版本高的话会报There is no PasswordEncoder mapped for the id "null"错误,所以需要对密码加密:
//认证 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //在内存读取用户认证(多个用户用”and“连接) auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("yeyu").password(new BCryptPasswordEncoder().encode("1234")).roles("vip2","vip3") .and() .withUser("yuese").password(new BCryptPasswordEncoder().encode("5678")).roles("vip1"); }4. 菜单权限这个时候发现不同权限看到的菜单依旧一样,所以引入thymeleaf-security整合包:
- 引入依赖
org.thymeleaf.extras thymeleaf-extras-springsecurity4 3.0.4.RELEASE
- 引入命名空间
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
- index代码:
5. 测试



