Springboot整合SpringSecurity--对静态文件进行权限管理
文章目录
一、要求
index.html 可以被所有用户访问
1.html只能被VIP1访问
2.html只能被VIP2访问
3.html只能被VIP3访问
没有权限跳到登录页
二、依赖管理
- springboot 2.2.5
- spring security 5.2.4
pom.xml需要的依赖如下:
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
三、配置config文件
我们先看看官网的教程:spring security 5.2.4 配置教程
模拟官网教程配置如下:
package com.xsy.worker_manager.config;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity // 开启web security服务
public class SecurityConfig 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");
//没有权限跳到登录页 /login,登陆后跳到主页
http.formLogin();
//开启注销功能 /logout, 注销后跳到首页
http.logout().logoutSuccessUrl("/index.html");
//开启记住我功能
http.rememberMe();
}
//授权
//密码编码
//spring security 5.0+要密码加密
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//将用户信息放到内存里
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).
withUser("xsy").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3").
and().withUser("csy").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2").
and().withUser("sy").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}
四、扩展
如果需要在后台接口设置权限,则需要在config配置文件上使用如下注解:
@EnableGlobalMethodSecurity(prePostEnabled = true)
然后直接在contoller的接口方法上添加注解如下:
@PreAuthorize("hasRole('vip1')")
Springboot整合SpringSecurity--对静态文件进行权限管理的更多相关文章
- SpringBoot整合SpringSecurity示例实现前后分离权限注解
SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...
- (十三)整合 SpringSecurity 框架,实现用户权限管理
整合 SpringSecurity 框架,实现用户权限管理 1.Security简介 1.1 基础概念 1.2 核心API解读 2.SpringBoot整合SpringSecurity 2.1 流程描 ...
- Springboot 整合ApachShiro完成登录验证和权限管理
1.前言 做一个系统最大的问题就是安全问题以及权限的问题,如何正确的选择一个安全框架对自己的系统进行保护,这方面常用的框架有SpringSecurity,但考虑到它的庞大和复杂,大多数公司还是会选择 ...
- 9、SpringBoot整合之SpringBoot整合SpringSecurity
SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...
- boke练习: springboot整合springSecurity出现的问题,传递csrf
boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...
- SpringBoot整合SpringSecurity简单实现登入登出从零搭建
技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...
- (十二)整合 Shiro 框架,实现用户权限管理
整合 Shiro 框架,实现用户权限管理 1.Shiro简介 1.1 基础概念 1.2 核心角色 1.3 核心理念 2.SpringBoot整合Shiro 2.1 核心依赖 2.2 Shiro核心配置 ...
- SpringBoot整合SpringSecurity实现JWT认证
目录 前言 目录 1.创建SpringBoot工程 2.导入SpringSecurity与JWT的相关依赖 3.定义SpringSecurity需要的基础处理类 4. 构建JWT token工具类 5 ...
随机推荐
- ThinkPHP5使用PHPExcel实现数据导出功能
1.将PHPExcel类库文件夹放入extend目录下 2.导出功能实现 public function download(){ if(request()->isPost()){ $val = ...
- Python3-socketserver模块-网络服务器框架
Python3中的socketserver模块简化了编写网络服务器的任务 在实际的开发中,特别是多并发的情况下,socket模块显然对我们的用处不大,因为如果你要通过socket模块来实现并发的soc ...
- Redis五种数据类型应用场景
目录 1.1 回顾 2.1 应用场景 2.1.1 String 2.1.2 Hash 2.1.3 List 2.1.4 Zet 2.1.5 zset 3.1 小结 1.1 回顾 Redis的五种数据类 ...
- 基于TCP与UDP协议的socket通信
基于TCP与UDP协议的socket通信 C/S架构与初识socket 在开始socket介绍之前,得先知道一个Client端/服务端架构,也就是 C/S 架构,互联网中处处充满了 C/S 架构(Cl ...
- 实战技巧,Vue原来还可以这样写
hookEvent,原来可以这样监听组件生命周期 1. 内部监听生命周期函数 <template> <div class="echarts"></di ...
- antd图标库按需加载的插件实现
前景概要 antd是阿里出品的一款基于antd的UI组件库,使用简单,功能丰富,被广泛应用在中台项目开发中,虽然也出现了彩蛋事故,但不能否认antd本身的优秀,而我们公司在实际工作中也大量使用antd ...
- CentOS7下安装和配置SVN
1. 由于是在CentOS7最小化安装的操作系统环境安装SVN,我们首先排除一些环境因素.在此首先关闭了防火墙,安装了vim文本编辑工具. 2. 使用yum install -y subversi ...
- uni-app中textarea组件
textarea组件,官方给出的监听事件有以下事件: 其中一定要注意,当使用 v-model 对表单内容进行双向绑定的时候,@input 事件是在绑定变量变化前触发的,所以如果在input事件内打印绑 ...
- 【Flutter 实战】动画核心
老孟导读:动画系统是任何一个UI框架的核心功能,也是开发者学习一个UI框架的重中之重,同时也是比较难掌握的一部分,下面我们就一层一层的揭开 Flutter 动画的面纱. 任何程序的动画原理都是一样的, ...
- 521我发誓读完本文,再也不会担心Spring配置类问题了
当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...