关于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')")
相关推荐
Predestination王瀞潞1 分钟前
Java EE3-我独自整合(第三章:Spring DI 入门案例)
java·spring·java-ee
Ttang234 分钟前
Java爬虫:Jsoup+OkHttp实战指南
java·爬虫·okhttp
李庆政3704 分钟前
OkHttp的基本使用 实现GET/POST请求 authenticator自动认证 Cookie管理 请求头设置
java·网络协议·http·okhttp·ssl
Chan167 分钟前
SpringAI:MCP 协议介绍与接入方法
java·人工智能·spring boot·spring·java-ee·intellij-idea·mcp
dllxhcjla12 分钟前
苍穹外卖2
java
迷藏49414 分钟前
**发散创新:Go语言中基于上下文的优雅错误处理机制设计与实战**在现代后端开发中,**错误处理**早已不是简单
java·开发语言·后端·python·golang
杰克尼17 分钟前
知识点总结--day10(Spring-Cloud框架)
java·开发语言
gelald20 分钟前
Spring - AOP 原理
java·后端·spring
zwqwyq21 分钟前
springboot与springcloud对应版本
java·spring boot·spring cloud
okiseethenwhat24 分钟前
Java 内部类详解
java·开发语言