我們使用spring security 3.0.5,Java 1.6和Tomcat 6.0.32。在我們的.xml配置文件中,我們已經有了:如何使用spring security從失敗登錄中獲取用戶名?
<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
。我怎樣才能做到這一點?
我試過了,但它不起作用 – 2012-06-06 08:33:44
我們有這個確切的代碼在我們的設置中工作。我無法告訴你爲什麼它不適合你。我們不必配置任何新的東西,也不需要在任何地方更改任何.xml文件。註釋似乎指向這個代碼的春天,並在出現不良登錄時執行它。它對我們來說非常合適。 – kasdega 2012-06-06 13:35:57
也許它需要一些其他配置。順便說一下,我找到了另一種解決方案。感謝您的信息,雖然:) – 2012-06-07 02:22:40