springboot2.x custom interception pit that intercepts static static files

New post, please correct me after spraying, thank you

1. The king of the king, the pit of the pit

  Like many people, when I custom configured login interception in springboo, I wrote a WebConfig class to inherit WebMvcConfigureAdapter, rewrite AddResourceHandlers, and happily implement HandlerInterceptor, write logic in preHandle, then visit the login page, and then what a fuck , what about the style of my login page? Why are there only a few frames? Black question marks all over the face?

  Project directory:

  

  Static path for properties configuration: spring.mvc.static-path-pattern=/resources/**

  The static file configuration and interception configuration in WebConfig are as follows:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置模板资源路径
         registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");  
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");  
    }
    
    
    @Override
    public  void addInterceptors(InterceptorRegistry registry) {
 //         InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
         // Do not operate in the following way, if it is written in that way, it is not allowed. This is equivalent to creating multiple Interceptors. Instead of just one Interceptor 
 //         addInterceptor.excludePathPatterns("/login");
 //         addInterceptor.excludePathPatterns("/login/main");
 //         // Intercept all paths
 //        addInterceptor.addPathPatterns("/**") ;
         // addPathPatterns("/**") intercepts all requests, but excludes interception of /login/main and /login requests 
        registry.addInterceptor(getSecurityInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/login","/login/main");
    }
    
    @Bean
    public MyInterceptor getSecurityInterceptor() {
        return new MyInterceptor();
    }

  

/**
     * Execute before controller
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
       log.info( " Start checking whether to log in" );
       log.info(request.getRequestURL().toString());
       // Determine whether there is a session logged in by the user 
       if (request.getSession().getAttribute(SESSION_KEY) != null ) {
           log.info( " User is logged in" );
            return  true ;
       }
       log.info( "The user has not logged in" );
        // Jump to the login page 
       response.sendRedirect(request.getContextPath() + " /login " );
        return  false ;
    }
    

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

  After printing the request address output, I found that (log.info(request.getRequestURL().toString())), the static pages under static were all blocked, and the question mark was added with a confused look.

2. Solutions

  I checked a lot of information on the Internet, but none of them mentioned the point. Later, I found out that the reason is: the version is actually:

  Spring boot 2.x has been changed to support the minimum jdk8 version, and the interface in jdk8 allows a default implementation, so the WebMvcConfigurerAdapter adaptation class has been abandoned, and the WebMvcConfigurer interface is directly implemented instead.

  The above information comes from the help of ==>https://my.oschina.net/dengfuwei/blog/1795346.

  Then WebConfig implements WebMvcConfigurer, is to implement WebMvcConfigurer. . . . . . Then modify the interception, leaving only @Configuration for the annotation.

registry.addInterceptor(getSecurityInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/login","/login/main","/static/**");

 

   Restart the project, visit: http://localhost:8080/login, perfect

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300200&siteId=291194637