java - 用自定义注解继承@PreAuthorize注解,用于spring自定义方法安全表达式
问题描述
我已经在我的 Spring Boot 项目中成功实现了自定义方法安全表达式。
这是我的表达式根:
public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
public boolean checkTwoParamsAreEqual(Long param1, Long param2){
return param1.equals(param2);
}
//constructor and overridden methods ignored for brevity
}
方法安全表达式的所有其他必需配置和处理程序均已正确实现。
在我的控制器类中,我做了:
@GetMapping("/test")
@PreAuthorize("checkTwoParamsAreEqual(#id1, #id2)")
public String tst( @RequestParam("id1") Long id1, @RequestParam("id2") Long id2){
return "Voila!!!";
}
有了这个,当我用相同的值id1
和id2
请求参数发出获取请求时,它会返回Voila!!!
,对于不同的请求参数值,它会抛出 403 异常。这正是我想要的。
现在,我想通过为@PreAuthorize
.
我的自定义注释定义为:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@PreAuthorize("checkTwoParamsAreEqual(#id1, #id2)")
public @interface PreCheckParamsAreEqual {
}
而且,我通过以下方式更新了我的控制器:
@GetMapping("/test")
@PreCheckParamsAreEqual
public String tst( @RequestParam("id1") Long id1, @RequestParam("id2") Long id2){
return "Voila!!!";
}
通过此更改,无论我为请求参数传递什么值id1
,id2
它们总是返回“瞧!!!”。我想不出如何使用参数创建自定义注释的方法。
解决方案
推荐阅读
- node.js - 使用 yarn 构建 node.js 应用程序但出现错误 Command "build" not found
- django - Mac 上的 Django 安装
- angular7 - 如何延迟 Angular 7 中错误的 http 响应
- sql-server - SQL Server 中公式的动态计算
- go - 并行运行基准测试,即模拟同时请求
- node.js - Node Express Gateway 多个 API 端点
- verilog - 为什么一旦测试台进入 fr_count 条件块,我的“数据”寄存器变量会重置为 0?
- python - Openpyxl 没有写入我的 Excel 电子表格
- scala - 在这种情况下我可以避免变异吗?
- php - 2 如何改变'X' col。如果表格的每一行表示在单独的 HTML 表单中,并且每个提交按钮更改“X”值,则单击一次表格的值?