关于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')")
相关推荐
向着光芒的女孩1 小时前
【IDEA】关不了的Proxy Authentication弹框探索过程
java·ide·intellij-idea
Filotimo_2 小时前
Spring Boot 整合 JdbcTemplate(持久层)
java·spring boot·后端
李慕婉学姐2 小时前
【开题答辩过程】以《“饭否”食材搭配指南小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring·小程序
abments3 小时前
pgsql timestamp without time zone > character varying解决方案
java
sanggou4 小时前
大数据量查询处理方案 - 内存优化与高效展示
java
没有bug.的程序员4 小时前
Java 字节码:看懂 JVM 的“机器语言“
java·jvm·python·spring·微服务
-大头.4 小时前
深入理解 Java 内存区域与 JVM 运行机制
java·jvm
没有bug.的程序员4 小时前
JVM 整体架构:一套虚拟机的心脏与血管
java·jvm·spring boot·spring cloud·架构
晨枫阳4 小时前
不同语言的元组对比
java·前端·javascript
悟能不能悟4 小时前
怎么在idea合并2个个branch
java·ide·intellij-idea