说明
(2)Spring Boot 2.0.6
(3)Spring Security 5.0.9
(4)Spring Data JPA 2.0.11.RELEASE
(5)hibernate5.2.17.Final
(6)MySQLDriver 5.1.47
(7)MySQL 8.0.12
需求缘起
在之前的章节中我们介绍过通过注解的方式进行权限的控制了,这里再详细的讲解下方法级安全的几个注解。
一、注解式方法级安全开启
需要在WebSecuirtyConfig添加配置:
@Configuration
@EnableWebSecurity //启用Spring Security.
////会拦截注解了@PreAuthrize注解的配置.
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
}
二、允许的注解
这里主要@PreAuthorize, @PostAuthorize, @Secured这三个注解可以使用。
2.1 @Secured
当@EnableGlobalMethodSecurity(securedEnabled=true)的时候,@Secured可以使用:
@GetMapping("/helloUser")
@Secured({"ROLE_normal","ROLE_admin"})
public String helloUser() {
return "hello,user";
}
说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。
如果我们要求,只有同时拥有admin & noremal的用户才能方法helloUser()方法,这时候@Secured就无能为力了。
2.2 @PreAuthorize
Spring的 @PreAuthorize/@PostAuthorize 注解更适合方法级的安全,也支持Spring 表达式语言,提供了基于表达式的访问控制。
当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PreAuthorize可以使用:
@GetMapping("/helloUser")
@PreAuthorize("hasAnyRole('normal','admin')")
public String helloUser() {
return "hello,user";
}
说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。
此时如果我们要求用户必须同时拥有normal和admin的话,那么可以这么编码:
@GetMapping("/helloUser")
@PreAuthorize("hasRole('normal') AND hasRole('admin')")
public String helloUser() {
return "hello,user";
}
此时如果使用user/123登录的话,就无法访问helloUser()的方法了。
2.3 @PostAuthorize
@PostAuthorize 注解使用并不多,在方法执行后再进行权限验证,适合验证带有返回值的权限,Spring EL 提供 返回对象能够在表达式语言中获取返回的对象returnObject。
当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PostAuthorize可以使用:
@GetMapping("/helloUser")
@PostAuthorize(" returnObject!=null && returnObject.username == authentication.name")
public User helloUser() {
Object pricipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
User user;
if("anonymousUser".equals(pricipal)) {
user = null;
}else {
user = (User) pricipal;
}
return user;
}
这三个最常用也就是@PreAuthorize这个注解了,在使用中主要是配合Spring EL表达式。
历史文章
215.Spring Boot+Spring Security:初体验
216.Spring Boot+Spring Security:基于内存的认证信息
217.Spring Boot+Spring Security:基于内存的角色授权
218.Spring Boot+Spring Security:基于内存数据库的身份认证和角色授权
219.Spring Boot+Spring Security:基于MySQL数据库的身份认证和角色授权
220.Spring Boot+Spring Security:自定义登录页面和构建主页
221.Spring Boot+Spring Security:登出和403处理
222.Spring Boot+Spring Security:动态加载角色
223.Spring Boot+Spring Security:原理1
224.Spring Boot+Spring Security:自定义Filter
246.Spring Boot+Spring Security:页面白名单和获取登录信息
13. Spring Boot+Spring Security:基于URL动态权限n种方案
248.Spring Boot+Spring Security:基于URL动态权限:准备工作
249.Spring Boot+Spring Security:基于URL动态权限:扩展access()的SpEL表达式
250.Spring Boot+Spring Security:基于URL动态权限:自定义AccssDesionManager
251.Spring Boot+Spring Security:基于URL动态权限:自定义Filter
252.Spring Boot+Spring Security:标签sec:authorize的使用
253.Spring Boot+Spring Security:获取用户信息和session并发控制
我就是我,是颜色不一样的烟火。
我就是我,是与众不同的小苹果。
à悟空学院:https://t.cn/Rg3fKJD
学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!
SpringBoot视频:http://t.cn/A6ZagYTi
Spring Cloud视频:http://t.cn/A6ZagxSR
SpringBoot Shiro视频:http://t.cn/A6Zag7IV
SpringBoot交流平台:https://t.cn/R3QDhU0
SpringData和JPA视频:http://t.cn/A6Zad1OH
SpringSecurity5.0视频:http://t.cn/A6ZadMBe
Sharding-JDBC分库分表实战:http://t.cn/A6ZarrqS
分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr
相关推荐
访问控制使用 @PreAuthorize 和 @PostAuthorize 15.3.1.2. 过滤使用 @PreFilter 和 @PostFilter V. 高级话题 16. 领域对象安全(ACLs) 16.1. 概述 16.2. 关键概念 16.3. 开始 17. 预认证场景 17.1. 预认证...
访问控制使用@PreAuthorize 和@PostAuthorize 15.3.1.2. 过滤使用@PreFilter 和@PostFilter 16. acegi 到spring security 的转换方式 16.1. Spring Security 是什么 16.2. 目标 16.3. 步骤 16.4. 总结 ...
访问控制使用 @PreAuthorize 和 @PostAuthorize 15.3.1.2. 过滤使用 @PreFilter 和 @PostFilter 16. acegi到spring security的转换方式 16.1. Spring Security是什么 16.2. 目标 16.3. 步骤 16.4. 总结...
本项目基于Spring,整合Spring的security模块,实现用户管理和权限控制,是一套较为通用的权限控制功能,主要内容如下: 1.登录,包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始密码1234; 3.拦截器...
SocketIO * socketGlobal = [[SocketIO alloc ] initWithDelegate: self host: @" localhost " port: 8080 namespace: nil timeout: 1000 secured: NO ]; [socketGlobal connect ]; 您可以使用命名空间: SocketIO...
Networkers2009:BRKCAM-2001 - Secured Wireless Networks: Bestpractices
信息安全_数据安全_Get your APIs Secured with Otoro 安全工作 安全研究 边界防御 安全设计 基础设施
自己项目中用到的一个CXF的wsdl,花了很多时间才搞明白的。
最新的2005版的03.48规范升级版,描述了电信GSM短信SMS结构和安全机制,简介如下: The present document specifies the structure of the Secured Packets in a general format and in implementations using Short ...
Across all industries, companies are pouring millions of dollars into taming cybercrime and other related security crime. Achieving and Sustaining Secured Business Operations presents a holistic ...
在高度安全的环境下如何进行渗透测试,主要讲BT5下的工具的使用
A secured tag of implementation of traceability in textile and clothing supply chain 纺织和服装供应链可追溯性的安全标签.one
物联网安全物联网安全堆栈安全TLS选项和ACL认证客户端配置在MQTTX中需要是这样的:
「WEB应用防火墙」A Secured and Robust Wireless Communication System for Low-Latency Applications - 威胁情报 WAF 系统安全 等级保护 云安全 安全体系
[Packt Publishing] VMware VCloud 安全教程 (英文版) [Packt Publishing] Vmware VCloud Security (E-Book) ☆ 图书概要:☆ Overview Take away an in-depth knowledge of how to secure a private cloud ...
ADVANCED SECURED SYSTEM
matlab代码如何加密安全音频传输 使用不同的流行算法对给定的音频文件进行编码和加密。 如何使用: 下载zip文件,并在代码中创建给定名称的任何音频文件。 分别使用server.m和sender.m制作Matlab服务器和Matlab接收...
A Secured and Robust Wireless Communication System for Low-Latency Applications 业务风控 渗透测试 APT 信息安全 渗透测试
IOT-secured-源码.rar
汽车网络信息安全技术 AUTOSAR 中SecOC与其他模块的交互 详细SecOC通讯过程图 安全报文介绍 新鲜度值 新鲜度值的构成:基于同步的复合Counter 新鲜度值的管理 新鲜度值中每个计数器详解 新鲜度值的构建 ...