我可以在Spring security的@Preauthorize中使用SpringEL的布尔值或运算符吗?

时间:2014-03-27 06:37:24

标签: java spring spring-security

当我尝试使用"或"我的@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注释中的运算符。

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

@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);
}

1 个答案:

答案 0 :(得分:1)

您可以使用上面显示的EL,并写入or#访问器可以按照您期望的方式处理基元。我遇到的问题与SpringEL表达式的结构无关。

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