Spring security Spring Security-防止AccessDeniedException停止应用程序正常流

Spring security Spring Security-防止AccessDeniedException停止应用程序正常流,spring-security,Spring Security,我正在使用SpringSecurity3和ACL模块。我正在使用一个自定义的PermissionEvaluator通过@PreAuthentication注释保护这些方法。它的工作正常,但是每次PermissionEvaluator返回一个拒绝访问时,就会在某个点抛出一个拒绝访问异常,从而停止应用程序的执行。所需的行为是,当PermissionEvaluator返回且访问被拒绝时,仅阻止(跳过)安全方法调用,应用程序的其余部分保持正常运行。有人对如何实现这一点有想法吗?如果您将每个调用都包装在一

我正在使用SpringSecurity3和ACL模块。我正在使用一个自定义的PermissionEvaluator通过@PreAuthentication注释保护这些方法。它的工作正常,但是每次PermissionEvaluator返回一个拒绝访问时,就会在某个点抛出一个拒绝访问异常,从而停止应用程序的执行。所需的行为是,当PermissionEvaluator返回且访问被拒绝时,仅阻止(跳过)安全方法调用,应用程序的其余部分保持正常运行。有人对如何实现这一点有想法吗?

如果您将每个调用都包装在一个
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的结果如何,它都允许方法调用,因此,基本上就像禁用安全性检查这个答案,基本上就是这样做的:@Chepech-我用一个例子和一个理由更新了我的答案