问题:单点登录和鉴权的拦截器不是自己写的而是在jar包里,还是在两个不同的jar包,访问配置了鉴权注释的controller时,无法获取单点登录的上下文
debug时发现先执行了鉴权的拦截器,再执行单点登录的拦截器,因此我们需要调整一下顺序
@Configuration
public class Config implements WebMvcConfigurer, ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//先执行单点登录拦截器
SpringSSOInterceptor springSSOInterceptor = context.getBean(SpringSSOInterceptor.class);
registry.addInterceptor(springSSOInterceptor).addPathPatterns("/**");
//再执行鉴权拦截器
UsfInterceptor usfInterceptor = context.getBean(UsfInterceptor.class);
registry.addInterceptor(usfInterceptor).addPathPatterns("/**");
}
}
在这里,添加拦截器的顺序就是执行的顺序,如果不显示指定,则会按照本地配置文件(@Configuration)—>jar包中的配置文件,—>配置文件外使用注解的Interceptor的顺序执行
另外,有多个拦截器的时候,会按顺序执行所有拦截器的preHandle,再按顺序执行拦截,最后按顺序执行所有拦截器的postHandle