java - 如果 SpEL 执行失败,如何在 Java 中跳过 PreAuthorize?
问题描述
我已经使用一些自定义 SpEL 函数保护了我的 API 端点。但是,由于@PreAuthorize
在构造 SpEL 表达式时出现异常,使用 SpEL 会导致运行时错误。我想在发生时@PreAuthorize
返回 true SPELExecutionException
。
解决方案
由于@PreAuthorize 用于验证请求者是否具有所需的授权,因此如果出现系统错误,用户绕过此安全机制没有任何意义。
你仍然可以绕过这个。看看下面的片段:只是一个更正它是 SpEL
@PostMapping()
@PreAuthorize("#{@someBean.validateAccess('HAS_ACCESS_TO_MY_METHOD')}")
public ResponseEntity<?> myMethod(@RequestBody final MyRequestBody requestBody) {
//some logic
}
@Service
public class SomeBean {
public boolean validateAccess(String permission){
boolean isAuthorized = false;
try{
//some logic to authorize
// set the hasAccess variable to true or false based on your logic
isAuthorized = true;
} catch (Exception e) {
// if some exception occurs set it to true
isAuthorized = true;
}
return isAuthorized;
}
}
推荐阅读
- javascript - 无论查询参数或本地化子域,有没有办法匹配 URL
- android - 清单合并失败:uses-sdk:minSdkVersion 7 不能小于版本 14
- csv - JRDataset 属性 CSV 文件
- css - 内容框响应高度
- unity3d - 使用恒定速度的统一动画为游戏对象设置动画
- ios - 执行从导航控件到标签栏导航 swift4 的 segue
- aws-lambda - 参考无服务器 yaml 中的属性
- c# - C# 8 从具体类型中调用默认实现
- wordpress - Wordpress 管理栏:更改前端用户的背景颜色
- python - Kotlin:从文件中加载包含字符串和元组的字典(哈希图)