package amu.zhcet.auth.login.handler;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@NoArgsConstructor
public class UsernameAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    public static final String USERNAME = UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY;

    public UsernameAuthenticationFailureHandler(String failureUrl) {
        super(failureUrl);
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        request.getSession().setAttribute(USERNAME, request.getParameter(USERNAME));
        super.onAuthenticationFailure(request, response, exception);
    }
}

@@ -1043,16 +1043,110 @@
 ption %7B%0A
+        String username = request.getParameter(USERNAME);%0A        if (username != null) %7B%0A    
         
@@ -1193,40 +1193,35 @@
 ME, 
-request.getParameter(USERNAME));
+username.trim());%0A        %7D
 %0A   

Trim the username on auth failure (#145)