一、安全
Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型。他可以实现强大的web安全控制。对于安全控制,我们仅需引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理。
几个类:
WebSecurityConfigurerAdapter:自定义Security策略
AuthenticationManagerBuilder:自定义认证策略
@EnableWebSecurity:开启WebSecurity模式
• 应用程序的两个主要区域是“认证”和“授权”(或者访问控制)。这两个主要区域是 Spring Security 的两个目标。•• “认证”( Authentication ),是建立一个他声明的主体的过程(一个“主体”一般是指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。•• “授权”( Authorization )指确定一个主体是否允许在你的应用程序执行一个动作的过程。为了抵达需要授权的店,主体的身份已经有认证过程建立。•• 这个概念是通用的而不只在 Spring Security 中。
二、SpringSecurity项目入门
1、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、编写配置类
(1)参照官方文档
源码
(2)源码分析
如果没有权限,就会来到springsecurity定制的登录页面
/**
* 1、引入spring-security
* 2、编写SpringSecurity的配置类:
* @EnableWebSecurity extends WebSecurityConfigurerAdapter
* 3、控制请求的访问权限:
*
*/
3、定义规则
(1)定义授权规则
@EnableWebSecurity
public class MySecurityConfig 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");
//开启自动配置的登录功能 效果:如果没有权限,就会来到springsecurity定制的登录页面
http.formLogin();
//1./login来到登录页 -- 由springsecurity自己处理这些请求
//2.重定向到/login?error表示登录失败
//3.更多详细规则
}
(2)定义认证规则
@Override
//定义认证规则
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zs").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP2")
.and()
.withUser("lisi").password(new BCryptPasswordEncoder().encode("123")).roles("VIP3");
}
(3)开启自动配置的注销功能:访问/logout 表示用户注销,并清空session
注销成功会来到 /login?logout 页面
(4)修改默认注销的规则,希望退出之后,来到首页
//开启自动配置的注销功能:访问/logout 表示用户注销,并清空session
//注销成功会来到/login?logout 页面
http.logout().logoutSuccessUrl("/"); //注销成功后来到首页
三、定制页面显示 -- 权限控制&注销
1、结合thymelead模板引擎对springsecurity的支持
(1)引入springsecurity与thymelead的整合模块
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> <version>3.0.4.RELEASE</version> </dependency>
(2)添加名称空间
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
(3)根据角色,显示不同的页面效果
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
<h3><span sec:authentication="name"></span>,您好,您的角色有:
<span sec:authentication="principal.authorities"></span></h3>
<form th:action="@{/logout}" method="post">
<input type="submit" value="退出">
</form>
</div>
(4)根据角色的权限,显示页面效果
<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>
四、记住我&定制登录页
1、默认登录页的“记住我”功能
登录成功以后,将cookie发送给浏览器保存,以后访问带上这个cookie,只要以后通过检查就可以免登录
//3.开启自动配置的 记住我 功能
http.rememberMe();
2、定制登录页
(1)这是spring security默认的登录页面
(2)
//开启自动配置的登录功能 效果:如果没有权限,就会来到springsecurity定制的登录页面
http.formLogin().loginPage("/userlogin");
@Controller
public class KungfuController {
private final String PREFIX = "pages/";
/**
* 欢迎页
* @return
*/
@GetMapping("/")
public String index() {
return "welcome";
}
登录表单我们已经定义为 /userlogin;
所以,登录认证需要发送 /login POST 请求
登录表单,发送登录请求
<body>
<h1 align="center">欢迎登陆武林秘籍管理系统</h1>
<hr>
<div align="center">
<form action="@{/login}" method="post">
用户名:<input name=""/><br>
密码:<input name=""><br/>
<input type="submit" value="登陆">
</form>
</div>
</body>
(3)用户名和密码
默认的用户名是username,密码是password
修改用户名为user,密码为pwd
//开启自动配置的登录功能 效果:如果没有权限,就会来到springsecurity定制的登录页面
http.formLogin().usernameParameter("user").passwordParameter("pwd").loginPage("/userlogin");
3、自定义登录页的“记住我”功能
(1)Controller中的 /userlogin 登录请求,来到登录页面
/**
* 登陆页
* @return
*/
@GetMapping("/userlogin")
public String loginPage() {
return PREFIX+"login";
}
(2)
http.formLogin().loginPage("/userlogin"); //告知spring security,我们自定义的登录页发送的请求是 /userlogin
(3)同时需要更改页面的登录请求
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
(4)修改自定义登录页的url
//5.一旦定制loginPage登录页,那么 loginPage的POST请求就是登录
<h1 align="center">欢迎登陆武林秘籍管理系统</h1> <hr> <div align="center"> <form th:action="@{/userlogin}" method="post"> 用户名:<input name="user"/><br> 密码:<input name="pwd"><br/> <input type="submit" value="登陆"> </form> </div>
(5)添加自定义登录页的“记住我”功能
http.rememberMe().rememberMeParameter("rememberMe");
<div align="center">
<form th:action="@{/userlogin}" method="post">
用户名:<input name="user"/><br>
密码:<input name="pwd"><br/>
<input type="checkbox" name="rememberMe">记住我<br>
<input type="submit" value="登陆">
</form>
</div>