SpringMVC interceptor to intercept page (pit)

Problem: When using the MVC interceptors, except when accessing the URL of the login page to determine whether logged in, logged in to access all jump to the login screen, but when set up, will judge and jump, but in the beginning after not logged access, will first enter to access a resource page, login page immediately flash receded.

But obviously this situation is not satisfied with the authority to intercept demand, it was in error.

 

The reason is that the beginning and then configure the default servlet default * .html release of resources in web.xml (as unworthy can not access to html resources.)

Often see such a configuration <servlet-name> default </ servlet-name> in the web.xml file, this configuration is the role: a static resource to client requests, such as request images, JS files are handed over to the default a servlet process, as follows:

<servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ico</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

How do you DispatcherServlet intercept "* .do" issues such as the URL suffix, does not exist can not access static resources.
If your DispatcherServlet block "/", in order to achieve REST style, to intercept all requests, then simultaneous access to static files * .js, *. Jpg, * . Html , etc. will be blocked.

Even after you were released some of the resources in mvc interceptors are also still unable to access static resources, because when you access static resources DispatcherServlet your request will first interception return to the browser after the release of still / ***. html, will visit again, and then again into DispatcherServlet, so the cycle.

Therefore, default servlet default configuration is still necessary.

So the question arises, here is my starting configuration (web.xml in):

    <-! Springmvc avoid ending interception html request ->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

However, although such functions are implemented on the surface, but it will issue began to appear outlined, or can not log in for a moment to see the intercepted resources.

Solution is simple, remove the above configuration in web.xml, the default servlet is configured to move mvc configuration file is as follows:

<! - Configure a static resource default servlet configuration allows the use of "/" to do the whole map, etc. -> 
<MVC: default -servlet-Handler />

Rerun, problem solving, will no longer be blocked into the page after the login screen will flash dance.

 

Guess you like

Origin www.cnblogs.com/xk920/p/11832553.html