nashuald
nashuald

Reputation: 825

Spring Security: redirect logged user depending on some conditions

I am new using spring security 3.

I want to do following: When users login, I want system to verify whether user has confirmed its email address, and whether user have configured its account profile.

I don't know exactly how to that.

I tried this:

<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>

And this is my Filter:

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);
    }

    ...
}

But, when I debug application step-by-step and loggin, the filter is called indefinitely, like it was in a loop.

How is the correct way to do this?

Upvotes: 3

Views: 7865

Answers (1)

Rob Winch
Rob Winch

Reputation: 21730

You need to continue the filter chain on URLs that you are redirecting to. For example:

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);
    }

    ...
}

Upvotes: 8

Related Questions