【问题标题】:Spring Security - Prevent AccessDeniedException from stopping application normal flowSpring Security - 防止 AccessDeniedException 停止应用程序正常流程
【发布时间】:2011-06-29 06:19:43
【问题描述】:

我正在使用带有 ACL 模块的 Spring Secuirty 3。我使用自定义 PermissionEvaluator 来保护带有 @PreAuthentication 注释的方法。它工作正常,但是每次 PermissionEvaluator 返回一个 ACCESS_DENIED 时都会抛出一个 AccessDeniedException 并且这会停止应用程序的执行。期望的行为是当 PermissionEvaluator 返回并且 ACCESS_DENIED 时,仅阻止(跳过)安全方法调用,而应用程序的其余部分保持正常运行。有没有人知道如何实现这一目标?

【问题讨论】:

标签: spring-security


【解决方案1】:

如果您将每次调用都包装在try...catch 中,您可以得到这种行为。基本上,由于它是一个异常,任何正常的异常处理都将适用于它。如果您的应用程序可以处理该异常并正常继续,那么就这样做!


这是我的意思的一个例子:

// The 1st method that could be denied but you want to continue execution after
try {
    // Call method A that will throw the exception
} catch (/*The exception you expect to be thrown and want to continue after*/){}


// The 2nd method that could be denied but you want to continue execution after
try {
    // Call method B that will throw the exception
} catch (/*The exception you expect to be thrown and want to continue after*/){}

etc.

是的,调用这些方法确实会增加很多开销,但这是一种相当简单的方法,可以在引发异常后继续执行。

我还认为它也更正确,因为调用代码确实知道如何处理这些异常。这也不需要任何额外的 Spring 配置,这意味着代码行为保持更接近默认值,并且不依赖外部配置来确定其行为。

【讨论】:

  • 这样做的问题是,无论 PermissionEvaluator 的结果如何,它都允许方法调用,所以它基本上就像禁用安全性
  • 检查这个答案,基本上就是这样:stackoverflow.com/questions/4621394/…
  • @Chepech - 我用一个例子和一个理由更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
相关资源
最近更新 更多