【问题标题】:How to include SPRING_SECURITY_LAST_USERNAME in authentication failure url?如何在身份验证失败 url 中包含 SPRING_SECURITY_LAST_USERNAME?
【发布时间】:2012-11-13 00:50:32
【问题描述】:

我的 Spring Security XML 中有以下内容:

<form-login login-page="/login" 
            authentication-failure-url="/login/failure" />

我想在身份验证失败时获取用户名。 我在网上看到它可以从 SPRING_SECURITY_LAST_USERNAME 获得,但我的问题是:

我如何访问它: 1) spring security XML 例如像

authentication-failure-url="/login/failure/SPRING_SECURITY_LAST_USERNAME"

2) 或处理 /login/failure 映射的控制器。

请帮忙。我被困住了! :(

【问题讨论】:

  • 如果您想使用 1) 策略,请记住用户输入可以包含一些不能在 URL 内的无效字符。空用户名?使用我的答案,您可以轻松实现 2)。

标签: java spring authentication spring-mvc spring-security


【解决方案1】:

已弃用:

/**
  * @deprecated If you want to retain the username, cache it in a customized {@code AuthenticationFailureHandler}
  */
@Deprecated
public static final String SPRING_SECURITY_LAST_USERNAME_KEY = "SPRING_SECURITY_LAST_USERNAME";

所以你需要实现自己的AuthenticationFailureHandler

我实施了 1) 策略: (请记住,用户名可以包含一些在 URL 中不合适的字符!)

package some.package;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UsernameInURLAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    private String urlPrefix;
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    private String formUsernameKey = UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY;

    public UsernameInURLAuthenticationFailureHandler(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

    //Failure logic:
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //We inherited that method:
        saveException(request, exception);

        //Prepare URL:
        String username = request.getParameter(formUsernameKey);
        String redirectUrl = urlPrefix + username;

        //Redirect:
        redirectStrategy.sendRedirect(request, response, redirectUrl);
    }

    //Getters and setters:
    public String getUrlPrefix() {
        return urlPrefix;
    }

    public void setUrlPrefix(String urlPrefix) {
        this.urlPrefix = urlPrefix;
    }

    public String getFormUsernameKey() {
        return formUsernameKey;
    }

    public void setFormUsernameKey(String formUsernameKey) {
        this.formUsernameKey = formUsernameKey;
    }

    public RedirectStrategy getRedirectStrategy() {
        return redirectStrategy;
    }

    public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
        this.redirectStrategy = redirectStrategy;
    }
}

豆子:

<bean class="some.package.UsernameInURLAuthenticationFailureHandler">
        <!-- prefix: -->
        <constructor-arg value="/login/failure/"/>
</bean>

您可以轻松实施 2) 策略。只需将用户名保存为 onAuthenticationFailure 方法中的会话属性之一,而不是将用户名添加到 URL。

您可以轻松设置自己的处理程序:

&lt;form-login&gt; 属性

authentication-failure-handler-ref

可以用作 authentication-failure-url 的替代方案,给出 您在身份验证后完全控制导航流程 失败。该值应该是他的名字 应用程序上下文中的 AuthenticationFailureHandler bean。

更多文档:CLICK

【讨论】:

  • 非常感谢,@Maciej !它让我省了很多麻烦,你的建议和代码就像魅力一样。你太棒了,因为像你这样优秀的人,我非常喜欢。有很多东西可以学习。再次感谢,伙计!
猜你喜欢
  • 2014-01-24
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多