preface

The WebMvcConfigurerAdapter class was deprecated in Spring Boot2.x. The WebMvcConfigurerAdapter class was deprecated in Spring Boot2.x. Inherits the WebMvcConfigurerAdapter class, which has this convenience, but has been deprecated in Spring5.0. The official documentation also states that the WebMvcConfigurer interface now has a default blank method, so it is better to implement WebMvcConfigurer under Springboot2.0 (Spring5.0).

The interceptor

directory

The interceptor

A custom interceptor must implement HandlerInterceptor, which defines a login interceptor that intercepts operations that require login and redirects operations that do not

package com.cxy.springboot.utils.Interceptor; import com.cxy.springboot.utils.GlobalConst; import com.cxy.springboot.utils.UserInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @Auther: cxy * @Date: 2019/1/10 * @Description: */ public class LoginInterceptor implements HandlerInterceptor {private Logger Logger = LoggerFactory.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { UserInfo user = (UserInfo)request.getSession().getAttribute(GlobalConst.USER_SESSION_KEY); logger.info(request.getRequestURI().toString());if (user == null || user.equals(""))  {
                response.sendRedirect("/login");
                logger.info("Please log in first.");
                return false;
            }
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            logger.info("postHandle...");
        }

        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            logger.info("afterCompletion..."); }}Copy the code
  1. PreHandle: Called before the request is processed by the business processor. Preprocessing, coding, security control, authority check and other processing;
  2. PostHandle: Executed after the business processor completes the execution of the request and before the view is generated. Post-processing (Service is called and ModelAndView is returned, but the page is not rendered), with an opportunity to modify the ModelAndView;
  3. AfterCompletion: Invoked after the DispatcherServlet has fully processed the request and can be used to clean up resources, etc. Return processing (the page has already been rendered);

Register interceptors

Create a new class, WebConfigurer. Java, addPathPatterns to set interception paths, and excludePathPatterns to whitelist paths that don’t need to trigger the interceptor.

package com.cxy.springboot.utils; import com.cxy.springboot.utils.Interceptor.LoginInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; /** * @Auther: cxy * @Date: 2019/1/10 * @Description: In the Web configuration file, instantiate the logged interceptor, */ @configuration public class WebConfigurer implements WebMvcConfigurer {@override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/ * *").excludePathPatterns("/login").excludePathPatterns("/loginSys").excludePathPatterns("/static/**"); }}Copy the code

AddResourceLocations sets the resource path. If you want to specify an external directory, you can simply specify addResourceLocations as follows:

registry.addResourceHandler("/static/**").addResourceLocations("file:E:/cxy/");
Copy the code

Configuring Static Resources

Specify static resource interception in application.properties or application.yml, otherwise static resources will be intercepted.

Configure static resources
spring.mvc.static-path-pattern=/static/**
Copy the code

Foreground static file path configuration

<link th:href="@{/static/js/hplus/css/bootstrap.min14ed.css}" rel="stylesheet">
Copy the code