关于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')")
相关推荐
努力努力再努力wz8 分钟前
【Qt入门系列】第一个 Qt Widgets 程序:项目创建、UI 文件、Hello World、对象树与 qDebug 日志
java·c语言·开发语言·数据结构·c++·qt·ui
东南门吹雪18 分钟前
Spring事务传播机制深度解析
java·数据库·spring
XS03010621 分钟前
Java基础 List集合
java·windows·list
凤凰院凶涛QAQ22 分钟前
《C++转Java快速入手系列》抽象类和接口篇
java·开发语言·c++
MuYiLuck25 分钟前
01-spring-boot-autoconfig-principle
java·spring·maven·自动配置
河阿里26 分钟前
Lambda表达式(Java):从语法本质到工程实践
java·开发语言
云烟成雨TD34 分钟前
Spring AI Alibaba 1.x 系列【47】状态图定义:StateGraph 源码解析
java·人工智能·spring
61900833636 分钟前
spring中 HTTP 请求常见格式
java·spring·http
Veggie2639 分钟前
cuda 13.2 install on ubuntu26
java