spring - 方法级别的@PreAuthorize - 如何将类级别变量注入自定义 PreAuthorize 方法参数
问题描述
这是我的代码:
public class MyController {
@Value("${resource.clientId}") // this value is retreiving from vault
private String clientId;
@PreAuthorize("isClient(#clientId)") //isClient a custom security method
public String Mymethod(Authentication authentication){
}
}
如果我使用 clientId 作为 Mymethod 的 arg,那么它工作正常。但与此同时,由于保险库 APPROLE 令牌到期,我面临着“找不到占位符 resource.clientId”之类的问题。
因此决定将方法 arg 更改为上面的类级别变量。但是这个@PreAuthorize [@PreAuthorize("isClient(#clientId)")] 逻辑没有选择clientid。谁能分享一个合适的方法来解决这个问题?我需要在这里使用哪个表达式来解决这个问题?
解决方案
最后,我找到了一个解决方案——我不确定有没有比这更好的解决方案。
public class MyController {
public static String CLIENT_ID;
@Value("${resource.clientId}") // this value is retrieving from vault
public void setClientId(String clientId) {
CLIENT_ID = clientId;
}
@PreAuthorize("isClient({T(com.test.MyController).CLIENT_ID})") //isClient a custom security method
public String Mymethod(Authentication authentication){
}
}
使用非静态方法将保险库值注入静态变量,最后将该静态变量传递给 @PreAuthorize 自定义方法。我希望,这对其他人也有帮助..
推荐阅读
- r - 同时将函数列表应用于一个矩阵
- node.js - Azure 存储队列 - Cosmos DB 输出 - 错误处理
- python - 聚合后对列中的值进行四舍五入
- javascript - 如何在条件语句下更改单元格的颜色?
- c# - 已发布的 C# 控制台应用程序 - 系统找不到指定的文件
- javascript - 更改边距
并在目标 c 中的 wkwebview 套件中 - javascript - 在 try...catch 语句中获取未定义的变量
- python - 在用户视图中添加 ManyTomany 列表(Odoo 12)
- python - 姜戈;TemplateDoesNotExist 错误 - 博客应用程序
- node.js - 将 nodejs 从 mongo atlas 迁移到 mongodb 4