java 弹簧安全 3.1。带有 url 参数的自定义身份验证失败 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17199423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:17:17  来源:igfitidea点击:

spring security 3.1. custom authentication-failure-url with url parameters

javaspring-security

提问by mad

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

春季版本:Spring 3.1

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

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

/login?code=client1

or

或者

/login?code=client2

So client1 and client2 has different login pages.

所以client1和client2有不同的登录页面。

security.xml:

安全.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.

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

What should I do? Have tyou examples please?

我该怎么办?请问你有例子吗?

Thanks a lot for advance.

非常感谢提前。

UPDATE #1:

更新#1:

I created FailureHandler class:

我创建了 FailureHandler 类:

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.

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

Thanks a lot!

非常感谢!

回答by LaurentG

You could use the SimpleUrlAuthenticationFailureHandlerand implement a different RedirectStrategywhich redirect to the configured URL and adds the original query string to the redirected URL.

您可以使用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 );
    }

}


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" />