2015-12-19 35 views

回答

2

無論事實如何,我發現用例超級怪異,你可以調整緩存幾乎所有與SpEL一起工作的東西。既然你可以用SpEL調用你想要的任何方法,那麼你就很好。

我意識到它比它應該更難,但下面的工作。首先創建一個靜態方法,它支票(可以使用SecurityContextHolder爲)

public class SecurityChecker { 

    public static boolean isSecured() { 
     // Whatever 
     return SecurityContextHolder.getContext().getAuthentication() != null; 
    } 
} 

然後在您的註解的方法,指定以下(假設myCache應該不受影響):

@Cacheable(cacheNames = "myCache", condition = "T(com.example.SecurityChecker).isSecured()") 
public Foo doIt(String key) { ... } 

目前有兩個問題:

  1. 您無法創建元註釋以避免重複condition屬性一遍又一遍(請參閱SPR-13475
  2. SpEL安裝程序不允許您輕鬆調用bean上的方法(這比調用靜態方法更好)。我剛剛爲此創建了SPR-13812