如何在登录屏幕上重定向 Spring security 并发会话控制“消息”?

问题描述 投票:0回答:3

我有一个Web应用程序,其中我使用了Spring框架。 对于并发会话控制,我使用了 spring 功能,其中只有 1 个登录会话将为 1 个用户维护,一旦该用户登录到另一个会话,他/她之前的会话将过期。

现在在这种情况下,我收到此消息“此会话已过期(可能是由于同一用户尝试多次并发登录)。

但是我在浏览器的完整白页上收到此消息。我希望此消息仅出现在我的登录屏幕上。

这是我的 spring security xml 的一部分,我在其中处理用户的并发会话。

<security:session-management invalid-session-url="/login.jsp?error=sessionExpired" session-authentication-error-url="/login.jsp?error=alreadyLogin">
                    <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</security:session-management>

任何专门用于自定义此消息并将此消息重定向到所需 Web 应用程序页面的链接将不胜感激。

提前致谢。

java spring jakarta-ee spring-security spring-webflow
3个回答
9
投票

spring-security.xml 中的原始 XML 条目

<security:session-management session-authentication-error-url="/login.jsp?error=alreadyLogin">
                    <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
</security:session-management>

只是您必须在 xml 中添加以下参数来重定向登录过期操作或无效会话 url

expired-url="url 值"

invalid-session-url="url 值"

修改了 XML 条目

<security:session-management invalid-session-url="/login.jsp?error=sessionExpired" session-authentication-error-url="/login.jsp?error=alreadyLogin">
                    <security:concurrency-control max-sessions="1" expired-url="/login.jsp?error=sessionExpiredDuplicateLogin" error-if-maximum-exceeded="false" />
</security:session-management>

1
投票

要在带有 REST API 的 Spring Boot 应用程序中使用此功能,请检查以下代码示例。默认情况下,当会话由于并发登录而过期时,响应代码将为 200。下面的代码示例将其更改为 401。

.sessionManagement(httpSecuritySessionManagementConfigurer -> {
    httpSecuritySessionManagementConfigurer.maximumSessions(1);
    httpSecuritySessionManagementConfigurer.sessionConcurrency(concurrencyControlConfigurer -> {
        concurrencyControlConfigurer.maxSessionsPreventsLogin(false);
        concurrencyControlConfigurer.expiredSessionStrategy(event -> {
            HttpServletResponse response = event.getResponse();
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().print("{\"responseCode\":\"FAILURE\" , \"error\":\"This session has been expired (possibly due to multiple concurrent logins being " +
                    "attempted as the same user).\"}");
            response.flushBuffer();
        });
    });
})

https://docs.spring.io/spring-security/reference/servlet/authentication/session-management.html#ns-concurrent-sessions


0
投票

在 spring-security-core jar 中有 .properties 文件。

您需要使用应用程序中的自定义消息覆盖所需的属性。

© www.soinside.com 2019 - 2024. All rights reserved.