当前位置: 首页 > 知识库问答 >
问题:

正在捕获AuthenticationProvider中引发的异常

通京
2023-03-14

我正在实现自定义'AuthenticationProvider'。如果没有经过身份验证,我将在'authenticate'函数中抛出异常,如下所示。

public class DelegatingLdapAuthenticationProvider implements AuthenticationProvider {

    private ActiveDirectoryLdapAuthenticationProvider primaryProvider;
    private List<ActiveDirectoryLdapAuthenticationProvider> secondaryProviders = new ArrayList<>();

    public DelegatingLdapAuthenticationProvider() {

    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        Authentication result = null;
        AuthenticationException exception = null;
        try {
            result = primaryProvider.authenticate(authentication);
        } catch (AuthenticationException e) {
            exception = e;
            for (ActiveDirectoryLdapAuthenticationProvider secondaryProvider : secondaryProviders) {
                try {
                    result = secondaryProvider.authenticate(authentication);
                    if (result.isAuthenticated()) {
                            break;
                    }
                } catch (AuthenticationException e1) {
                            exception = e;
                }
            }
        }
        if (result == null || !result.isAuthenticated()) {
            throw exception;
    }

    return result;
}

我有全局异常处理程序,如下所示。

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler({NoPermissionException.class})
    @ResponseBody
    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    public Map<String, String> noPermission(NoPermissionException e) {
        return createErrorResponse(e, "Don't have permissions");
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public Map<String, String> exceptionInProcessing(Exception e) {
        return createErrorResponse(e, "Unable to process. Unknown error occurred: " + e.getMessage());
    }

    private Map<String, String> createErrorResponse(Exception e, String errorMessage) {
        Map<String, String> errorResponse = new HashMap<>();
        errorResponse.put("message", errorMessage);
        errorResponse.put("reason", e.toString());
        return errorResponse;
    }
}

当在'authenticate'函数内部引发异常时,不会调用全局异常处理程序。对于所有其他例外情况,它正在被调用。我想在全局异常处理程序中捕获异常并返回自定义错误消息。我怎么能那样做?感谢任何帮助。提前道谢。

共有1个答案

万俟均
2023-03-14

GlobalExceptionHandler用于控制器异常处理程序,但AuthenticationProvider仍在筛选器中,如果要处理AuthenticationException,则需要处理它以实现AuthenticationEntryPoint并重写Commentence方法。

public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException

AuthenticationExceptionAccessDeniedException已由ExceptionTranslationFilter处理。您只需要注入authenticationentrypointaccessdeniedhandler(它们处理accessdeniedexception)

或者,您可以在filter中捕获这些异常,然后在filer中处理它,如authenticationfailurehandlerabstractauthenticationprocessingfilter

 类似资料:
  • 所以我有两个关于java的一般问题。第一个问题是,什么时候在方法体中使用try/catch,而不是在声明方法时使用throws异常?这是我的意思的一个小例子。这是: 对抗 然后我的第二个问题是什么时候知道捕获或抛出什么类型的异常?我指的是诸如IOException或EOFException等异常... 如果有一个好的链接,有人可以发给我教这一切(这可能比我想象的更复杂),我会像你回答它一样感激。谢

  • null 我的例外是没有被抓到。我做错了什么?

  • 我无法在Spring中捕获异步方法抛出的异常。我已经编写了一个未捕获的异常处理程序来捕获,但没有成功。该应用程序将启用启动任意数量的永远运行的异步作业。我认为我的异步方法需要返回Future,以便我可以将其存储在hashmap中并检查其状态或停止作业。我也可以通过存储它来获取所有正在运行的作业。我认为我不能使用get method of Future,因为如果输入正确,它会阻塞,我的作业将永远运行

  • 问题内容: 我正在尝试编写一个使用PyQt5在系统托盘中运行的应用程序。该代码有时会引发异常,我需要能够捕获它们。 我希望当应用程序中发生异常时,会退出主事件循环,因此像这样捕获它应该起作用: 在下面的示例中,当我按下“提高”按钮时,我仅看到回溯,但从未看到打印过的内容。 有2个类似的问题,但是那里的答案并没有满足我的需要: 抓住PyQt中的任何异常:OP希望 监视 异常,不退出偶数循环。 防止P

  • 其中api为: 带有try/catch得客户端用法: 依赖项的版本为: null null ps.当假客户服务可用时,它可以工作,好的。我只关注例外方面。

  • 我在“php油测试”命令的丛林中徘徊了几天。 我在windows7中通过pear安装了phpunit。在安装之后,当我执行“PHPOilTest”命令时,出现以下错误。 未捕获异常油\异常:PHPUnit似乎未安装。 但是,看起来phpUnit安装正确,因为phpUnit--version命令工作正常(请参考以下内容)。 PHPUnit 4.0.14 by Sebastian Bergmann.