如果 maxSessionsPreventsLogin 设置为 true 并且某些使用我的登录凭据的用户尝试登录,如何在我的 thymelaf 登录页面中显示错误?
Posted
技术标签:
【中文标题】如果 maxSessionsPreventsLogin 设置为 true 并且某些使用我的登录凭据的用户尝试登录,如何在我的 thymelaf 登录页面中显示错误?【英文标题】:how to show error in my thymelaf login page if maxSessionsPreventsLogin set to true and some user with my login credentials try to log in? 【发布时间】:2017-10-08 17:25:03 【问题描述】:我正在使用 spring security 4。场景是我希望一个用户一次登录。也就是说,如果我的登录凭据知道某人并且我已经登录,但是除非并且直到我注销,否则某人应该无法登录,我通过设置成功实现了 maximumSessions(1) 和 maxSessionsPreventsLogin(true) 。
我的要求是如果有人使用我的登录凭据尝试登录,即使我已经登录。我想显示错误消息,例如用户已经在我的 thymeaf 登录页面中登录..
在我的情况下,如果有人使用我的登录凭据尝试登录,即使我已经登录,它也会显示无效的登录名或密码错误。
我想显示自定义错误消息,例如用户已在我的 thyemeaf 登录页面中登录。 我怎么能实现呢? 注意:- 我正在使用自定义登录成功处理程序和自定义注销成功处理程序
【问题讨论】:
【参考方案1】:@Component("customAuthenticationFailureHandler")
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException
System.out.println("inside custom authentication failure handler");
if (exception.getClass().isAssignableFrom(BadCredentialsException.class))
response.sendRedirect("/login?error=true");
else if (exception.getClass().isAssignableFrom(DisabledException.class))
response.sendRedirect("/login?account=disabled");
else if (exception.getClass().isAssignableFrom(SessionAuthenticationException.class))
response.sendRedirect("/login?status=loggedin");
【讨论】:
以上是关于如果 maxSessionsPreventsLogin 设置为 true 并且某些使用我的登录凭据的用户尝试登录,如何在我的 thymelaf 登录页面中显示错误?的主要内容,如果未能解决你的问题,请参考以下文章