关于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')")
相关推荐
-大头.2 分钟前
Spring Bean作用域深度解析与实战
java·后端·spring
qq_336313935 分钟前
java基础-排序算法
java·开发语言·排序算法
豆沙沙包?8 分钟前
2025年--Lc298-1019. 链表中的下一个更大节点(栈)--java版
java·数据结构·链表
fengfuyao9859 分钟前
匈牙利算法的MATLAB实现
java·算法·matlab
毕设源码-钟学长10 分钟前
【开题答辩全过程】以 基于springboot农科所农作物信息管理系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
b***66611 小时前
Spring Boot 整合 Apollo 配置中心实战
java·spring boot·后端
CoderYanger1 小时前
递归、搜索与回溯-综合练习:27.黄金矿工
java·算法·leetcode·深度优先·1024程序员节
vx_vxbs661 小时前
【SSM高校普法系统】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
android·java·python·mysql·小程序·php·idea
张较瘦_1 小时前
Springboot3 | ResponseEntity 完全使用教程
java·springboot·开发
毕设源码-郭学长1 小时前
【开题答辩全过程】以 高校兼职系统为例,包含答辩的问题和答案
java·spring boot