第十三章:(2)Spring Boot 与 安全 之 SpringBoot + SpringSecurity + Thymeleaf
一、使用 Spring Security
1、引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
2、编写 Spring Security 配置类
@EnableWebSecurity
public class MySercurityConfig extends WebSecurityConfigurerAdapter {}
二、定制授权规则
定义哪些url由哪些角色可以进行访问:
@EnableWebSecurity
public class MySercurityConfig 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();
//1、/login 来到登陆页
//2、重定向到 /login?error 表示登陆失败
//3、更多详细规定
}
}
三、定制认证规则
定义用户名,密码和角色:
@EnableWebSecurity
public class MySercurityConfig 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();
//1、/login 来到登陆页
//2、重定向到 /login?error 表示登陆失败
//3、更多详细规定
}
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
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");
}
}
四、注销功能
开启自动配置的注销功能:
@EnableWebSecurity
public class MySercurityConfig 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();
//1、/login 来到登陆页
//2、重定向到 /login?error 表示登陆失败
//3、更多详细规定
//开启自动配置的注销功能
//1 访问 /logout 表示用户注销,清空 session
//2.注销成功返回 /login?logout 页面,可以使用 logoutSuccessUrl() 设置注销之后要去的url
http.logout().logoutSuccessUrl("/");
}
}
添加注销按钮
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销">
form>
五、权限控制
1、使用 Thymeleaf 与 SpringSecurity 整合依赖
<properties>
<java.version>1.8java.version>
<thymeleaf.version>3.0.9.RELEASEthymeleaf.version>
<thymeleaf-layout-dialect.version>2.3.0thymeleaf-layout-dialect.version>
<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASEthymeleaf-extras-springsecurity4.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.thymeleaf.extrasgroupId>
<artifactId>thymeleaf-extras-springsecurity4artifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
2、使用 Thymeleaf 实现权限控制
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统h1>
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录a>h2>
div>
<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>
<hr>
<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>
body>
html>
六、记住我功能
开启自动配置的"记住我"功能:
@EnableWebSecurity
public class MySercurityConfig 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();
//1、/login 来到登陆页
//2、重定向到 /login?error 表示登陆失败
//3、更多详细规定
//开启自动配置的注销功能
//1 访问 /logout 表示用户注销,清空 session
//2.注销成功返回 /login?logout 页面,可以使用 logoutSuccessUrl() 设置注销之后要去的url
http.logout().logoutSuccessUrl("/");
//开启记住我功能
http.rememberMe();
//登录成功以后,将 cookie 发送给浏览器保存,以后访问页面带上这个 cookie,只要通过检查就可以免登录
//点击注销会删除 cookie
}
}
通过 cookie 来实现的。
七、定制登录页面
自定义登录页面及 "记住我"功能:
@EnableWebSecurity
public class MySercurityConfig 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().usernameParameter("user")
.passwordParameter("pwd")
.loginPage("/userlogin");
//1、/login 来到登陆页
//2、重定向到 /login?error 表示登陆失败
//3、更多详细规定
//4. 默认 post 形式的/login代表处理登录
//5、一旦定制 loginPage,那么 loginPage 的 post 请求就是登录
//开启自动配置的注销功能
//1 访问 /logout 表示用户注销,清空 session
//2.注销成功返回 /login?logout 页面,可以使用 logoutSuccessUrl() 设置注销之后要去的url
http.logout().logoutSuccessUrl("/");
//开启记住我功能
http.rememberMe().rememberMeParameter("remember");
//登录成功以后,将 cookie 发送给浏览器保存,以后访问页面带上这个 cookie,只要通过检查就可以免登录
//点击注销会删除 cookie
}
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
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");
}
}
定义控制器方法:
/**
* 登陆页
* @return
*/
@GetMapping("/userlogin")
public String loginPage() {
return PREFIX+"login";
}
定义登录页面:
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
<h1 align="center">欢迎登陆武林秘籍管理系统h1>
<hr>
<div align="center">
<form th:action="@{/userlogin}" method="post">
用户名:<input name="user"/><br>
密码:<input name="pwd"><br/>
<input type="checkbox" name="remember">记住我<br/>
<input type="submit" value="登陆">
form>
div>
body>
html>
修改登录地址:
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录a>h2>
div>