春季安全3.1。使用url参数自定义authentication-failure-url [英] spring security 3.1. custom authentication-failure-url with url parameters
问题描述
我用Google搜索并尝试了很多变种,但没有任何成功。
请帮我找一个解决方案。
I googled and tried lot of variants for some hours but without any success. Please help me to find a solution.
春季版:春季3.1
我已登录页。登录页面取决于URL参数:
I have login page. Login page depends on URL parameter:
/login?code=client1
或
/login?code=client2
因此client1和client2有不同的登录页面。
So client1 and client2 has different login pages.
security.xml:
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?
非常感谢提前。
UPDATE#1:
我创建了FailureHandler类:
I created FailureHandler class:
public class GRSAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {
}
}
我应该在里面写什么来重定向到所需的URL?
如果你能给我更多详细信息请。
What should I write inside to get redirect to needed URL? Please if you can give me more details.
非常感谢!
推荐答案
您可以使用 SimpleUrlAuthenticationFailureHandler
并实现不同的 RedirectStrategy
,它重定向到配置的URL并将原始查询字符串添加到重定向的URL。
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" />
这篇关于春季安全3.1。使用url参数自定义authentication-failure-url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!