我正在使用带有ACL模块的Spring Secuirty 3。我使用自定义 PermissionEvaluator 通过 @PreAuthentication 注释保护方法。它的工作正常,但是每次 PermissionEvaluator 返回 ACCESS_DENIED 时,都会抛出 AccessDeniedException ,这会停止应用程序的执行。期望的行为将是当 PermissionEvaluator 返回并且 ACCESS_DENIED 时,仅阻止(跳过)安全方法调用,并且应用程序的其余部分保持正常运行。有没有人知道如何实现这个目标?
答案 0 :(得分:0)
如果您将每个调用都包含在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配置,这意味着代码行为保持更接近默认值,并且不依赖于外部配置来确定它的行为。