关于SpringSecurity出现Failed to evaluate expression ‘ps.hasAuthority(‘role‘)‘

1. 问题描述

在使用SpringSecurity对接口进行权限校验时,报错java.lang.IllegalArgumentException: Failed to evaluate expression 'ps.hasAuthority('role')'。

2. 解决方案

2.1 查看是否添加注解

对于SpringSecurity对配置类,我们需要添加注解@EnableGlobalMethodSecurity(prePostEnabled = true)。

java 复制代码
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration	
public class AdminSecurityConfig extends WebSecurityConfigurerAdapter {
	//...
}

2.2 校验接口实现类的名称

  • 对于直接使用@Service注解标识的接口实现类,其注入Spring容器后,对应bean名称的首字母会变为小写。例如注入的实现类为PermissionServiceImpl,其bean名称为permissionServiceImpl。
  • 对于使用@Service(value = "xxx"),其对应bean的名称就为xxx,直接使用即可。

2.3 校验是否添加@符号进行标识。

对应接口上添加了@PreAuthorize注解后,参数正确格式为@beanName.methodName(roleName)。例如:

java 复制代码
@PreAuthorize("@permissionServiceImpl.hasPermission('role')")
相关推荐
apcipot_rain3 小时前
C++转Java日记(06)
java·开发语言
金銀銅鐵7 小时前
带您看懂字节码指令(一):一帧里有什么
java·后端·ai编程
nice_lcj5209 小时前
【从零理解 Spring AI · 14】RAG 的四个环节:加载、切分、向量化、检索
java·spring boot·embedding·向量数据库·rag·spring ai·ai应用开发
学长毕设11 小时前
基于springboot科创竞赛信息平台-附源码
java·spring boot·后端
聪明的笨猪猪11 小时前
Java 工程案例:AI 生成 @Async 异步线程池踩坑
java·经验分享·面试·ai编程
学长毕设12 小时前
基于SpringBoot公务员考试交流平台-附源码
java·spring boot·后端
SL_staff14 小时前
私有化低代码:面向强监管行业的可控数字化实践
java·低代码
三翼鸟数字化技术团队15 小时前
高性能存储引擎RocksDB:原理与Java生态实践
java
SL_staff15 小时前
JVS-APS有限产能建模的七个刚性支点:面向产线真实约束的技术实现解析
java·开源·全栈
特立独行的猫A15 小时前
少 3 个环节、少 1 次 ABI、少 3500 行代码:cj-tauri 与 Ionic/Capacitor 的鸿蒙桥接机制对比
java·后端·python