一、登录&认证&授权&权限控制&注销&记住我&定制登录页
安全部分内容主要以演示功能为主,实际项目中用户信息和权限都存在数据库中。
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2.编写SpringSecurity的配置类
- 控制请求的访问权限
configure(HttpSecurity http);
- 定义认证规则
configure(AuthenticationManagerBuilder auth);
- 开启自动配置的登陆功能
configure(HttpSecurity http);
- 默认post形式的
/login
代表处理登陆,get形式的/login
代表进入登录页,/login?error
就是登录失败,/login?logout
代表注销 - 一但定制loginPage参数,那么 loginPage的post请求就是登陆请求,get请求就是进入登录页,定制的路径后加
?error
就是登录失败,定制的路径后加?logout
就是注销
- 默认post形式的
- 注销
http.logout();
- 访问 /logout 表示用户注销,清空session
- 注销成功会返回 /login?logout 页面;
- 记住我
http.rememberMe();
- 登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录
- 点击注销会删除cookie
//此注解中包含@Configuration
@EnableWebSecurity//开启安全模块
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
//定制请求的授权规则
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("VIP1")
.antMatchers("/level2/**").hasRole("VIP2")
.antMatchers("/level3/**").hasRole("VIP3");
//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面
//可以指定user和pwd为页面传的用户名密码的参数名,默认是username和password
//loginPage--自定义登录请求
http.formLogin().usernameParameter("user").passwordParameter("pwd")
.loginPage("/userlogin");
//开启自动配置的注销功能。
http.logout().logoutSuccessUrl("/");//注销成功以后来到首页/
//开启记住我功能
//可以指定页面发送的参数name,默认为remeberme
http.rememberMe().rememberMeParameter("remeber");
}
//定义认证规则(用户名、密码、角色)
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//super.configure(auth);
auth.inMemoryAuthentication()
.withUser("zhangsan").password("123456").roles("VIP1","VIP2")
.and()
.withUser("lisi").password("123456").roles("VIP2","VIP3")
.and()
.withUser("wangwu").password("123456").roles("VIP1","VIP3");
}
}
3.页面获取登录信息
引入依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
名称空间thymeleaf-extras-springsecurity4
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
标签sec:authentication
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>,您好,您的角色有:
<span sec:authentication="principal.authorities"></span></h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销"/>
</form>
</div>
分权限显示hasRole()
<div sec:authorize="hasRole('VIP1')">
<h3>普通武功秘籍</h3>
<ul>
<li><a th:href="@{/level1/1}">罗汉拳</a></li>
<li><a th:href="@{/level1/2}">武当长拳</a></li>
<li><a th:href="@{/level1/3}">全真剑法</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP2')">
<h3>高级武功秘籍</h3>
<ul>
<li><a th:href="@{/level2/1}">太极拳</a></li>
<li><a th:href="@{/level2/2}">七伤拳</a></li>
<li><a th:href="@{/level2/3}">梯云纵</a></li>
</ul>
</div>
<div sec:authorize="hasRole('VIP3')">
<h3>绝世武功秘籍</h3>
<ul>
<li><a th:href="@{/level3/1}">葵花宝典</a></li>
<li><a th:href="@{/level3/2}">龟派气功</a></li>
<li><a th:href="@{/level3/3}">独孤九剑</a></li>
</ul>
</div>