4、配置类

声明配置类

SpringBoot的配置类的使用方法基本与SpringMVC中相同

@Configuration
public class MyConfig implements WebMvcConfigurer{
    //根据需求重写方法
}

配置拦截器

1、自定义拦截器

@Component
public class ExetInterceptor extends HandlerInterceptorAdapter{
    /**
	 * 实现访问登录拦截
	 */
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        HttpSession session = req.getSession();
        Object ob = session.getAttribute("nowLogin");
        String uri = req.getRequestURI();
        if(uri.endsWith("/fileUpload.do")||uri.endsWith("/loginAjax.do")||uri.endsWith("/login.do")){
            return true;
        }else{
            if(ob == null){
                resp.sendRedirect("index.jsp");
                return false;
            }else{
                return true;
            }
        }
    }
}

2、配置拦截器

@Configuration
public class InterceptorConfig implements WebMvcConfigurer{
    @Autowired
    private ExetInterceptor exetInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(exetInterceptor);
        registration.addPathPatterns("/**");                      //所有路径都被拦截
        registration.excludePathPatterns(                         //添加不拦截路径
                                         "你的登陆路径",            //登录
                                         "/**/*.html",            //html静态资源
                                         "/**/*.js",              //js静态资源
                                         "/**/*.css",             //css静态资源
                                         "/**/*.woff",
                                         "/**/*.ttf"
                                         );    
    }	
}

配置过滤器

1、编写过滤器

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    }
    @Override
    public void destroy() {

    }
}

2、配置过滤器

@Configuration
public class FilterConfig {
    @Bean
    public FilterRegistrationBean<MyFilter> registrationBean(){
        FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new MyFilter());
        //setUrlPatterns() 一次性将映射关系配置进去
        filterRegistrationBean.addUrlPatterns("/*");  
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.setName("myFilter");
        return filterRegistrationBean;
    }
}