【SpringSecurity】01 授权、认证、注销
【前提情要】
Security学习地址:
https://www.bilibili.com/video/BV1KE411i7bC
狂神的微信笔记:
https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247483957&idx=1&sn=fc30511490b160cd1519e7a7ee3d4ed0&chksm=ce610496f9168d8082bf6cb2e54b0b8628a1db596c1d297d06e756e621569dc3047334b3062c&mpshare=1&scene=23&srcid=0729Qvgf1Lb6AstEnrABaRiH&sharer_sharetime=1595987265426&sharer_shareid=04a2988e19defb0bbbb3d27a02ec4289#rd
【演示案例搭建】
首先是新建一个SpringBoot工程,
不要勾选任何组件,直接添加依赖即可:
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency> <dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
把静态资源文件和配置导入资源目录中:

每个Level目录中的页面都一样的【空的,啥都没有】,只是为了代表不同的可访问级别的资源
首页展示了所有可以访问的内容:

然后这是登陆页面:

编写一个路由控制器,实现页面之间的跳转访问:
package cn.echo42.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; /**
* @author DaiZhiZhou
* @file SpringSecurity
* @create 2020-07-29 9:53
*/ @Controller
public class RouterController { @RequestMapping({"/","/index"})
public String index(){
return "index";
} @RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
} @RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id){
return "views/level1/" + id;
} @RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id){
return "views/level2/" + id;
} @RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") int id){
return "views/level3/" + id;
} }
在我们启动工程之后,所有的页面毫无阻拦的就可以被所有的访问者访问到
用户的隐私数据就可以被攻击者窃取。简而言之就是安全隐患
从这里引入安全的概念,然后就是SpringSecurity这样一个解决方案:
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。
它实际上是保护基于spring的应用程序的标准。 Spring Security是一个框架,侧重于为Java应用程序提供身份验证和授权。
与所有Spring项目一样,Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求 从官网的介绍中可以知道这是一个权限框架。
想我们之前做项目是没有使用框架是怎么控制权限的?
对于权限 一般会细分为功能权限,访问权限,和菜单权限。
代码会写的非常的繁琐,冗余。 怎么解决之前写权限代码繁琐,冗余的问题,
一些主流框架就应运而生而Spring Scecurity就是其中的一种。
首先引入安全框架组件坐标:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
我们之前编写的内容不需要做出任何的改动:
【授权authorize】
编写一个SpringSecurity配置类:
package cn.echo42.config; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /**
* @author DaiZhiZhou
* @file SpringSecurity
* @create 2020-07-29 10:34
*/ @EnableWebSecurity // 注册为WebSecurity的一个Bean
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override // 注意重写的是这个HttpSecurity参数的configure方法
protected void configure(HttpSecurity http) throws Exception {
// 首页允许所有人访问,那些LEVEL页面只有对应具备的权限的用户才可以访问 http.
authorizeRequests(). // 对以下请求进行授权
antMatchers("/"). // /表示我们的首页
permitAll(). // 表示完全允许访问 // 对相应的地址进行相应的角色设置
antMatchers("/level1/**").hasRole("vip1").
antMatchers("/level2/**").hasRole("vip2").
antMatchers("/level3/**").hasRole("vip3");
}
}
这时候运行工程:
虽然首页所有人均可以访问,但是点击下面的这些LEVEL页面就会被拒绝

There was an unexpected error (type=Forbidden, status=403).
访问被禁止了,也就是我们Security起效了
但是如果访问禁止,按照业务流程,我们应该是将这个请求返回到登录页面:
所以需要进行下一步的设置:
// 没有权限,重定向到登录页面
http.formLogin();
这时候再来访问就会跳转到登录页面:
不过这登陆页面并不是我们自己那个素材的登录页,而是Security提供的

【权限认证】
重写 重载的config方法,是参数为AuthenticationManagerBuilder的方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
}
但是我们这个案例没有涉及数据库,所以使用的是从内存中获取数据用以认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().
// 认证角色 用户名称、密码、对应的多个角色 // 超级管理员就直接赋值所有角色即可
withUser("admin").password("admin").roles("vip1","vip2","vip3").
// 如果是多个角色则使用and方法加上
and().withUser("user1").password("123").roles("vip1").
and().withUser("user2").password("123").roles("vip2").
and().withUser("user3").password("123").roles("vip3");
}
然后再次访问:
当访问被拒绝重定向到登录页面
然后输入我们在Security设置的权限信息之后。。。

我这里看不出来,只有一个500,Security的要求意思是说,密码没有加密也不能访问
所以我们需要做的是就是对密码再加密即可:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).
// 认证角色 用户名称、密码、对应的多个角色 // 超级管理员就直接赋值所有角色即可
withUser("admin").password(new BCryptPasswordEncoder().encode("admin")).roles("vip1","vip2","vip3").
// 如果是多个角色则使用and方法加上
and().withUser("user1").password(new BCryptPasswordEncoder().encode("123")).roles("vip1").
and().withUser("user2").password(new BCryptPasswordEncoder().encode("123")).roles("vip2").
and().withUser("user3").password(new BCryptPasswordEncoder().encode("123")).roles("vip3");
}
再次访问:
使用user1访问level1目录的资源是可以的

但是访问level2就没办法了

【注销功能】
@Override // 注意重写的是这个HttpSecurity参数的configure方法
protected void configure(HttpSecurity http) throws Exception {
// 首页允许所有人访问,那些LEVEL页面只有对应具备的权限的用户才可以访问 http.
authorizeRequests(). // 对以下请求进行授权
antMatchers("/"). // /表示我们的首页
permitAll(). // 表示完全允许访问 // 对相应的地址进行相应的角色设置
antMatchers("/level1/**").hasRole("vip1").
antMatchers("/level2/**").hasRole("vip2").
antMatchers("/level3/**").hasRole("vip3"); // 没有权限,重定向到登录页面
http.formLogin(); // 注销,退出
http.logout();
}
在首页中插入一个注销标签:
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i> 注销
</a>
测试退出:

点击之后自动回到登录页面,并且标注了是注销回到登录页的

如果不是跳转到登录页,而是首页,则可以:
http.logout().logoutSuccessUrl("/");
即追加一个注销成功的重定向地址
【SpringSecurity】01 授权、认证、注销的更多相关文章
- SpringBoot--- 使用SpringSecurity进行授权认证
SpringBoot--- 使用SpringSecurity进行授权认证 前言 在未接触 SpringSecurity .Shiro 等安全认证框架之前,如果有页面权限需求需要满足,通常可以用拦截器, ...
- 使用SpringSecurity搭建授权认证服务(1) -- 基本demo认证原理
使用SpringSecurity搭建授权认证服务(1) -- 基本demo 登录认证是做后台开发的最基本的能力,初学就知道一个interceptor或者filter拦截所有请求,然后判断参数是否合理, ...
- IOS第三天-新浪微博 - 版本新特性,OAuth授权认证
*********版本新特性 #import "HWNewfeatureViewController.h" #import "HWTabBarViewController ...
- 七天学会ASP.NET MVC (四)——用户授权认证问题
小编应各位的要求,快马加鞭,马不停蹄的终于:七天学会 Asp.Net MVC 第四篇出炉,在第四天的学习中,我们主要了学习如何在MVC中如何实现认证授权等问题,本节主要讲了验证错误时的错误值,客户端验 ...
- MVC授权认证
处于安全性考虑,MVC可以完成授权认证,授权认证的方式如下: 1.配置Config文件,设置登录页面: <authentication mode="Forms"> &l ...
- MySQL权限授权认证详解
MySQL权限授权认证详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL权限系统介绍1>.权限系统的作用是授予来自某个主机的某个用户可以查询.插入.修改.删除 ...
- SpringSecurity个性化用户认证流程
⒈自定义登录页面 package cn.coreqi.security.config; import org.springframework.context.annotation.Bean; impo ...
- 七天学会ASP.NET MVC (四)——用户授权认证问题 【转】
http://www.cnblogs.com/powertoolsteam/p/MVC_four.html 小编应各位的要求,快马加鞭,马不停蹄的终于:七天学会 Asp.Net MVC 第四篇出炉,在 ...
- SpringSecurity之授权
SpringSecurity之授权 目录 SpringSecurity之授权 1. 写在前面的话 2. web授权 1. 建库 2. 添加查询权限的接口 3. 前端页面的编写 4. SpringSec ...
- Yii2框架RESTful API教程(二) - 格式化响应,授权认证和速率限制
之前写过一篇Yii2框架RESTful API教程(一) - 快速入门,今天接着来探究一下Yii2 RESTful的格式化响应,授权认证和速率限制三个部分 一.目录结构 先列出需要改动的文件.目录如下 ...
随机推荐
- SELinux(一) 简介
首发公号:Rand_cs 前段时间的工作遇到了一些关于 SELinux 的问题,初次接触不熟悉此概念,导致当时配置策略时束手束脚,焦头烂额,为此去系统的学习了下 SELinux 的东西.聊 SELin ...
- Adobe软件资源 PS PR AE等等
整理了一波Adobe软件,19年20年21年Mac版本的都有,关注Rand_cs即可领取
- 阻塞外挂 TCP 端口 让外挂服务器增加无用处理 反攻击 是4个IP 苹果 安卓 pc 域名
using namespace std;#include<stdlib.h>#pragma comment(lib,"WS2_32.lib") #include < ...
- jenkins结合远程仓库
既然是持续集成,对代码进行构建,我们得获取代码仓库的内容,这里选择我们搭建的gitlab服务器 1.开发工程师的机器 1. 在window上生成ssh-key $ ssh-keygen.exe -t ...
- 双等号==与equals的区别与联系
==与equals的区别与联系: 1)对于双等号==来说,比较的是值是否相等 如果双等号比较的是基本数据类型的变量,则直接比较变量的值是否相等, 如果双等号比较的是引用类型的变量,则比较的是所指向对象 ...
- CNN --入门MNIST识别
Smiling & Weeping ---- 下次你撑伞低头看水洼, 就会想起我说雨是神的烟花. 简介:主要是看刘二大人的视频讲解:https://www.bilibili.com/video ...
- Oh-My-Zsh 提示符只显示当前路径,不需要修改主题文件
我真是服了.就这么一个简单的小问题我在网上找了一个多小时,一大堆 CSDN 文章都是抄 同一篇博客 的教程,所有的博客都要我去把 ~/.oh-my-zsh/themes/*.zsh-theme 文件里 ...
- shell脚本获取函数返回值
方式1 原理:return返回的值可以通过$?得到. 缺点:return只能返回整数 #!/bin/sh function test() { return 100 } test echo $? 方式2 ...
- Linux中的inode
inode是什么 一个文件由两部分组成:元信息和数据. 在Linux中,inode包含文件的元信息,具体来说有以下内容: Size 文件的字节数 Uid 文件拥有者的User ID Gid 文件的Gr ...
- 构建高可用性、高性能和可扩展的Zabbix Server架构
简介 本教程讲解了一下如何设计构建一个高性能.高可靠.高扩展的Zabbix 监控集群. 架构图 架构图PDF下载: https://songxwn.com/file/Zabbix_HA.pdf Pig ...