我的要求是,如果用户尝试在会话中使用错误的凭据登录超过3次,我想锁定用户。我想将计数存储在(预先验证的)会话中。
我正在使用表单身份验证
弹簧security.xml文件
<security:form-login login-page="/login" default-target-url="/home" authentication-failure-url="/loginFailed" />
<security:logout logout-success-url="/" />
登录控制器:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login() throws Exception {
return new ModelAndView(UrlRegistry.SIMPLEFI_LOGIN_PAGE);
}
@RequestMapping(value = "/loginFailed", method = RequestMethod.GET)
public ModelAndView loginFailed() {
return new ModelAndView(UrlRegistry.SIMPLEFI_LOGIN_PAGE, "error", "true");
}
来自搜索。我了解了ApplicationListener的使用,但是我无法访问会话对象,因此无法在那里存储计数。
任何人都可以建议如何实施吗?
答案 0 :(得分:2)
您确定要在会话中存储计数吗?攻击者可以通过简单地从请求中省略会话ID来轻松规避这一点。无论如何,如果你不关心那个......
尝试实现自定义AuthenticationFailureHandler
,这是一个定义单个方法的回调接口:onAuthenticationFailure()
。此方法接收请求对象和导致身份验证失败的异常实例,因此它提供了一个理想的扩展点来添加您的功能。
如果您想保持标准行为(将请求重定向到特定页面),请不要忘记包装或扩展默认实现(SimpleUrlAuthenticationFailureHandler
)。
以下是有关如何连接自定义处理程序的一些帮助:
<security:http ...>
...
<security:form-login
authentication-failure-handler-ref="customAFH"
...
/>
</security:http>
<bean id="customAFH" class="CustomAuthenticationFailureHandler">
...
</bean>