春季安全3.1.使用url参数自定义authentication-failure-url

mad*_*mad 5 java spring-security

我用谷歌搜索并尝试了很多变种,但没有任何成功.请帮我找一个解决方案.

春季版:春季3.1

我有登录页面.登录页面取决于URL参数:

/login?code=client1
Run Code Online (Sandbox Code Playgroud)

要么

/login?code=client2
Run Code Online (Sandbox Code Playgroud)

因此client1和client2具有不同的登录页面.

security.xml文件:

<sec:form-login login-page="/login" default-target-url="/start" authentication-failure-url="/login"/>
Run Code Online (Sandbox Code Playgroud)

因此,如果用户进行了错误的身份验证,我会向他显示/登录页面...但是我必须显示具有相应代码参数的登录页面.

我该怎么办?有tyou的例子吗?

非常感谢您的进步.

更新#1:

我创建了FailureHandler类:

public class GRSAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler implements AuthenticationFailureHandler {

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

}

}
Run Code Online (Sandbox Code Playgroud)

我应该在里面写什么来重定向到所需的URL?如果你能给我更多细节,请.

非常感谢!

Lau*_*ntG 6

您可以使用SimpleUrlAuthenticationFailureHandler并实现不同的RedirectStrategy重定向到配置的URL,并将原始查询字符串添加到重定向的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 );
    }

}
Run Code Online (Sandbox Code Playgroud)


验证失败处理程序配置

<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" />
Run Code Online (Sandbox Code Playgroud)