gpt4 book ai didi

java - 如何在身份验证失败 url 中包含 SPRING_SECURITY_LAST_USERNAME?

转载 作者:搜寻专家 更新时间:2023-11-01 01:45:04 26 4
gpt4 key购买 nike

我的 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 映射的 Controller 。

请帮忙。我卡住了! :(

最佳答案

已弃用:

/**
* @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 类:

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

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

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

<form-login> Attributes

authentication-failure-handler-ref

Can be used as an alternative to authentication-failure-url, giving you full control over the navigation flow after an authentication failure. The value should be he name of an AuthenticationFailureHandler bean in the application context.

更多文档:CLICK

关于java - 如何在身份验证失败 url 中包含 SPRING_SECURITY_LAST_USERNAME?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com