绕过@Cacheable 用于(未)验证的请求
Bypass @Cacheable for (un)authenticated requests
能否让Spring的缓存框架知道请求上下文的身份验证状态,还是更容易推出自己的缓存解决方案?
尽管我觉得这个用例超级奇怪,但您可以为几乎所有适用于 SpEL 的东西设置条件缓存。由于您可以使用 SpEL 调用您想要的任何方法,所以您可以开始了。
我意识到这比它应该的要难,但下面的工作。首先创建一个 static 方法来进行检查(您可以为此使用 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) { ... }
目前有两个问题:
能否让Spring的缓存框架知道请求上下文的身份验证状态,还是更容易推出自己的缓存解决方案?
尽管我觉得这个用例超级奇怪,但您可以为几乎所有适用于 SpEL 的东西设置条件缓存。由于您可以使用 SpEL 调用您想要的任何方法,所以您可以开始了。
我意识到这比它应该的要难,但下面的工作。首先创建一个 static 方法来进行检查(您可以为此使用 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) { ... }
目前有两个问题: