关于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')")
相关推荐
沐浴露z1 天前
【微服务】基本概念介绍
java·微服务
Z3r4y1 天前
【代码审计】RuoYi-4.7.3&4.7.8 定时任务RCE 漏洞分析
java·web安全·ruoyi·代码审计
Kuo-Teng1 天前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
Jooou1 天前
Spring事务实现原理深度解析:从源码到架构全面剖析
java·spring·架构·事务
盖世英雄酱581361 天前
commit 成功为什么数据只更新了部分?
java·数据库·后端
码上淘金1 天前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json
刘一说1 天前
Spring Boot 应用的指标收集与监控体系构建指南
java·spring boot·后端
老友@1 天前
Java Excel 导出:EasyExcel 使用详解
java·开发语言·excel·easyexcel·excel导出
Full Stack Developme1 天前
java.net.http 包详解
java·http·.net