其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了。下面主要介绍两种常用的拦截器:
一、基于URL实现的拦截器:
1public class LoginInterceptor extends HandlerInterceptorAdapter{ 2 /** 3 * 在请求处理之前进行调用(Controller方法调用之前) 4 * 基于URL实现的拦截器 5 * @param request 6 * @param response 7 * @param handler 8 * @return 9 * @throws Exception 10 */ 11 @Override 12 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 13 String path = request.getServletPath(); 14 if (path.matches(Const.NO_INTERCEPTOR_PATH)) { 15 //不需要的拦截直接过 16 return true; 17 } else { 18 // 这写你拦截需要干的事儿,比如取缓存,SESSION,权限判断等 19 System.out.println("===================================="); 20 return true; 21 } 22 } 23}
关键代码:path.matches(Const.NO_INTERCEPTOR_PATH 就是基于正则匹配的url。
1/** 2 * @author BianP 3 * @explain 常量类 4 */ 5public class Const { 6 7 public static final String SUCCESS = "SUCCESS"; 8 public static final String ERROR = "ERROR"; 9 public static final String FIALL = "FIALL"; 10 /**********************对象和个体****************************/ 11 public static final String SESSION_USER = "loginedAgent"; // 用户对象 12 public static final String SESSION_LOGINID = "sessionLoginID"; // 登录ID 13 public static final String SESSION_USERID = "sessionUserID"; // 当前用户对象ID编号 14 15 public static final String SESSION_USERNAME = "sessionUserName"; // 当前用户对象ID编号 16 public static final Integer PAGE = 10; // 默认分页数 17 public static final String SESSION_URL = "sessionUrl"; // 被记录的url 18 public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // 登录页验证码 19 // 时间 缓存时间 20 public static final int TIMEOUT = 1800;// 秒 21 public static final String ON_LOGIN = "/logout.htm"; 22 public static final String LOGIN_OUT = "/toLogout"; 23 // 不验证URL anon:不验证/authc:受控制的 24 public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*"; 25}
二、基于注解的拦截器
①创建注解:
1/** 2 * 在需要登录验证的Controller的方法上使用此注解 3 */ 4@Target({ElementType.METHOD})// 可用在方法名上 5@Retention(RetentionPolicy.RUNTIME)// 运行时有效 6public @interface LoginRequired { 7 8}
②创建拦截器:
1public class AuthorityInterceptor extends HandlerInterceptorAdapter{ 2 3 @Override 4 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 5 // 如果不是映射到方法直接通过 6 if (!(handler instanceof HandlerMethod)) { 7 return true; 8 } 9 // ①:START 方法注解级拦截器 10 HandlerMethod handlerMethod = (HandlerMethod) handler; 11 Method method = handlerMethod.getMethod(); 12 // 判断接口是否需要登录 13 LoginRequired methodAnnotation = method.getAnnotation(LoginRequired.class); 14 // 有 @LoginRequired 注解,需要认证 15 if (methodAnnotation != null) { 16 // 这写你拦截需要干的事儿,比如取缓存,SESSION,权限判断等 17 System.out.println("===================================="); 18 return true; 19 } 20 return true; 21 } 22}
三、把拦截器添加到配置中,相当于SpringMVC时的配置文件干的事儿:
1/** 2 * 和springmvc的webmvc拦截配置一样 3 * @author BIANP 4 */ 5@Configuration 6public class WebConfigurer implements WebMvcConfigurer { 7 @Override 8 public void addInterceptors(InterceptorRegistry registry) { 9 // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录 10 registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**"); 11 registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**"); 12 } 13 14 @Bean 15 public LoginInterceptor LoginInterceptor() { 16 return new LoginInterceptor(); 17 } 18 19 @Bean 20 public AuthorityInterceptor AuthorityInterceptor() { 21 return new AuthorityInterceptor(); 22 } 23}
1、一定要加@Configuration 这个注解,在启动的时候在会被加载。
2、有一些教程是用的“WebMvcConfigurerAdapter”,不过在spring5.0版本后这个类被丢弃了 WebMvcConfigurerAdapter ,虽然还可以用,但是看起来不好。
3、也有一些教程使用的WebMvcConfigurationSupport,我使用后发现,classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/)不生效。具体可以原因,大家可以看下源码因为:WebMvcAutoConfiguration上有个条件注解:
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
所以还是建议使用WebMvcConfigurer, 其实springMVC很多东西,都可以搬到springboot中来使用,只需要把配置文件的模式,改成 对应@Configuration 类就好了。
微信群在QQ群中发,QQ技术群:
