我们使用的是spring security 3.0.5,Java 1.6和Tomcat 6.0.32。在我们的.xml配置文件中,我们有:
<form-login login-page="/index.html" default-target-url="/postSignin.html" always-use-default-target="true"
authentication-failure-handler-ref="authenticationFailureHandler"/>
我们的authenticationFailureHandler
定义为:
<beans:bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
<beans:property name="exceptionMappings">
<beans:props>
<beans:prop key="org.springframework.security.authentication.BadCredentialsException">/index.html?authenticationFailure=true</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
Java
@RequestMapping(params={"authenticationFailure=true"}, value ="/index.html")
public String handleInvalidLogin(HttpServletRequest request) {
//... How can I get the username that was used???
// I've tried:
Object username = request.getAttribute("SPRING_SECURITY_LAST_USERNAME_KEY");
Object username = request.getAttribute("SPRING_SECURITY_LAST_USERNAME"); // deprecated
}
因此,我们将所有BadCredentialsExceptions
定向到index.html
和IndexController
。在IndexController
中,我想要获取用于失败的登录尝试的username
。我该怎么做呢?
发布于 2011-12-31 00:46:39
好的,据我所知,答案是非常简单的,没有过多的讨论或记录。
这是我要做的(没有任何配置,只是创建了这个类)……
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
private static final Logger LOG = Logger.getLogger(MyApplicationListener.class);
@Override
public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
Object userName = event.getAuthentication().getPrincipal();
Object credentials = event.getAuthentication().getCredentials();
LOG.debug("Failed login using USERNAME [" + userName + "]");
LOG.debug("Failed login using PASSWORD [" + credentials + "]");
}
}
我不是一个spring安全专家,所以如果有人读了这篇文章,知道我们不应该这样做的原因,或者知道更好的方法,我很乐意听到。
发布于 2014-12-12 03:14:14
我是这样做的:
onAuthenticationFailure
方法的类,该方法以HttpServletRequest
形式接收一个"username"
,其中是我在onAuthenticationFailure
表单中输入的名称。
发布于 2011-12-30 22:19:32
相反,您可以提供自己版本的DefaultAuthenticationEventPublisher并覆盖publishAuthenticationFailure方法。
https://stackoverflow.com/questions/8676206
复制相似问题