【问题标题】:get userid on Controller classes when hit ExceptionMappingAuthenticationFailureHandler命中 ExceptionMappingAuthenticationFailureHandler 时获取控制器类的用户 ID
【发布时间】:2014-03-06 11:51:38
【问题描述】:

我已经设置了我的 spring 安全性,如下所示。我想在这些控制器类中获取用户 ID,以便在凭据错误时更新失败计数,或者我想将其重定向到重置密码页面,密码已过期。

提前致谢。

<beans:bean id="exceptionTranslationFilter" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
    <beans:property name="exceptionMappings">
        <beans:props>           
            <beans:prop key="org.springframework.security.authentication.BadCredentialsException">/login_error</beans:prop>
            <beans:prop key="org.springframework.security.authentication.CredentialsExpiredException">/password_expired</beans:prop>
            <beans:prop key="org.springframework.security.authentication.LockedException">/locked</beans:prop>
            <beans:prop key="org.springframework.secuirty.authentication.DisabledException">/disabled</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<http use-expressions="true">
    <form-login login-page="/login" default-target-url="/" authentication-failure-handler-ref="exceptionTranslationFilter" />
</http>

【问题讨论】:

    标签: java spring-mvc spring-security


    【解决方案1】:

    还有一个ApplicationListener可以添加监听BadCredentials事件,看这个post

    已编辑: 添加 AuthenticationSuccessHandler 怎么样,看到这个例子:Redirect to different pages after Login with Spring Security

    【讨论】:

    • 是的,我已经看到了。它适用于 BadCredentialsExceptionLockedException。但是当它击中CredentialsExpiredException。我希望它重定向到更改密码页面,这不能在 ApplicationListener 中完成
    • 那么添加AuthenticationSuccessHandler怎么样?链接已添加到编辑后的回复中
    • 但那是不对的..因为使用是经过身份验证的..并且用户将能够通过 URL 转到其他页面...
    • 再说明一下,好像如果实现了AuthenticationSuccessHandler,是可以设置“authentication.setAuthenticated(false);”的在 onAuthenticationSuccess 方法中,在重定向到 changePassword 页面之前。这似乎能够阻止用户访问其他经过身份验证的页面。它还假设 changePassword 页面不应该要求用户已经过身份验证。这似乎对我有用,但还没有看到任何其他确认这种方法的帖子。并听取其他响应者建议最佳实践/方法。
    • 谢谢..我想如果没有其他响应者,我想这将是我能想到的最好的解决方法。
    猜你喜欢
    • 1970-01-01
    • 2022-12-05
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    相关资源
    最近更新 更多