Return username and password to login form grails spring security

情到浓时终转凉″ 提交于 2019-12-28 06:53:45

问题


When a user fails authentication, I want the username and password to be returned to the form. I'm using the spring security core plugin with Grails and Spring Security LDAP. I've searched around for a while and have come up with zip. Any ideas?


回答1:


From UsernamePasswordAuthenticationFilter javadoc:

If you want to retain the username, cache it in a customized AuthenticationFailureHandler

As for password there is no point to cache it, because it cannot be put back to form password field for security reasons.




回答2:


For future reference, as the above answers are either too vague to be helpful to those of us who are just beginning to learn this framework for the first time (prompting such questions as: what's an AuthenticationFailureHandler? How do I implement one? How do I connect it to my existing infrastructure that was magically created by the <security:http> namespace handler?) or no longer work (the code to store the username in SPRING_SECURITY_LAST_USERNAME was removed from UsernamePasswordAuthenticationFilter as of version 3.1.0), here's a little more detail on the first answer:

  • An AuthenticationFailureHandler is used by the login process to decide what to do when authentication fails.
  • The default login form setup as provided by <security:http><security:form-login /></security:http> uses a SimpleUrlAuthenticationFailureHandler to perform the redirection to the login failed url (which defaults to /spring_security_login?login_error).
  • You can hook your own implementation in by using the authentication-failure-handler-ref attribute of your <form-login> element.

So, my implementation looks like this:

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);
    }
}

which is configured thus:

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

And then I can access the failed login username using the same approach as described in James Kleeh's answer here, but which no longer worked because of the change to the framework.




回答3:


I was able to do the following to get the username back to the form: In LoginController.groovy:

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


来源:https://stackoverflow.com/questions/13035995/return-username-and-password-to-login-form-grails-spring-security

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!