5

I googled and tried lot of variants for some hours but without any success. Please help me to find a solution.

Spring version: Spring 3.1

I have login page. Login page depends on URL parameter:

/login?code=client1

or

/login?code=client2

So client1 and client2 has different login pages.

security.xml:

<sec:form-login login-page="/login" default-target-url="/start" authentication-failure-url="/login"/>

So if user make wrong authentication I show him /login page... But point is I have to show login page with corresponding code parameter.

What should I do? Have tyou examples please?

Thanks a lot for advance.

UPDATE #1:

I created FailureHandler class:

public class GRSAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler implements AuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {

}

}

What should I write inside to get redirect to needed URL? Please if you can give me more details.

Thanks a lot!

mad
  • 1,029
  • 3
  • 17
  • 38

1 Answers1

7

You could use the SimpleUrlAuthenticationFailureHandler and implement a different RedirectStrategy which redirect to the configured URL and adds the original query string to the redirected URL.

public class QueryStringPropagateRedirectStrategy extends DefaultRedirectStrategy {

    public void sendRedirect(HttpServletRequest request,
            HttpServletResponse response, String url) throws IOException {
        String urlWithOriginalQueryString = url + "?" + request.getQueryString();
        super.sendRedirect(request, response, urlWithOriginalQueryString );
    }

}


Authentication failure handler configurations

<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="redirectStrategy" ref="queryStringPropagateRedirectStrategy" />
    <property name="defaultFailureUrl" value="/login" />
</bean>

<bean id="queryStringPropagateRedirectStrategy" class="...QueryStringPropagateRedirectStrategy" />
Vladlen Gladis
  • 1,699
  • 5
  • 19
  • 41
LaurentG
  • 11,128
  • 9
  • 51
  • 66
  • See, thanks. And last stupid question: how should I link the strategy to my handler? – mad Jun 19 '13 at 19:49
  • Does easy way to get an auth exception exist? I have different exceptions and different corresponding info to show to user. Should I create my handler and override saveException() method? Thanks again. – mad Jun 19 '13 at 21:02
  • not working on spring-security 4.0.3, request.getQueryString() is always null – SooCheng Koh Apr 20 '16 at 11:14