当前位置: 首页 > news >正文

SpringSecurity-授权示例

用户基于权限进行授权

定义用户与权限

authorities()。

package com.cms.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;/*** @author: coffee* @date: 2024/6/27 20:33* @description: ...*/
@Configuration
public class UserConfig {@Beanpublic UserDetailsService userDetailsService () {InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();UserDetails user1 = User.withUsername("john").password("123456").authorities("READ").build();UserDetails user2 = User.withUsername("jane").password("123456").authorities("WRITE").build();userDetailsManager.createUser(user1);userDetailsManager.createUser(user2);return userDetailsManager;}@Beanpublic PasswordEncoder passwordEncoder () {return NoOpPasswordEncoder.getInstance();}}

权限维度授权配置

package com.cms.config;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @author: coffee* @date: 2024/6/27 20:37* @description: 基于用户权限限制所有端点的访问*/
@Configuration
public class ProjectConfig extends WebSecurityConfigurerAdapter {/*** 指定用户可以访问端点的条件:1.hasAuthority() 2.hasAnyAuthority()  3.access()*/@Overrideprotected void configure (HttpSecurity httpSecurity) throws Exception {httpSecurity.httpBasic();// permitAll()方法修改授权配置,无需凭据(用户名密码)也可以直接调用接口。   curl http://localhost:8080/hello// httpSecurity.authorizeRequests().anyRequest().permitAll();// 指定用户可以访问端点的条件-hasAuthority 。 发现john报403、jane正常;// httpSecurity.authorizeRequests().anyRequest().hasAuthority("WRITE");// 允许具有WRITE或者READ权限的用户访问端点-hasAnyAuthority。  发现john报正常、jane正常;httpSecurity.authorizeRequests().anyRequest().hasAnyAuthority("WRITE","READ");// access() - 为配置访问提供了无限的可能性,因为应用程序会基于SPEL构建授权规则。但是,他会让代码更难阅读和调试。所以作为次要解决方案,仅在不能使用hasAuthority和hasAnyAuthority时才使用}
}

用户基于角色进行授权

定义用户与角色

roles()。

package com.cms.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;/*** @author: coffee* @date: 2024/6/27 20:33* @description: ...*/
@Configuration
public class UserConfig {@Beanpublic UserDetailsService userDetailsService () {InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();// authorities:使用"ROLE_"前缀,GrantedAuthority现在就表示一个角色UserDetails user1 = User.withUsername("john").password("123456").authorities("ROLE_ADMIN").build();// roles:不需要添加"ROLE_"前缀// UserDetails user1 = User.withUsername("john").password("123456").roles("ADMIN").build();UserDetails user2 = User.withUsername("jane").password("123456").authorities("ROLE_MANAGER").build();// UserDetails user2 = User.withUsername("jane").password("123456").roles("MANAGER").build();userDetailsManager.createUser(user1);userDetailsManager.createUser(user2);return userDetailsManager;}@Beanpublic PasswordEncoder passwordEncoder () {return NoOpPasswordEncoder.getInstance();}}

角色维度授权配置

package com.cms.config;import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** @author: coffee* @date: 2024/6/27 20:37* @description: 基于用户权限限制所有端点的访问*/
@Configuration
public class ProjectConfig extends WebSecurityConfigurerAdapter {/*** 指定用户可以访问端点的条件:1.hasAuthority() 2.hasAnyAuthority()  3.access()*/@Overrideprotected void configure (HttpSecurity httpSecurity) throws Exception {httpSecurity.httpBasic();// permitAll()方法修改授权配置,无需凭据(用户名密码)也可以直接调用接口。   curl http://localhost:8080/hello// httpSecurity.authorizeRequests().anyRequest().permitAll();// 指定用户可以访问端点的条件-hasRole 。 hasRole()方法现在会指定允许访问端点的角色。请注意,这里没有出现ROLE_前缀// httpSecurity.authorizeRequests().anyRequest().hasRole("ADMIN");// 允许具有ADMIN或者MANAGER角色权限的用户访问端点-hasAnyRole。httpSecurity.authorizeRequests().anyRequest().hasAnyRole("ADMIN","MANAGER");// access() - 为配置访问提供了无限的可能性,因为应用程序会基于SPEL构建授权规则。但是,他会让代码更难阅读和调试。所以作为次要解决方案,仅在不能使用hasRole和hasAnyRole时才使用}
}

相关文章:

SpringSecurity-授权示例

用户基于权限进行授权 定义用户与权限 authorities()。 package com.cms.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.core.userdetails.User; import…...

选哪个短剧系统源码好:全面评估与决策指南

在短剧内容创作和分享日益流行的今天,选择合适的短剧系统源码对于构建一个成功的短剧平台至关重要。短剧系统源码不仅关系到平台的稳定性和用户体验,还直接影响到内容创作者和观众的互动质量。本文将提供一份全面的评估指南,帮助您在众多短剧…...

AI时代的软件工程:挑战与改变

人工智能(AI)正以惊人的速度改变着我们的生活和工作方式。作为与AI关系最为密切的领域之一,软件工程正经历着深刻的转变。 1 软件工程的演变 软件工程的起源 软件工程(Software Engineering)是关于如何系统化、规范化地…...

Zuul介绍

Zuul 是 Netflix 开源的一个云平台网络层代理,它主要用于路由、负载均衡、中间件通信和动态路由。Zuul 本质上是一个基于 JVM 的网关,它提供了以下功能: 1.路由:Zuul 允许客户端和服务器之间的所有入站和出站请求通过一个中心化的…...

7-1作业

1.实验目的:完成字符收发 led.h #ifndef __GPIO_H__ #define __GPIO_H__#include "stm32mp1xx_rcc.h" #include "stm32mp1xx_gpio.h" #include "stm32mp1xx_uart.h"//RCC,GPIO,UART初始化 void init();//字符数据发送 void set_tt…...

ElasticSearch安装、配置详细步骤

一、环境及版本介绍 操作系统: Windows 10 软件版本: elasticsearch-7.17.22、kibana-7.17.22、IK-7.17.22 开发环境选择软件版本应提前考虑正式系统环境,否则会产生软件与服务器环境不兼容的问题出现,ElasticSearch与环境支持…...

【Mybatis 与 Spring】事务相关汇总

之前分享的几篇文章可以一起看,形成一个体系 【Mybatis】一级缓存与二级缓存源码分析与自定义二级缓存 【Spring】Spring事务相关源码分析 【Mybatis】Mybatis数据源与事务源码分析 Spring与Mybaitis融合 SpringManagedTransaction: org.mybatis.spri…...

Leetcode 2065. 最大化一张图中的路径价值(DFS / 最短路)

Leetcode 2065. 最大化一张图中的路径价值 暴力DFS 容易想到,从0点出发DFS,期间维护已经走过的距离(时间)和途径点的权值之和,若访问到0点则更新答案,若下一步的距离与已走过的距离和超出了maxTime&#…...

SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution

CVPR2024 香港理工大学&OPPO&bytedancehttps://github.com/cswry/SeeSR?tabreadme-ov-file#-licensehttps://arxiv.org/pdf/2311.16518#page5.80 问题引入 因为有些LR退化情况比较严重,所以超分之后的结果会出现语义的不一致的情况,所以本文训…...

七月论文审稿GPT第5版:拿我司七月的早期paper-7方面review数据集微调LLama 3

前言 llama 3出来后,为了通过paper-review的数据集微调3,有以下各种方式 不用任何框架 工具 技术,直接微调原生的llama 3,毕竟也有8k长度了 效果不期望有多高,纯作为baseline通过PI,把llama 3的8K长度扩展…...

盘古5.0,靠什么去解最难的题?

文|周效敬 编|王一粟 当大模型的竞争开始拼落地,商业化在B端和C端都展开了自由生长。 在B端,借助云计算向千行万业扎根;在C端,通过软件App和智能终端快速迭代。 在华为,这家曾经以通信行业起…...

2.3章节Python中的数值类型

1.整型数值 2.浮点型数值 3.复数   Python中的数值类型清晰且丰富,主要分为以下几种类型,每种类型都有其特定的用途和特性。 一、整型数值 1.定义:整数类型用于表示整数值,如1、-5、100等。 2.特点: Python 3中的…...

每日Attention学习7——Frequency-Perception Module

模块出处 [link] [code] [ACM MM 23] Frequency Perception Network for Camouflaged Object Detection 模块名称 Frequency-Perception Module (FPM) 模块作用 获取频域信息,更好识别伪装对象 模块结构 模块代码 import torch import torch.nn as nn import to…...

【从0实现React18】 (五) 初探react mount流程 完成核心递归流程

更新流程的目的: 生成wip fiberNode树标记副作用flags 更新流程的步骤: 递:beginWork归:completeWork 在 上一节 ,我们探讨了 React 应用在首次渲染或后续更新时的整体更新流程。在 Reconciler 工作流程中&#xff…...

0-30 VDC 稳压电源,电流控制 0.002-3 A

怎么运行的 首先,有一个次级绕组额定值为 24 V/3 A 的降压电源变压器,连接在电路输入点的引脚 1 和 2 上。(电源输出的质量将直接影响与变压器的质量成正比)。变压器次级绕组的交流电压经四个二极管D1-D4组成的电桥整流。桥输出端…...

HTML5+CSS3+JS小实例:图片九宫格

实例:图片九宫格 技术栈:HTML+CSS+JS 效果: 源码: 【HTML】 <!DOCTYPE html> <html lang="zh-CN"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1…...

湘潭大学软件工程数据库总结

文章目录 前言试卷结构给学弟学妹的一些参考自己的一些总结 前言 自己可能很早很早之前就准备复习了&#xff0c;但是感觉还是没有学到要点&#xff0c;主要还是没啥紧迫的压力&#xff0c;我们是三月份开学&#xff0c;那时候实验室有朋友挺认真开始学习数据库了&#xff0c;…...

Codeforces Testing Round 1 B. Right Triangles 题解 组合数学

Right Triangles 题目描述 You are given a n m nm nm field consisting only of periods (‘.’) and asterisks (‘*’). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of ‘*’-cells. …...

怎样将word默认Microsoft Office,而不是WPS

设置——>应用——>默认应用——>选择"word"——>将doc和docx都选择Microsoft Word即可...

C语言之进程的学习2

Env环境变量&#xff08;操作系统的全局变量&#xff09;...

一、ES6-let声明变量【解刨分析最详细】

一、块级作用域 { let Tim"Tim是靓仔&#xff01;" } console.log("Tim:",Tim) 打印结果&#xff1a;Tim未进行任何定义&#xff01; 原因&#xff1a;因为Tim定义再块级{}里面&#xff0c;它的声音Tim只服务于该块级里面。而打印结果是再块级外面&#…...

【笔记】PyCharm 使用问题反馈与官方进展速览

#工作记录 https://youtrack.jetbrains.com/issue/IJPL-190308 【笔记】记一次PyCharm的问题反馈_the polyglot context is using an implementation th-CSDN博客 【笔记】与PyCharm官方沟通解决开发环境问题-CSDN博客 与 JetBrains 官方沟通记录&#xff08;PyCharm 相关问题…...

Ubuntu挂载本地镜像源(像CentOS 一样挂载本地镜像源)

1.挂载 ISO 镜像 sudo mount -o loop /ubuntu-22.04.5-desktop-amd64.iso /mnt/iso 2.备份现有的软件源配置文件&#xff1a; sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 3.编辑软件源配置文件 编辑 /etc/apt/sources.list sudo nano /etc/apt/sources.l…...

windows10下搭建nfs服务器

windows10下搭建nfs服务器 有参考这篇博客 Windows10搭建NFS服务 - fuzidage - 博客园 下载 NFS Server这个app 通过网盘分享的文件&#xff1a;nfs1268 (1).exe 链接: https://pan.baidu.com/s/1rE4h710Uh-13kWGXvjkZzw 提取码: mwa4 --来自百度网盘超级会员v5的分享 下载后…...

[蓝桥杯 2024 国 B] 蚂蚁开会

问题描述 二维平面上有 n 只蚂蚁&#xff0c;每只蚂蚁有一条线段作为活动范围&#xff0c;第 i 只蚂蚁的活动范围的两个端点为 (uix,uiy),(vix,viy)。现在蚂蚁们考虑在这些线段的交点处设置会议中心。为了尽可能节省经费&#xff0c;它们决定只在所有交点为整点的地方设置会议…...

02.管理数据库

管理数据库 1. 创建数据库 mysql> create database db1; Query OK, 1 row affected (0.01 sec)mysql> show databases; -------------------- | Database | -------------------- | db1 | | hellodb | | information_schema | | m…...

第22讲、Odoo18 QWeb 模板引擎详解

Odoo QWeb 模板引擎详解与实战 Odoo 的 QWeb 是其自研的模板引擎&#xff0c;广泛应用于 HTML、XML、PDF 等内容的生成&#xff0c;支撑了前端页面渲染、报表输出、门户页面、邮件模板等多种场景。本文将系统介绍 QWeb 的核心用法、工作原理&#xff0c;并通过实战案例演示如何…...

一文掌握 Tombola 抽象基类的自动化子类测试策略

深入解析 Python 抽象基类的自动化测试框架设计 在 Python 开发中&#xff0c;抽象基类&#xff08;ABC&#xff09;是定义接口规范的强大工具。本文将以 Tombola 抽象基类为例&#xff0c;详细解析其子类的自动化测试框架设计&#xff0c;展示如何通过 Python 的内省机制实现…...

csharp基础....

int[][] jaggedArray new int[3][]; jaggedArray[0] new int[] { 1, 2 }; jaggedArray[1] new int[] { 3, 4, 5 }; jaggedArray[2] new int[] { 6, 7, 8, 9 }; 嵌套 反转和排序 List<int> list new List<int> { 1, 2, 3, 4, 5 }; list.Reverse(); Cons…...

详细介绍uni-app中Composition API和Options API的使用方法

uni-app 中 Composition API 和 Options API 的使用方法详解 一、Options API&#xff08;Vue 2.x 传统方式&#xff09; 1. 基本结构 Options API 通过配置对象的不同选项&#xff08;如 data、methods、computed 等&#xff09;组织代码&#xff1a; <template><…...