SpringBoot 登录拦截功能的实现

用到 handlerInterceptor;
1、实现HandlerInterceptor接口或继承实现了HandlerInterceptor的类
1) 重写 preHanle 方法 实现登录用户的过滤 判断是否已经登陆,未登录跳转到登录页面
2、
1)在springmvc.xml文件的mvc拦截器中加上实现HanderInterceptor的类

<mvc:interceptors>
	<mvc:interceptor>
        <bean class="interceptor.LoginInterceptor"/>
        <!-- 需要拦截的url-->
		<mvc:mapping path="/**"/>
		<!-- 排除以下url -->
		<mvc:exclude-mapping path="/main/**"/>    

2)新建配置类WebMvcConfigure实现WebMvcConfigurer接口
这种方法要在启动类里扫描到实现类

@Configuration
public class WebMvcConfigure implements WebMvcConfigurer {

    public final static String SESSION_KEY = "user";
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**","/main.html").excludePathPatterns("/main/**");
    }

}
原文地址:https://www.cnblogs.com/ZCWang/p/12924839.html