捕获AuthenticationProvider中引发的异常

cho*_*hom 5 java spring spring-security spring-boot

我正在实现自定义“ 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;
}
Run Code Online (Sandbox Code Playgroud)

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

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

当在'authenticate'函数中引发异常时,不会调用全局异常处理程序。对于所有其他异常,它被调用。我想在全局异常处理程序中捕获异常并返回自定义错误消息。我怎样才能做到这一点?任何帮助表示赞赏。提前致谢。

cha*_*luo 3

GlobalExceptionHandler用于控制器异常处理程序,但AuthenticationProvider仍然是在过滤器中,如果你想处理它AuthenticationException,你需要处理它来实现AuthenticationEntryPoint和重写commence方法。

public void commence(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException authException) throws IOException, ServletException
Run Code Online (Sandbox Code Playgroud)

AuthenticationException并且AccessDeniedException已经被处理了ExceptionTranslationFilter。你只需要注入AuthenticationEntryPointand AccessDeniedHandler(哪个句柄AccessDeniedException

或者您可以在过滤器中捕获这些异常,然后在文件管理器中处理它,AuthenticationFailureHandler例如AbstractAuthenticationProcessingFilter