use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.
the class AuthenticationRequiredHandlerImplTest method testSendError.
@Test
public void testSendError() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContext context = new RequestContext(request, response, null);
handler.handle(context, new AuthenticationRequiredException(""));
verify(requestCache).saveRequest(request, response);
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
assertTrue(response.isCommitted());
}
use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.
the class AuthenticationRequiredHandlerImplTest method testRedirectToLoginFormUrl.
@Test
public void testRedirectToLoginFormUrl() throws Exception {
handler.setLoginFormUrl(LOGIN_FORM_URL);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContext context = new RequestContext(request, response, null);
handler.handle(context, new AuthenticationRequiredException(""));
verify(requestCache).saveRequest(request, response);
assertEquals(LOGIN_FORM_URL, response.getRedirectedUrl());
assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
assertTrue(response.isCommitted());
}
use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.
the class SecurityExceptionProcessor method handleAccessDeniedException.
/**
* Handles the specified {@link AccessDeniedException}, by calling the {@link AccessDeniedHandler}.
*/
protected void handleAccessDeniedException(RequestContext context, AccessDeniedException e) throws SecurityProviderException, IOException {
Authentication auth = SecurityUtils.getAuthentication(context.getRequest());
// If user is anonymous, authentication is required
if (auth == null) {
try {
// Throw ex just to initialize stack trace
throw new AuthenticationRequiredException("Authentication required to access the resource", e);
} catch (AuthenticationRequiredException ae) {
logger.debug("Authentication is required", ae);
authenticationRequiredHandler.handle(context, ae);
}
} else {
logger.debug("Access denied to user '" + auth.getProfile().getUsername() + "'", e);
accessDeniedHandler.handle(context, e);
}
}
use of org.craftercms.security.exception.AuthenticationRequiredException in project profile by craftercms.
the class RestAuthenticationRequiredHandlerTest method testHandle.
@Test
public void testHandle() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/profile.json");
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContext context = new RequestContext(request, response, null);
handler.handle(context, new AuthenticationRequiredException(ERROR_MESSAGE));
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}
Aggregations