From 1aad466dcc266a7de7154b1693ee5af2e8447bfb Mon Sep 17 00:00:00 2001 From: Isaries Date: Sun, 15 Nov 2026 18:00:52 +0800 Subject: [PATCH] fix(WISEAuthenticationSuccessHandler): Signing in to a closed site redirects to a page that does not exist When sign-in is disabled, a non-administrator who signs in is signed out again and redirected to /pages/maintenance.html, a page that has never existed in this repository. The sign-in page posts the credentials with XHR, follows the redirect, and cannot read the response, so the person is told that the username or password is wrong. The refusal is now a JSON body in the format WISEAuthenticationFailureHandler already uses, with isLoginDisabled set, and the unused LOGIN_DISABLED_MESSGE_PAGE constant is removed. --- .../WISEAuthenticationProcessingFilter.java | 1 - .../WISEAuthenticationSuccessHandler.java | 14 ++-- .../WISEAuthenticationSuccessHandlerTest.java | 65 +++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandlerTest.java diff --git a/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationProcessingFilter.java b/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationProcessingFilter.java index 1a5ff1268..ba520e05c 100644 --- a/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationProcessingFilter.java +++ b/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationProcessingFilter.java @@ -63,7 +63,6 @@ public class WISEAuthenticationProcessingFilter extends UsernamePasswordAuthenti public static final String TEACHER_DEFAULT_TARGET_PATH = "/teacher"; public static final String ADMIN_DEFAULT_TARGET_PATH = "/admin"; public static final String RESEARCHER_DEFAULT_TARGET_PATH = "/teacher"; - public static final String LOGIN_DISABLED_MESSGE_PAGE = "/pages/maintenance.html"; private static final Log LOGGER = LogFactory.getLog(WISEAuthenticationProcessingFilter.class); diff --git a/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandler.java b/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandler.java index 10624429e..a987d8dc2 100644 --- a/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandler.java +++ b/src/main/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandler.java @@ -23,6 +23,8 @@ */ package org.wise.portal.presentation.web.filters; +import org.json.JSONException; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.security.core.Authentication; @@ -36,6 +38,7 @@ import org.wise.portal.domain.authentication.impl.StudentUserDetails; import org.wise.portal.domain.authentication.impl.TeacherUserDetails; import org.wise.portal.domain.portal.Portal; +import org.wise.portal.presentation.web.controllers.ControllerUtil; import org.wise.portal.service.authentication.AuthorityNotFoundException; import org.wise.portal.service.authentication.UserDetailsService; import org.wise.portal.service.portal.PortalService; @@ -100,7 +103,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo } } - // if user is not admin and login is disallowed, log out user and redirect them to the "we are undergoing maintenance" page + // if user is not admin and login is disallowed, log out user and tell the sign-in page try { Portal portal = portalService.getById(Integer.valueOf(1)); if (!userIsAdmin && !portal.isLoginAllowed()) { @@ -109,14 +112,15 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo new SecurityContextLogoutHandler().logout(request, response, auth); } SecurityContextHolder.getContext().setAuthentication(null); - String contextPath = request.getContextPath(); - response.sendRedirect( - contextPath + WISEAuthenticationProcessingFilter.LOGIN_DISABLED_MESSGE_PAGE); + JSONObject responseJSON = ControllerUtil.createErrorResponse(); + responseJSON.put("isLoginDisabled", true); + response.setContentType("application/json"); + response.getWriter().write(responseJSON.toString()); return; } } catch (ObjectNotFoundException e) { // do nothing - } catch (IOException ioe) { + } catch (IOException | JSONException e) { // do nothing } diff --git a/src/test/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandlerTest.java b/src/test/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandlerTest.java new file mode 100644 index 000000000..f32ec25c0 --- /dev/null +++ b/src/test/java/org/wise/portal/presentation/web/filters/WISEAuthenticationSuccessHandlerTest.java @@ -0,0 +1,65 @@ +package org.wise.portal.presentation.web.filters; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; + +import org.easymock.EasyMock; +import org.json.JSONObject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.mock.env.MockEnvironment; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.test.util.ReflectionTestUtils; +import org.wise.portal.domain.authentication.MutableUserDetails; +import org.wise.portal.domain.authentication.impl.StudentUserDetails; +import org.wise.portal.domain.portal.Portal; +import org.wise.portal.domain.portal.impl.PortalImpl; +import org.wise.portal.service.authentication.UserDetailsService; +import org.wise.portal.service.portal.PortalService; + +class WISEAuthenticationSuccessHandlerTest { + + @AfterEach + void tearDown() { + SecurityContextHolder.clearContext(); + } + + @Test + void signingInToAClosedSite_shouldAnswerLoginDisabled() throws Exception { + Portal portal = new PortalImpl(); + portal.setSettings("{isLoginAllowed:false}"); + PortalService portalService = EasyMock.createMock(PortalService.class); + EasyMock.expect(portalService.getById(1)).andReturn(portal); + UserDetailsService userDetailsService = EasyMock.createMock(UserDetailsService.class); + EasyMock.replay(portalService, userDetailsService); + WISEAuthenticationSuccessHandler handler = new WISEAuthenticationSuccessHandler(); + ReflectionTestUtils.setField(handler, "portalService", portalService); + ReflectionTestUtils.setField(handler, "userDetailsService", userDetailsService); + ReflectionTestUtils.setField(handler, "appProperties", + new MockEnvironment().withProperty("wise.hostname", "http://localhost")); + MutableUserDetails student = new StudentUserDetails(); + student.setUsername("student"); + student.setLanguage("en"); + UsernamePasswordAuthenticationToken authentication = + new UsernamePasswordAuthenticationToken(student, "", List.of()); + SecurityContextHolder.getContext().setAuthentication(authentication); + MockHttpServletRequest request = new MockHttpServletRequest("POST", + "/api/j_acegi_security_check"); + MockHttpServletResponse response = new MockHttpServletResponse(); + + handler.onAuthenticationSuccess(request, response, authentication); + + assertNull(response.getRedirectedUrl()); + JSONObject body = new JSONObject(response.getContentAsString()); + assertEquals("error", body.getString("status")); + assertTrue(body.getBoolean("isLoginDisabled")); + assertNull(SecurityContextHolder.getContext().getAuthentication()); + EasyMock.verify(portalService, userDetailsService); + } +}