Novice User
Novice User

Reputation: 3824

Endless loop in Spring MVC Interceptor

I have written a custom interceptor PreventScreenInterceptor extends HandlerInterceptorAdapter

in preHandle I am checking some conditions, and based on that, I am redirecting using

response.sendRedirect("/myapp/user/noaccess");

Now, whenever I am hitting /myapp/user/noaccess , it is going into endless loop as I am not able to come out of this interceptor. Its getting called again and again.

My Application context has :

    <mvc:interceptor>
        <mvc:mapping path="/myapp/user/**"/>
        <bean class="com.mypackage.interceptors.PreventScreenInterceptor" />
    </mvc:interceptor>

Upvotes: 0

Views: 1939

Answers (2)

Ritesh Kumar
Ritesh Kumar

Reputation: 89

You must check that the if the request is not coming with the action where you are redirecting from Interceptor , otherwise it will recall itself again and again.

for reference use this code -

@Override
    public boolean preHandle(HttpServletRequest request,

    HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        logger.debug("inside interceptor  and uri = "+uri);

        if (!uri.endsWith("/noaccess") ) {
            logger.info("request is coming from other than /myapp/user/noaccess");
            response.sendRedirect("/myapp/user/noaccess");
        }

        return true;
    }

Upvotes: 1

ced-b
ced-b

Reputation: 4065

You have to use request.getRequestURI() to check that the URI being called is not "/myapp/user/noaccess", before sending your redirect.

Upvotes: 1

Related Questions