学习目标:
全面掌握Spring Security各种细节
学习内容:
- Spring Security的默认登录页面
- 如何配置Spring Security
- sec标签的使用
文章目录
前言
新手第一篇文章
这是基于狂神的SpringSecurity讲解后自己整理的笔记
主要用于自己巩固学过的知识
一、Spring Security的默认登录页面
Spring Security框架一般都是要和Thymeleaf一起整合使用的
当我们在pom里面导入了它们的依赖时
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
我们在访问前端页面时会有一个默认的登录页面用来拦截访问路径
用户名是user,密码是控制台上的密码
二、如何配置Spring Security
1.创建一个类
使用@EnableWebSecurity
开启WebSecurity模式
然后继承WebSecurityConfigurerAdapter
自定义Security策略
2.授权(Authorization)
- 设置哪些人可以访问哪些页面
- 自定义登录页面
- 开启记住我功能
设置谁可以访问
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人都可以访问,功能页只能对应的有权限的人才能访问,类似于拦截器
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//login页面和logout页面都是springSecurity自带的
//没有权限自动进入到登录界面,这个登录界面Login不是自己写的页面,是系统后台的页面
//http.formLogin();
http.formLogin()
.loginPage("/toLogin")
.usernameParameter("user")
.passwordParameter("pwd");
//开启注销功能(要在前端写跳转到这个注销地址/logout的标签)
//注销成功默认跳到登录login页面
//http.logout();
http.csrf().disable(); //用来解决注销错误的Bug
http.logout().logoutSuccessUrl("/"); //设置注销成功页面为首页
//开启记住我功能,自定义从前端接收参数
http.rememberMe().rememberMeParameter("remember");
}
3.认证(Authentication)
- 设置用户
- 设置权限
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这是在内存中定义的数据,也可以在数据库中去拿
//(type=Internal Server Error, status=500).密码未加密错误
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("Coder").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("12345")).roles("vip1", "vip2", "vip3");
}
三、sec标签的使用
- sec:authorize 类似于if判断,true现实,否则不显示
- isAuthenticated() 是否认证了
- sec:authentication 返回一些
AuthenticationManagerBuilder auth
的属性,比如用户名和权限 - hasRole("") 是否拥有该权限
name和authorities都是Principal
接口实现类里的属性
分别表示用户名和用户的权限,可以直接在配置中的认证(Authentication)中拿到
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item">
用户名: <span sec:authentication="name"></span>
角色: <span sec:authentication="principal.authorities"></span>
</a>
<a class="item" th:href="@{/logout}">
<i class="iconfont icon-log-out"></i> 注销
</a>
</div>
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
</div>
</div>
</div>
</div>