繁体   English   中英

返回用户名和密码以登录表单使Spring安全

[英]Return username and password to login form grails spring security

当用户身份验证失败时,我希望将用户名和密码返回到表单。 我正在使用带有Grails和Spring Security LDAP的spring安全核心插件。 我搜索了一段时间,并找到了拉链。 有任何想法吗?

UsernamePasswordAuthenticationFilter javadoc:

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

至于密码,则没有缓存的意义,因为出于安全原因不能将其放回密码字段。

供以后参考,因为上述答案太含糊,以至于对于刚开始学习此框架的我们这些人(对这样的问题提出了以下提示:什么是AuthenticationFailureHandler ?如何实现?)。将其连接到由<security:http>名称空间处理程序神奇创建的现有基础结构?)或不再起作用(将用户名存储在SPRING_SECURITY_LAST_USERNAME的代码已从3.1.0版的UsernamePasswordAuthenticationFilter中删除),还有更多内容关于第一个答案的详细信息:

  • AuthenticationFailureHandler验证失败时,登录过程将使用AuthenticationFailureHandler来决定要执行的操作。
  • <security:http><security:form-login /></security:http>提供的默认登录表单设置使用SimpleUrlAuthenticationFailureHandler来执行对登录失败的url的重定向(默认为/spring_security_login?login_error )。
  • 您可以使用<form-login>元素的authentication-failure-handler-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的答案中所述的相同方法访问失败的登录用户名,但由于框架的更改而不再起作用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM