返回用户名和密码以登录grails spring security表单

返回用户名和密码以登录grails spring security表单,grails,spring-security,Grails,Spring Security,当用户身份验证失败时,我希望将用户名和密码返回到表单中。 我将SpringSecurityCore插件与Grails和SpringSecurityLDAP一起使用。我找了一段时间,找到了拉链。有什么想法吗?来自UsernamePasswordAuthenticationFilterjavadoc: 如果要保留用户名,请将其缓存在自定义的AuthenticationFailureHandler中 至于密码,缓存它没有意义,因为出于安全原因,无法将其放回表单密码字段。我可以执行以下操作将用户名恢复到

当用户身份验证失败时,我希望将用户名和密码返回到表单中。
我将SpringSecurityCore插件与Grails和SpringSecurityLDAP一起使用。我找了一段时间,找到了拉链。有什么想法吗?

来自
UsernamePasswordAuthenticationFilter
javadoc:

如果要保留用户名,请将其缓存在自定义的AuthenticationFailureHandler中


至于密码,缓存它没有意义,因为出于安全原因,无法将其放回表单密码字段。

我可以执行以下操作将用户名恢复到表单中:在LoginController.groovy中:

        render view: view, model: [postUrl: postUrl,
                               rememberMeParameter: config.rememberMe.parameter,
                               lastUsername: request.getSession().getAttribute("SPRING_SECURITY_LAST_USERNAME")]

作为将来的参考,上述答案要么太模糊,对我们这些刚刚开始第一次学习这个框架的人没有帮助(提示这样的问题:什么是
AuthenticationFailureHandler
?如何实现一个?如何将其连接到由
命名空间处理程序神奇地创建的现有基础结构?)或不再工作(自3.1.0版起,用于在
SPRING\u SECURITY\u LAST\u username
中存储用户名的代码已从
UsernamePasswordAuthenticationFilter
中删除),下面是关于第一个答案的更多细节:

  • 登录过程使用
    AuthenticationFailureHandler
    来决定在身份验证失败时要做什么
  • 提供的默认登录表单设置使用来执行对登录失败url的重定向(默认为
    /spring\u security\u login?login\u error
  • 您可以通过使用
    元素的
    身份验证失败处理程序ref
    属性来钩住自己的实现
因此,我的实现如下所示:

public class UsernameStoringUrlAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler
{
    @Override
    public void onAuthenticationFailure (HttpServletRequest request, HttpServletResponse response,
            AuthenticationException exception) throws IOException, ServletException
    {
        request.getSession (true).setAttribute ("SPRING_SECURITY_LAST_USERNAME", request.getParameter ("j_username"));
        super.onAuthenticationFailure (request, response, exception);
    }
}
其配置如下:

    <security:form-login authentication-failure-handler-ref="authenticationFailureHandler" [...] />
    ...
<bean id="authenticationFailureHandler" class="my.package.UsernameStoringUrlAuthenticationFailureHandler" p:defaultFailureUrl="/LoginError" />

...

然后我可以使用James Kleeh在这里的回答中描述的相同方法访问失败的登录用户名,但由于框架的更改,该方法不再有效。

此外,这在大多数网站上都不是很常见。事实上,我越来越多地看到它,删除最后一个字符y非常方便你不小心键入了
SPRING\u SECURITY\u LAST\u USERNAME
已被弃用。你能给我举一些例子吗?我正在为如何做到这一点而苦苦挣扎。只需实现
AuthenticationFailureHandler
接口并配置SPRING SECURITY来使用它。这仍然会留下许多问题没有答案。举个例子就好了。H你试过了吗?什么不起作用?不管怎样,对我来说这似乎是另一个问题。