Spring Security:如何重置SPRING_SECURITY_LAST_EXCEPTION.message?

时间:2010-10-17 15:00:09

标签: spring jsp login spring-security

当用户尝试使用不正确的凭据登录时,我能够显示SPRING_SECURITY_LAST_EXCEPTION.message(“Bad Credentials”)。

我的登录jsp目前使用以下代码:

<c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION.message}">
    <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>

我的问题是,当用户离开登录页面然后返回时,“Bad Credentials”消息仍然存在。

当用户刷新登录页面时,如何重置SPRING_SECURITY_LAST_EXCEPTION.message

2 个答案:

答案 0 :(得分:16)

典型方法是仅在登录失败后显示错误消息,其中登录失败由请求参数确定。也就是说,将Spring Security配置为

<form-login ... authentication-failure-url = "/login?error=1" />

并将错误消息显示为

<c:if test="${not empty param['error']}"> 
    <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" /> 
</c:if> 

但是,由于SPRING_SECURITY_LAST_EXCEPTION是会话属性,我猜您可以使用以下方法重置它:

<c:remove var = "SPRING_SECURITY_LAST_EXCEPTION" scope = "session" />

答案 1 :(得分:0)

对我来说,这样更容易

<c:if test="${param.error != null}">
    <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>

所以你不用担心删除变量和更改默认 URL,如果有一些错误,它会被打印为 URL 被定义为参数 ?error 并且会打印异常(如果你手动传递参数错误什么都不会发生,因为异常不存在)。