【问题标题】:Ask user to change their password after their password expires, but not during their session要求用户在密码过期后更改密码,但不在会话期间
【发布时间】:2015-04-20 02:38:51
【问题描述】:

目前,我的应用程序的逻辑是,当用户的密码在 30 天后过期时,即使用户正在做某事,他们也会被重定向到更改密码屏幕。这是错误的。仅应在下次登录时提示用户更改密码。

我创建了一个 CheckAfterLoginFilter,它扩展了逻辑所在的 OncePerRequestFilter。但是,这会在每个请求上进行过滤,以便用户在会话中注销。如果可能的话,我不确定如何在这里实现所需的逻辑。

我的登录表单 jsp 使用 j_security_check。我的第一个想法是将逻辑从 CheckAfterLoginFilter 移动到 LoginController 但 j_security_check 似乎重定向到它自己的东西,我不知道或在哪里可以找到。

我们将不胜感激!

谢谢

【问题讨论】:

    标签: java spring spring-security passwords j-security-check


    【解决方案1】:

    当您使用 Spring Security 时,我假设您已经配置了 authenticationManager,并且您的 UserEntity 实现了 UserDetails

    我的建议是提供自定义身份验证失败处理程序并在您的 UserEntity 中覆盖 isCredentialsNonExpired()

    这是一个示例(使用基于 java 的配置)。

    自定义身份验证失败提供程序

    @Bean
    public AuthenticationFailureHandler customAuthenticationFailureHandler() {
        ExceptionMappingAuthenticationFailureHandler exceptionMappingAuthenticationFailureHandler =
                new ExceptionMappingAuthenticationFailureHandler();
        Map<Object, Object> map = new HashMap<>();
        map.put(
                "org.springframework.security.authentication.CredentialsExpiredException",
                "/resetPassword.html"
        );        
    
        exceptionMappingAuthenticationFailureHandler.setExceptionMappings(map);
    
        exceptionMappingAuthenticationFailureHandler.setRedirectStrategy(
                new RedirectStrategy() {
                    @Override
                    public void sendRedirect(
                            HttpServletRequest request, HttpServletResponse response, String url
                    ) throws IOException {
                        response.sendRedirect(request.getContextPath() + url);
                    }
                }
        );
    
        return exceptionMappingAuthenticationFailureHandler;
    }
    

    XML方式

    <bean id="customAuthenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.security.authentication.CredentialsExpiredException">/change_password_page</prop>
            </props>
        </property>
        <property name="defaultFailureUrl" value="/resetPassword"/>
    </bean>
    

    在你的 security.xml 中

    <security:form-login ... authentication-failure-handler-ref="customAuthenticationFailureHandler">
    

    最后在你的UserEntity

        @Override
        public boolean isCredentialsNonExpired() {
            if (// check password is expired or not) {
               return false;
            }
            return true;
         }
    

    所以当密码过期时,失败处理程序将重定向到您想要的页面。

    【讨论】:

      【解决方案2】:

      在签入 servlet 过滤器时允许到期日期为 31 天,但在登录时仅为 30 天。

      因此,当它在 30 天后命中 servlet 过滤器时,它将继续运行直到另一天,但如果他们在 31 天之前没有更改密码,则无论如何你都希望他们被注销。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-18
        • 2016-08-30
        • 2015-07-09
        • 2013-07-06
        • 1970-01-01
        相关资源
        最近更新 更多