一、要求



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--对静态文件进行权限管理的更多相关文章

  1. SpringBoot整合SpringSecurity示例实现前后分离权限注解

    SpringBoot 整合SpringSecurity示例实现前后分离权限注解+JWT登录认证 作者:Sans_ juejin.im/post/5da82f066fb9a04e2a73daec 一.说 ...

  2. (十三)整合 SpringSecurity 框架,实现用户权限管理

    整合 SpringSecurity 框架,实现用户权限管理 1.Security简介 1.1 基础概念 1.2 核心API解读 2.SpringBoot整合SpringSecurity 2.1 流程描 ...

  3. Springboot 整合ApachShiro完成登录验证和权限管理

    1.前言 做一个系统最大的问题就是安全问题以及权限的问题,如何正确的选择一个安全框架对自己的系统进行保护,这方面常用的框架有SpringSecurity,但考虑到它的庞大和复杂,大多数公司还是会选择 ...

  4. 9、SpringBoot整合之SpringBoot整合SpringSecurity

    SpringBoot整合SpringSecurity 一.创建项目,选择依赖 选择Spring Web.Thymeleaf即可 二.在pom文件中导入相关依赖 <!-- 导入SpringSecu ...

  5. boke练习: springboot整合springSecurity出现的问题,传递csrf

    boke练习: springboot整合springSecurity出现的问题,传递csrf freemarker模板 在html页面中加入: <input name="_csrf&q ...

  6. SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建

    SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...

  7. SpringBoot整合SpringSecurity简单实现登入登出从零搭建

    技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...

  8. (十二)整合 Shiro 框架,实现用户权限管理

    整合 Shiro 框架,实现用户权限管理 1.Shiro简介 1.1 基础概念 1.2 核心角色 1.3 核心理念 2.SpringBoot整合Shiro 2.1 核心依赖 2.2 Shiro核心配置 ...

  9. SpringBoot整合SpringSecurity实现JWT认证

    目录 前言 目录 1.创建SpringBoot工程 2.导入SpringSecurity与JWT的相关依赖 3.定义SpringSecurity需要的基础处理类 4. 构建JWT token工具类 5 ...

随机推荐

  1. 谈谈spring-boot-starter-data-redis序列化

    在上一篇中springboot 2.X 集成redis中提到了在spring-boot-starter-data-redis中使用JdkSerializationRedisSerializerl来实现 ...

  2. Head First 设计模式

    OO基础 抽象 封装 多态 继承 OO原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程 为交互对象之间的松耦合设计而努力 对扩展开放,对修改关闭 依赖抽象,不要依赖具体类 最少知识原则 ...

  3. 07.Easymock的实际应用

    第一步下载对应的java包添加到工程中 并静态导入所需要的j类 import static org.easymock.EasyMock.*; 这里有的注意点 package com.fjnu.serv ...

  4. SpringBoot之入门教程-SpringBoot项目搭建

    SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺 ...

  5. Spring 容器的初始化

    读完这篇文章你将会收获到 了解到 Spring 容器初始化流程 ThreadLocal 在 Spring 中的最佳实践 面试中回答 Spring 容器初始化流程 引言 我们先从一个简单常见的代码入手分 ...

  6. 让对象拥有状态——C#中的状态模式

    大家好,老胡又在博客和大家见面了,在聊今天的主角之前,老胡先给大家讲一个以前发生的故事.   真实的故事 当老胡还是小胡的时候,跟随团队一起开发一款游戏.这款游戏是一款末日生存类游戏,玩家可以 收集资 ...

  7. Kafka消费者拉取数据异常Unexpected error code 2 while fetching data

    Kafka消费程序间歇性报同一个错: 上网没查到相关资料,只好自己分析.通过进一步分析日志发现,只有在拉取某一个特定的topic的数据时报错,如果拉取其他topic的数据则不会报错.而从这个异常信息来 ...

  8. APIO强掠计划(spfa+tarjan缩点)

    强掠计划 题目大意: \(Siruseri\) 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定,在每个路口都设立了一个 \(Siruseri\) 银行的 \(ATM\) 取款机.令人奇怪的 ...

  9. nginx限制访问域名,禁止IP访问

    有些时候我们希望系统只能通过固定的域名访问,禁止IP或者恶意绑定的域名访问. 下面的nginx配置,假如host变量不是指定的域名,将返回403. server { listen 80; server ...

  10. 汉王JAVA笔试题

    汉王JAVA笔试题 1,jsp中动态include与静态include的区别? (1)动态包含总是会检查文件中的变化,适合用于包含动态页面,并且可以带参数. (2)静态包含不会检查所含文件的变化,适用 ...