【SpringBoot1.x】SpringBoot1.x 安全
2024-09-07 07:36:05
SpringBoot1.x 安全
环境搭建
SpringSecurity 是针对 Spring 项目的安全框架,也是 SpringBoot 底层安全模块默认的技术选型。他可以实现强大的 web 安全控制。对于安全控制,我们仅需引入 spring-boot-starter-security
模块,进行少量的配置,即可实现强大的安全管理。
登录 注销 认证 授权 权限控制
应用程序的两个主要区域是 “认证” 和 “授权(或者访问控制)”。这两个主要区域是 SpringSecurity 的两个目标。
通过继承 WebSecurityConfigurerAdapter,可以自定义 Security 策略。使用 @EnableWebSecurity,开启 WebSecurity 模式。
“认证”(Authentication),是建立一个它声明的主体的过程(一个“主体”一般是指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。
“授权”(Authorization),指确定一个主体是否允许在你的应用程序执行一个动作的过程。为了抵达需要授权的点,主体的身份已经由认证过程建立。
Thymeleaf 提供的 SpringSecurity 标签支持,需要引入依赖:
<!-- Thymeleaf 提供的 SpringSecurity 标签支持 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
</dependency>
首页 welcome.html
<!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 here</title>
</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 th:align="center">用户:<span sec:authentication="name"></span>,角色:<span sec:authentication="principal.authorities"></span></h2>
<form th:align="center" 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>
登录页 login.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</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>
自定义安全配置类 CustomSecurityConfig.java
/**
* @Author : parzulpan
* @Time : 2021-01
* @Desc : 自定义安全配置类
*/ @EnableWebSecurity // 开启 WebSecurity 模式
public class CustomSecurityConfig 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"); // 开启自动配置的登录功能,如果没有登录或者没有权限就会来到登录页面
// 1. 访问 /login 来到登录页
// 2. 重定向到 login?error 表示登录失败
// 3. ...
// http.formLogin(); // 指定自定义的登录页
// 1. 默认 post 形式,/login 代表处理登录
// 2. 一旦定制 loginPage,那么 loginPage 的 post 请求就是登录
http.formLogin().usernameParameter("user").passwordParameter("pwd")
.loginPage("/userlogin"); // 关闭 CSRF(Cross-site request forgery)跨站请求伪造
// http.csrf().disable(); // 开启自动配置的注销功能
// 1. 访问 /logout 表示用户注销,清空 session
// 2. 注销成功,会自动重定向到 login?logout
// 3. logoutSuccessUrl 注销成功,自动回到首页
http.logout().logoutSuccessUrl("/"); // 开启记住我功能
// 1. 登录成功以后,将 cookie 发给浏览器保存,以后访问页面会带上这个 cookie,只要通过检查就可以免登录
// 2. 点击注销就会删除 cookie
http.rememberMe().rememberMeParameter("remember"); } // 定制认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("User01").password("123456").roles("VIP1", "VIP2")
.and()
.withUser("User02").password("123456").roles("VIP2", "VIP3")
.and()
.withUser("User03").password("123456").roles("VIP3", "VIP1");
}
}
书籍控制类 KungfuController.java
/**
* @Author : parzulpan
* @Time : 2021-01
* @Desc : 书籍控制类
*/ @Controller
public class KungfuController {
private final String PREFIX = "pages/"; /**
* 欢迎页
* @return
*/
@GetMapping("/")
public String index() {
return "welcome";
} /**
* 登陆页
* @return
*/
@GetMapping("/userlogin")
public String loginPage() {
return PREFIX+"login";
} /**
* level1页面映射
* @param path
* @return
*/
@GetMapping("/level1/{path}")
public String level1(@PathVariable("path")String path) {
return PREFIX+"level1/"+path;
} /**
* level2页面映射
* @param path
* @return
*/
@GetMapping("/level2/{path}")
public String level2(@PathVariable("path")String path) {
return PREFIX+"level2/"+path;
} /**
* level3页面映射
* @param path
* @return
*/
@GetMapping("/level3/{path}")
public String level3(@PathVariable("path")String path) {
return PREFIX+"level3/"+path;
}
}
总结和练习
最新文章
- [虾扯蛋] android界面框架-Window
- 浅谈Js对象的概念、创建、调用、删除、修改!
- windows10 网络热点
- top命令下的各种指标意义
- Java的自动装箱和拆箱的简单讲解
- LeetCode Shell Problems
- BZOJ1572: [Usaco2009 Open]工作安排Job
- [转载] iOS开发分辨率那点事
- net core 中间件详解及项目实战
- 6种炫酷的CSS3按钮边框动画特效
- python中导入模块的本质, 无法导入手写模块的解决办法
- uWSGI+Django (中)
- 《Self-Attention Generative Adversarial Networks》里的注意力计算
- COMCMS v0.9 版本发布,带前后端的一个响应式企业站
- Oracle数据库设置为归档模式的操作方法
- python-Django-01基础配置
- bzoj千题计划225:bzoj2143: 飞飞侠
- 180328_gVim设置tab为4个空格
- LA 3708
- HDU3727 Jewel(主席树+树状数组(或二分))
热门文章
- SpringBoot + SpringSecurity + Mybatis-Plus + JWT实现分布式系统认证和授权
- Maven笔记之核心概念及常用命令
- Docker(七): 安装Loki
- js上 四、数据类型转换
- 多任务-python实现-使用队列完成进程间的通信(2.1.8)
- .net下com调用支持x86/x64
- MySQL误删除用户怎么解决
- 在vue cli3.0 里面下载less,下载成功了还是提示没有 需要下载
- 什么是可变参数?如何创建不可变集合?Steam三类方法是什么?获取流方法特点?流中间方法特点?终结流方法特点?
- Git 是如何存储文件的