深圳最好的活动策划公司/seo网站平台
Spring Boot与安全视频
Spring Security, shiro等安全框架。主要功能是”认证“和”授权“,或者说是访问控制。
认证
(Authentication)是建立在一个声明主体的过程(一个主体一般指用户,设备或一些可以在你的应用程序中执行动作的其他系统)。
授权
(Authorization)指确定一个主体是否允许在你的应用程序执行一个动作的过程。 为了抵达需要授权的店, 主体的身份已经有认证过程建立。
这里我们使用Spring Security练习。
1. pom.xml
创建springboot应用,导入spring-boot-starter-security
等相关依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.crysw.security</groupId><artifactId>springboot05-security</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot05-security</name><description>springboot05-security</description><properties><java.version>1.8</java.version><!--指定thymeleaf相关依赖的版本--><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version><thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version></properties><dependencies><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><!--可以在html中引用thymeleaf来获取授权和认证的相关信息--><dependency><groupId>org.thymeleaf.extras</groupId><artifactId>thymeleaf-extras-springsecurity4</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2. html页面
准备静态资源(html测试页面)
2.1 welcome.html
/
请求转发到首页welcome.html
<!DOCTYPE html>
<html lang="en" 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>Title</title>
</head>
<body>
<h1 th:align="center">欢迎光临武林秘籍管理系统</h1><!--没有认证-->
<div sec:authorize="!isAuthenticated()"><!--/login的get请求是security默认跳转登录页面的处理--><!--<h2 align="center">游客您好,如果想查看武林秘籍,<a th:href="@{/login}">请登录</a></h2>--><!--/userlogin的get请求是自定义跳转自己登录认证页面的处理--><h2 th:align="center">游客您好,如果想查看武林秘籍,<a th:href="@{/userlogin}">请登录</a></h2>
</div><!--只有登录认证了才展示注销按钮-->
<div sec:authorize="isAuthenticated()"><h2><span sec:authentication="name"></span>, 您好,您的角色有:<spansec:authentication="principal.authorities"></span></h2><form th:action="@{/logout}" method="post"><input type="submit" value="注销"/></form>
</div><hr>
<!--授权VIP1的模块展示-->
<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>
<!--授权VIP2的模块展示-->
<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><!--授权VIP3的模块展示-->
<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>
2.2 login.html
请求/userlogin
转发到自定义的登录页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta http-equiv="Content-Type" content="text/html" charset="UTF-8"><title>Title</title>
</head>
<body>
<h1 th:align="center">欢迎来到武林秘籍管理系统</h1>
<hr/>
<div th:align="center"><!--默认post形式的/login代表处理登录提交--><!--如果定制loginPage,那么loginPage的post请求就是登录提交--><form th:action="@{/userlogin}" method="post">用户名: <input type="text" name="uname"/><br/>密码:<input type="password" name="pwd"/><br/><input type="checkbox" name="remember" id=""/> remember me<br/><input type="submit" value="登录"/></form>
</div>
</body>
</html>
2.3 level1模块页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta http-equiv="Content-Type" content="text/html" charset="UTF-8"><title>Title</title>
</head>
<body>
<a th:href="@{/}">返回</a>
<h1>罗汉拳</h1>
<p>罗汉拳站当秧,打起来不要慌</p>
</body>
</html>
其他模块页面一样,简单改一下内容即可。
3. security配置类
WebSecurityConfigurerAdapter为创建WebSecurityConfigurer实例提供了一个方便的基类。允许通过重写方法进行定制实现,自定义授权规则和认证规则。
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {/*** 定制请求的授权规则** @param http the {@link HttpSecurity} to modify* @throws Exception*/@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/").permitAll()// VIP1角色的用户才能访问level1的页面,其他同理.antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");// 开启自动配置的登录功能
// http.formLogin();http.formLogin().usernameParameter("uname").passwordParameter("pwd").loginPage("/userlogin").loginProcessingUrl("/userlogin");// 1. 如果没有访问权限,转发到/login请求来到登录页;// 2. 重定向到/login?error表示登录失败。 更多详细规定// 3. 默认post形式的/login代表处理登录提交// 4.如果定制loginPage,那么loginPage的post请求就是登录提交// 开启自动配置的注销功能, 访问/logout表示用户注销,清空session; 注销成功后来到首页;http.logout().logoutSuccessUrl("/");// 开启记住我的功能, 将cookie发给浏览器保存,以后登录带上这个cookie,只要通过服务器端的验证就可以免登录// 如果点击”注销“,也会删除这个cookie
// http.rememberMe();http.rememberMe().rememberMeParameter("remember");}/*** 定制认证规则** @param auth the {@link AuthenticationManagerBuilder} to use* @throws Exception*/@Overrideprotected 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");}
}
启动应用后,访问主页,需要先登录认证。可以看到张三有访问VIP1, VIP2权限的区域展示。
登录认证成功后,跳转到欢迎主页。
相关文章:

chapter14:springboot与安全
Spring Boot与安全视频 Spring Security, shiro等安全框架。主要功能是”认证“和”授权“,或者说是访问控制。 认证(Authentication)是建立在一个声明主体的过程(一个主体一般指用户,设备或一些可以在你的应用程序中…...

Linux初识网络基础
目录 网络发展 认识“协议 ” 网络协议 OSI七层模型: TCP/IP五层(或四层)模型 网络传输基本流程 网络传输流程图: 数据包封装和封用 网络中的地址 认识IP地址: 认识MAC地址: 网络发展 1.独立…...

vue3+ts 动态导入多文件组件
1、在components文件夹中新建index.ts文件(components文件夹下为创建的组件) // index.ts const modules import.meta.globEager("./*.vue"); //参数为组件路径 let componentsOpts {}const getCaption (obj, str, z: boolean) > {let…...

补充122836356
ul {margin-bottom: 20px;& > li {margin-bottom: 0;} }等效于ul {margin-bottom: 20px; }ul > li {margin-bottom: 0; }CSS中的&代表的什么 如源码: ul{ margin-bottom: 20px; & >li { margin-bottom: 0; } } & 表示嵌套的上一级 这是s…...

记录 pl-table 表格头部文字抖动的问题
本文记录一个实际开发中 pl-table 的问题,项目比较老,vue还是2.x版本。pl-table 是基于 el-table 改造过来的表格展示组件,已经停止更新。 问题描述 当 data 内数据动态改变时,pl-table 的表头部分,列的文字会左右抖动…...

Vite 创建 Vue项目之后,eslint 错误提示的处理
使用 npm create vuelatest创建 vue 项目(TS)之后,出现了一些 eslint 错误提示,显然,不是代码真实的错误,而是提示搞错了。 vuejs/create-vue: 🛠️ The recommended way to start a Vite-pow…...

FFmpeg 硬编码VideoToolBox流程
介绍 FFmpeg已经提供对 VideoToolBox 的编解码支持;主要涉及到的文件有videotoolbox.c、videotoolbox.h、videotoolboxenc.c、ffmepg_videotoolbox.c。在编译 FFmpeg 源码时,想要支持VideoToolBox,在 configure 时,需要–enable-…...

恒盛策略:内盘是买入还是卖出?
内盘,又称成交明细,是指交易所实时发布的每一笔成交价格、交易时刻、成交量等具体数据。关于股民来说,每天重视内盘数据已成为必修课。可是,当看到内盘数据时,很多人都会困惑,到底内盘是买入还是卖出呢&…...

安装Lombok--Lombok的常用注解说明及使用方法
😀前言 本篇博文是关于Lombok的基本介绍和基本使用,希望能够帮助到您😊 🏠个人主页:晨犀主页 🧑个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满…...

无涯教程-Perl - endpwent函数
描述 此功能告诉系统您不再希望使用getpwent从密码文件读取条目。在Windows下,使用Win32API::Net函数从域服务器获取信息。 语法 以下是此函数的简单语法- endpwent返回值 此函数不返回任何值。 例 以下是显示其基本用法的示例代码- #!/usr/bin/perlwhile(($name, $pas…...

vue项目在body设置公共的背景前提下,区分首页背景图和其他页面背景图
1.需求:在vue项目已设置统一的body背景图的前提,单独给首页换一个背景图,然后其他页面背景图不变的临时需求 实现思路1:在首页home.vue中 在公共的style.css文件中写上两个背景样式(写在公共样式中是因为style.css比组件内部的先加载,避免页面出现后背景空白的问题) …...

测试人员该怎样写软件缺陷报告?
软件测试过程中,每个公司都制订了软件的缺陷处理流程,每个公司的软件缺陷处理流程不尽相同,但是它们遵循的最基本流程是一样的,都要经过提交、分配、确认、处理、复测、关闭等环节,如图1所示。 缺陷处理流程 关于图1所…...

【大数据】Flink 详解(二):核心篇 Ⅱ
Flink 详解(二):核心篇 Ⅱ 22、刚才提到 State,那你简单说一下什么是 State。 在 Flink 中,状态 被称作 state,是用来保存中间的计算结果或者缓存数据。根据状态是否需要保存中间结果,分为 无状…...

一孩半政策
一) 一孩半,又称独女户二胎,即中国大陆部分农村的一项计划生育政策,第一胎是女孩的夫妻可生育第二个子女。试问这个政策会不会影响男女平衡。 二)如果生女孩一直生,直到生男孩停止,试问会不会…...

如何在 Spring Boot 中集成日志框架 SLF4J、Log4j
文章目录 具体步骤附录 笔者的操作环境: Spring Cloud Alibaba:2022.0.0.0-RC2 Spring Cloud:2022.0.0 Spring Boot:3.0.2 Nacos 2.2.3 Maven 3.8.3 JDK 17.0.7 IntelliJ IDEA 2022.3.1 (Ultimate Edition) 具体步骤 因为 …...

如何在Linux布置nginx(附带Nginx基本操作步骤)
文章目录 前言一、下载环境依赖二、下载nginx安装包三、具体操作流程总结 前言 提示:下述操作步骤适合内网服务器、局域网服务器和公网服务器。 不足之处欢迎交流指正,不喜勿喷。 一、下载环境依赖 yum -y install gcc zlib zlib-devel pcre-devel ope…...

Xcode升级导致关联库报错
想办法找到对应的库 然后到 Build Phases -- LinkBinary With Libraries中点击,选择对应的framework即可,就像我工程的报错 Undefined symbol: _OBJC_CLASS_$_ADClient _OBJC_CLASS_$_ASIdentifierManager 缺失的库是AdSupport.framework 添加后再次编…...

利用docker run --rm 命令实现使用宿主机中没有的命令
利用docker run --rm 命令实现使用宿主机中没有的命令 使用容器中的jar命令解压jar包,并将解压内容输出到挂载在宿主机中的目录里使用宿主机中没有的nmap命令来通过端口找IP 使用容器中的jar命令解压jar包,并将解压内容输出到挂载在宿主机中的目录里 do…...

中级课程——XSS
文章目录 介绍挖掘思路分类反射型存储型dom类型 介绍 挖掘思路 注入点:各种输入框 测试代码(poc):js语句 分类 反射型 存储型 dom类型...

win10+Vmware+ubuntu18 mosquitto调试记录
记录一下在建立mqtt调试环境上遇到的问题及对策。 我的PC环境为,win10为办公环境,Vmware虚拟机安装ubuntu18,虚拟机主要用来进行代码编译,建立mosquitto server测试环境。 1. ubuntu 安装mosquitto 安装mosquitto网上很多教程&…...

Java EE 突击 9 - Spring Boot 日志文件
Spring Boot 日志文件 学习目标一 . 日志有什么用1.1 日志格式说明 二 . 自定义日志打印2.1 得到日志对象2.2 使用日志对象提供的方法 , 输出自定义的日志内容2.3 日志的级别 三 . 日志持久化3.1 在配置文件里面设置日志名称3.2 设置日志的保存目录 四 . 日志级别的设置五 . 简…...

篇十六:命令模式:封装请求
篇十六:"命令模式:封装请求" 开始本篇文章之前先推荐一个好用的学习工具,AIRIght,借助于AI助手工具,学习事半功倍。欢迎访问:http://airight.fun/。 另外有2本不错的关于设计模式的资料&#x…...

Android 系统框架
启动流程 init 进程启动过程 Android系统启动流程 Zygote启动流程及源码分析 APP启动流程 init进程是Android用户空间第一个进程,主要做以下3件事情: 创建和挂载启动所需的文件目录。初始化和启动关键服务,守护关键服务。解析init.rc配…...

【Hystrix技术指南】(3)超时机制的原理和实现
[每日一句] 也许你度过了很糟糕的一天,但这并不代表你会因此度过糟糕的一生。 [背景介绍] 分布式系统的规模和复杂度不断增加,随着而来的是对分布式系统可用性的要求越来越高。在各种高可用设计模式中,【熔断、隔离、降级、限流】是经常被使…...

MySQL: Failed to Connect to MySQL at XXXX:3306 with user root
客户端连接MySQL服务器,报错: 解决方案: 没有让root用户远程登录,需要设置; 进入MySQL服务器,修改一下 # mysql -h localhost -uroot -P3306 -p12345678 mysql: [Warning] Using a password on the comm…...

《大型网站技术架构设计》第二篇 架构-性能
不同视角下的网站性能 1、用户 从用户角度,网站性能就是用户在浏览器上直观感受到的网站响应速度快还是慢。用户感受到的时间。 2、开发人员 开发人员关注的主要是应用程序本身及其相关子系统的性能,包括响应延迟、系统吞吐量、并发处理能力、系统稳定…...

谷歌推出AI模型机器人RT2 将文本和图像输出为机器人动作
去年年底,ChatGPT火遍全球,全世界都见识了大语言模型的强大力量。人们对大模型不再陌生,开始使用基于大模型的应用绘画、作图、搜索资料、设计剧情等,而妙用不止于此。谷歌推出了Robotics Transformer 2(RT2),这是一个…...

常见的服务器安全管理漏洞
常见的服务器安全管理漏洞 企业信息化技术的应用,以不可逆转。随着文件服务器、ERP管理软件等等在企业中生根发芽,应用服务器也逐渐在企业中普及起来。以前在企业中有一台应用服务器已经是了不起的事情,现在有两台、三台的,也不为…...

JavaScript高级:探索作用域链的神秘面纱
在 JavaScript 的编程世界中,作用域是一种控制变量可访问性和生命周期的机制。而作用域链则是闭包的关键所在,它使得函数在创建时捕获并保存了外部作用域的变量,为 JavaScript 增添了更多的魔力。本文将深入探讨作用域链的概念和作用…...

mysql数据库如何转移到oracle
mysql数据库转移到oracle 在研发过程中,可能会用到将表数据库中的表结构及数据迁移到另外一种数据库中, 比如说从mysql中迁移到oracle中, 常用的方法有好些,如下 1、使用powerdesigner,先连接mysql然后生成mysql的p…...