2014-03-27 37 views
1

當我嘗試在我的@Preauthorize註釋中使用「或」運算符時,出現以下錯誤。我可以在Spring安全性的@Preauthorize中使用SpringEL的布爾值或運算符嗎?

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

和春季安全明確地說,它使用「春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

1.你有這個@EnableGlobalMethodSecurity(prePostEnabled = true)嗎? 2.你能展示更多StackTrace嗎?我不認爲這個問題是圍繞「或」運營商 –

回答

1

你確實可以像上面那樣使用EL,or這樣寫。訪問器按您期望的方式爲原語工作。我遇到的問題與SpringEL表達式的構造無關。

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