You need to configure SimpleUrlAuthenticationFailureHandler , for example with the new RedirectStrategy .
Before you start: you should take a look at the source of these classes to understand what they do:
UsernamePasswordAuthenticationFilter and its superclass AbstractAuthenticationProcessingFilter is a filter that triggers authenticationSimpleUrlAuthenticationFailureHandler - this is responsible for doing something if authentication is required (it is called `SimpleUrlAuthenticationFailureHandler).DefaultRedirectStrategy - Used by SimpleUrlAuthenticationFailureHandler to execute redirct.FormLoginBeanDefinitionParser - FormLoginBeanDefinitionParser XML security:form-login element. You must read it to understand how beans are created and referenced.
You should write your own RedirectStrategy , name it MyAppendParameterRedirectStrategy (maybe look at DefaultRedirectStrategy first). It needs only one method: void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) . At least you should do the same as DefaultRedirectStrategy , but instead of returning the login URL to calculateRedirectUrl , you should calculate the url as stripParams(getRequestURL()) + "?login_error=1"
import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.security.web.RedirectStrategy; public class MyAppendParameterRedirectStrategy implements RedirectStrategy { @Override public void sendRedirect(final HttpServletRequest request, final HttpServletResponse response, final String url) throws IOException { String redirectUrl = calculateRedirectUrl(request.getRequestURL().toString()); redirectUrl = response.encodeRedirectURL(redirectUrl); response.sendRedirect(redirectUrl); } private String calculateRedirectUrl(final String requestUrl) {
The second part is that you need to change the spring configuration (so you should read the classes I mentioned) I think the configuration should be like this (but I haven't tested it):
<security:form-login login-processing-url="/login/j_spring_security_check" login-page="/login" authentication-failure-handler-ref="simpleUrlAuthenticationFailureHandler"/> <bean id="SimpleUrlAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <property="defaultFailureUrl" value="NotNullButWeDoNotUseIt" /> <property="redirectStrategy"> <bean class="MyAppendParameterRedirectStrategy"/> </property> </bean>
Ralph source share