Spring Security:根据某些条件重定向记录的用户

时间:2013-10-23 16:52:49

标签: java spring spring-mvc spring-security

我是使用Spring security 3的新手。

我想这样做:当用户登录时,我希望系统验证用户是否已确认其电子邮件地址,以及用户是否已配置其帐户配置文件。

我不知道该怎么做。

我试过了:

<http use-expressions="true" auto-config="true">
    <intercept-url ... />
    ...
    <custom-filter after="SECURITY_CONTEXT_FILTER" ref="usrFilter" />
    ...
 </http>

 <b:bean id="usrFilter"
    class="com.zxxztech.zecure.security.MyAuthenticationFilter">
    <b:property name="authenticationManager" ref="authenticationManager" />
    <b:property name="failureHandler">
        <b:bean
            class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
            <b:property name="exceptionMappings">
                <b:map>
                    <b:entry key="org.springframework.security.authentication.DisabledException" value="/disabled.htm" />
                </b:map>
            </b:property>
        </b:bean>
    </b:property>
 </b:bean>

这是我的过滤器:

public class MyAuthenticationFilter extends GenericFilterBean {
  ...
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null) {
            Usuario usuario=(Usuario) authentication.getPrincipal();
            if (usuario.getActivationKey()!=null) {
                ((HttpServletResponse) response).sendRedirect("/activacion");
                return;
            } else if (authentication.getAuthorities().contains(AppRole.NUEVO_USUARIO)) {
                ((HttpServletResponse)response).sendRedirect("/configuracion_modelo");
                return;
            }
        }

        chain.doFilter(request, response);
    }

    ...
}

但是,当我逐步调试应用程序和loggin时,过滤器被无限调用,就像它在循环中一样。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:6)

您需要在要重定向到的网址上继续过滤器链。例如:

import org.springframework.security.web.util.UrlUtils;

public class MyAuthenticationFilter extends GenericFilterBean {
  ...
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null) {
            String currentUrl = UrlUtils.buildRequestUrl((HttpServletRequest) request);
            Usuario usuario=(Usuario) authentication.getPrincipal();
            if("/activacion".equals(currentUrl) || "/configuracion_modelo".equals(currentUrl)) {
                chain.doFilter(request, response);
                return;
            } else if (usuario.getActivationKey()!=null) {
                ((HttpServletResponse) response).sendRedirect("/activacion");
                return;
            } else if (authentication.getAuthorities().contains(AppRole.NUEVO_USUARIO)) {
                ((HttpServletResponse)response).sendRedirect("/configuracion_modelo");
                return;
            }
        }

        chain.doFilter(request, response);
    }

    ...
}