简体   繁体   中英

get response status code tells if the request is supposed to responded with HTTP 404 inside a Spring MVC Interceptor preHandle method

Is it possible to get the response status code tells if the request is supposed to responded with HTTP 404 with preHandle method inside a Spring MVC Interceptor.

Because the following code would result to redirecting every invalid requests to the signin page:

public class SecurityInterceptor extends HandlerInterceptorAdapter {
    private static final Log LOG = LogFactory.getLog(SecurityInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOG.info("Interceptor: Pre-handle");
        HttpSession session = request.getSession();
        String urlPath = request.getRequestURI();
        String contextPath = request.getContextPath();
        String relativePath = urlPath.replaceFirst(contextPath, "");
        if (!relativePath.equals("/signin") && !relativePath.equals("/register")) {
            if (session == null || session.getAttribute("curUser") == null) {
                //redirect to signin
                response.sendRedirect(contextPath + "/signin");
                return false;
            }
        }
        return true;
    }

}

The following code will redirect the http request with the status code: "Temporary 302 HTTP". However, if the controller responsible for the "/signin" path (or the original request path) doesn't exist, this might generate an HTTP 404.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM