gpt4 book ai didi

java - 我可以将 SpringEL 的 boolean 值或运算符与 Spring 安全的 @Preauthorize 一起使用吗?

转载 作者:行者123 更新时间:2023-11-30 08:24:48 27 4
gpt4 key购买 nike

当我尝试在我的@Preauthorize 注释中使用“或”运算符时,出现以下错误。

java.lang.IllegalArgumentException: Failed to evaluate expression '#authenticatingUser.id == #id or hasRole('ROLE_ADMIN')'

我认为我的语法是正确的,如以下文档中的“或”示例所示。

http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/expressions.html

Spring Security 明确表示它使用“Spring EL 表达式”

http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

但我还没有在网上看到有人在@Preauthorize 注释中使用 boolean “或”运算符的任何示例。

为了完整性,这里是 Controller 代码。表面上,我希望用户能够更改自己的密码或管理员。我已经成功地单独使用了 #authenticatingUser.id == #idhasRole('ROLE_ADMIN'),但是使用 or 我得到了一个错误。

@RequestMapping(value = "{id}", method = RequestMethod.PUT)
@ResponseBody
@PreAuthorize("#authenticatingUser.id == #id or hasRole('ROLE_ADMIN')")
public User newPassword(@PathVariable int id, @RequestParam String newPassword, @ModelAttribute User authenticatingUser) {
User user = userService.findById(id);
user.setPassword(newPassword);
return userService.save(user);
}

最佳答案

您确实可以如上所示使用 EL,使用 编写。 # 访问器以您期望的方式为原语工作。我遇到的问题与 SpringEL 表达式的构造无关。

@PreAuthorize("#authenticatingUser.id == #id or hasRole('ROLE_ADMIN')")
public User newPassword(@PathVariable int id, @RequestParam String newPassword, @ModelAttribute User authenticatingUser) {

关于java - 我可以将 SpringEL 的 boolean 值或运算符与 Spring 安全的 @Preauthorize 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com