Java 如何在身份验证失败url中包含SPRING\u SECURITY\u LAST\u用户名?
我的Spring安全XML中包含以下内容:Java 如何在身份验证失败url中包含SPRING\u SECURITY\u LAST\u用户名?,java,spring,authentication,spring-mvc,spring-security,Java,Spring,Authentication,Spring Mvc,Spring Security,我的Spring安全XML中包含以下内容: <form-login login-page="/login" authentication-failure-url="/login/failure" /> 我想在身份验证失败时获取用户名。 我在网上看到,它可以从SPRING\u SECURITY\u LAST\u用户名获得,但我的问题是: 我如何在以下位置访问它: 1) spring安全XML,例如 身份验证失败url=“/login/failure/SP
<form-login login-page="/login"
authentication-failure-url="/login/failure" />
我想在身份验证失败时获取用户名。
我在网上看到,它可以从SPRING\u SECURITY\u LAST\u用户名获得,但我的问题是:
我如何在以下位置访问它:
1) spring安全XML,例如
身份验证失败url=“/login/failure/SPRING\u SECURITY\u LAST\u USERNAME”
2) 或者处理/登录/故障映射的控制器
请帮忙。我卡住了!:(它已被弃用:
/**
* @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中不合适的字符!)
豆子:
你可以很容易地实施策略。只需将用户名保存为onAuthenticationFailure
方法中的会话属性之一,而不是将用户名添加到URL中
您可以轻松设置自己的处理程序:
属性
身份验证失败处理程序ref
可作为身份验证失败url的替代方案,提供
您可以在身份验证后完全控制导航流
失败。该值应该是
应用程序上下文中的AuthenticationFailureHandler bean
文档中的更多信息:如果您想使用1)策略,请记住用户输入可能包含一些无效字符,这些字符不能在URL中。空用户名?使用我的答案,你可以很容易地实现2)。非常感谢,@Maciej!!这帮我省去了很多麻烦&你的建议和代码很有魅力。你真是太棒了,因为像你这样了不起的人,我非常喜欢你。有很多东西值得我们学习。再次感谢你,伙计!
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>