关于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')")
相关推荐
疯狂成瘾者17 分钟前
YAML配置介绍
java
cccccc语言我来了18 分钟前
C++轻量级消息队列服务器
java·服务器·c++
better_liang23 分钟前
每日Java面试场景题知识点之-MCP协议在Java开发中的应用实践
java·spring boot·ai·mcp·企业级开发
河阿里23 分钟前
SpringBoot :使用 @Configuration 集中管理 Bean
java·spring boot·spring
xiaoshuaishuai823 分钟前
C# Codex 脚本编写
java·服务器·数据库·c#
Flittly25 分钟前
【SpringSecurity新手村系列】(4)验证码功能实现
java·spring boot·安全·spring
Flittly30 分钟前
【SpringSecurity新手村系列】(3)自定义登录页与表单认证
java·笔记·安全·spring·springboot
小小码农Come on1 小时前
C++访问QML控件-----QML访问C++对象属性和方法
java·开发语言·c++
Stella Blog1 小时前
狂神Java基础学习笔记Day04
java·笔记·学习
RReality1 小时前
【Unity Shader URP】Matcap 材质捕捉实战教程
java·ui·unity·游戏引擎·图形渲染·材质