LoginSAMLAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { @Override public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException exception) throws IOException, ServletException { String redirectTo = null; if (exception instanceof LoginSAMLException) { HttpSession session = request.getSession(); if (session != null) { DefaultSavedRequest savedRequest = (DefaultSavedRequest) SessionUtils.getSavedRequestSession(session); if (savedRequest != null) { String[] redirectURI = savedRequest.getParameterMap().get("redirect_uri"); if (redirectURI != null && redirectURI.length > 0) { URI uri = URI.create(redirectURI[0]); URIBuilder uriBuilder = new URIBuilder(uri); uriBuilder.addParameter("error", "access_denied"); uriBuilder.addParameter("error_description", exception.getMessage()); redirectTo = uriBuilder.toString(); if (LOG.isDebugEnabled()) { LOG.debug("Error redirect to: " + redirectTo); } getRedirectStrategy().sendRedirect(request, response, redirectTo); } } } } if (redirectTo == null) { Throwable cause = exception.getCause(); if (cause != null) { AuthenticationException e = new AuthenticationServiceException(cause.getMessage(), cause.getCause()); logger.debug(cause); super.onAuthenticationFailure(request, response, e); } else { logger.debug(exception); super.onAuthenticationFailure(request, response, exception); } } }  @Override void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response,
            final AuthenticationException exception);  }
@Test public void testErrorRedirect() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); DefaultSavedRequest savedRequest = mock(DefaultSavedRequest.class); Map<String, String[]> parameterMap = new HashMap<String, String[]>(); parameterMap.put("redirect_uri", new String[] { "https: when(savedRequest.getParameterMap()).thenReturn(parameterMap); MockHttpSession session = new MockHttpSession(); SessionUtils.setSavedRequestSession(session, savedRequest); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); MockHttpServletResponse response = new MockHttpServletResponse(); LoginSAMLException exception = new LoginSAMLException("Denied!"); handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertEquals("https: int status = response.getStatus(); assertEquals(302, status); }
@Test public void testErrorRedirectWithExistingQueryParameters() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); DefaultSavedRequest savedRequest = mock(DefaultSavedRequest.class); Map<String, String[]> parameterMap = new HashMap<String, String[]>(); parameterMap.put("redirect_uri", new String[] { "https: when(savedRequest.getParameterMap()).thenReturn(parameterMap); MockHttpSession session = new MockHttpSession(); SessionUtils.setSavedRequestSession(session, savedRequest); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); MockHttpServletResponse response = new MockHttpServletResponse(); LoginSAMLException exception = new LoginSAMLException("Denied!"); handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertEquals("https: int status = response.getStatus(); assertEquals(302, status); }
@Test public void testSomeOtherErrorCondition() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); DefaultSavedRequest savedRequest = mock(DefaultSavedRequest.class); Map<String, String[]> parameterMap = new HashMap<String, String[]>(); parameterMap.put("redirect_uri", new String[] { "https: when(savedRequest.getParameterMap()).thenReturn(parameterMap); MockHttpSession session = new MockHttpSession(); SessionUtils.setSavedRequestSession(session, savedRequest); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); MockHttpServletResponse response = new MockHttpServletResponse(); AuthenticationException exception = new AuthenticationException("Authentication Exception") { private static final long serialVersionUID = 1L; }; handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertNull(actual); int status = response.getStatus(); assertEquals(401, status); }
@Test public void testNoSession() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); LoginSAMLException exception = new LoginSAMLException("Denied!"); handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertNull(actual); int status = response.getStatus(); assertEquals(401, status); }
@Test public void testNoSavedRequest() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); DefaultSavedRequest savedRequest = mock(DefaultSavedRequest.class); Map<String, String[]> parameterMap = new HashMap<String, String[]>(); parameterMap.put("redirect_uri", new String[] { "https: when(savedRequest.getParameterMap()).thenReturn(parameterMap); MockHttpSession session = new MockHttpSession(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); MockHttpServletResponse response = new MockHttpServletResponse(); LoginSAMLException exception = new LoginSAMLException("Denied!"); handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertNull(actual); int status = response.getStatus(); assertEquals(401, status); }
@Test public void testNoRedirectURI() throws IOException, ServletException { LoginSAMLAuthenticationFailureHandler handler = new LoginSAMLAuthenticationFailureHandler(); DefaultSavedRequest savedRequest = mock(DefaultSavedRequest.class); Map<String, String[]> parameterMap = new HashMap<String, String[]>(); when(savedRequest.getParameterMap()).thenReturn(parameterMap); MockHttpSession session = new MockHttpSession(); SessionUtils.setSavedRequestSession(session, savedRequest); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); MockHttpServletResponse response = new MockHttpServletResponse(); LoginSAMLException exception = new LoginSAMLException("Denied!"); handler.onAuthenticationFailure(request, response, exception); String actual = response.getRedirectedUrl(); assertNull(actual); int status = response.getStatus(); assertEquals(401, status); }