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>
ジャワ
@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
失敗したログイン試行のために使用されたことを。これどうやってするの?
わかりましたので、答えは非常に単純ですが、私が知る限りでは、あまり議論も文書化もされていません。
ここに私がしなければならなかったすべてがあります(このクラスを作成したところに設定はありません)...
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 + "]");
}
}
私は春のセキュリティ専門家とはほど遠いので、誰かがこれを読んで、私たちがこのようにしてはいけない理由を知っているか、私がそれについて聞きたいと思うより良い方法を知っています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加