应用场景项目需要拦截除指定url以外的所有请求,在请求进入controller之前判断参数是否合法正确。注册拦截器:继承WebMvcConfigurationSupport,重写addInterceptors方法@ConfigurationpublicclassInterceptorConfigextendsWebMvcConfigurationSupport{privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass());@AutowiredprivateJumpControllerInterceptorjumpControllerInterceptor;@OverridepublicvoidaddInterceptors(InterceptorRegistry#不包含项目注册表的路径)呵呵registry.addInterceptor(jumpControllerInterceptor).addPathPatterns("/**").excludePathPatterns("/test");super.addInterceptors(注册表);}@OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistryregistry){Stringos=System.getProperty("os.name");//如果是Windows系统//if(os.toLowerCase().startsWith("win")){//registry.addResourceHandler("/app_file/**")/////app_file/**表示在磁盘文件PathWindow目录下的所有资源都会被解析到以下路径//.addResourceLocations("file:"+filePathWindow);//}else{//linux和mac//registry.addResourceHandler("/app_file/**")//.addResourceLocations("file:"+filePathLinux);//}registry.addResourceHandler("/jumpLogs/**").addResourceLocations("file:jumpLogs/");super.addResourceHandlers(注册表);}}实现HandlerInterceptorThisinterface@ComponentpublicclassJumpControllerInterceptorimplementsHandlerInterceptor{privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass());@OverridepublicbooleanpreHandle(HttpServletRequestreq,HttpServletResponseres,Objecto){Stringrequest.getRI=if(requestURI.startsWith("/jump/app/")){logger.info("检查路径变量:appCode");映射pathVariables=(Map)req.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);字符串appCode=(String)pathVariables.get("appCode");/*2.判断IDP是否登录*/AppAddrMappingappAddrMapping=AppAddrMapping.ADDR_MAPPING_MAP.get(appCode);if(null==appAddrMapping){logger.error("当前url中的appCode:{}与app.json中的不匹配,请检查配置文件",appCode);thrownewConfArgException("app不存在,请检查appcode是否正确");}}返回真;}@OverridepublicvoidpostHandle(HttpServletRequestreq,HttpServletResponseres,Objecto,ModelAndViewmav){}@OverridepublicvoidafterCompletion(HttpServletRequestreq,HttpServletResponserep,Objecto,Exceptione){}拦截器不起作用的原因a)继承orsWebMvcConfigurationIntercept,override方法该类需要使用@Configuration注解b)addPathPatterns中的问题拦截器的最终路径必须是/,如果是目录就是/*/c)excludePathPatterns或者addPathPatterns写错了,项目的ContextPath拦截器的main方法在configurePathMatch之前添加:配置路由请求规则configureContentNegotiation:contentnegotiationconfigureconfigureAsyncSupportconfigureDefaultServletHandling:默认静态资源处理器addFormatters:注册自定义转换器addInterceptors:拦截器配置addResourceHandlers:资源处理addCorsMappings:CORS配置addViewControllers:视图跳转控制器configureViewResolvers:配置视图解析addArgumentResolvers:添加自定义方法参数处理器addReturnValueHandlers:添加自定义返回结果processorconfigureMessageConverters:配置消息转换器。重载覆盖默认注册的HttpMessageConverterextendMessageConverters:配置消息转换器。只添加一个自定义的HttpMessageConverter.configureHandlerExceptionResolvers:配置异常转换器extendHandlerExceptionResolvers:添加异常转换器getValidatorgetMessageCodesResolver,使用如下三个接口:/**解决跨域问题**/publicvoidaddCorsMappings(CorsRegistryregistry);/**添加拦截器**/voidaddInterceptors(InterceptorRegistryregistry);/**静态资源处理**/voidaddResourceHandlers(ResourceHandlerRegistryregistry);@ConfigurationpublicclassAddCorsMappingsextendsWebMvcConfigurationSupport{/***跨域访问配置接口*@paramregistry*/@OverridepublicvoidaddCorsMappings(CorsRegistryregistry){registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST","GET","PUT","OPTIONS","DELETE").allowCredentials(true).allowedHeaders("*").maxAge(3600);}}一个非常重要的关于静态资源处理的提示:Springboot中只有一个WebMvcConfigurationSupport配置类是真正启动的对于这个问题,其实可以通过implementsWebMvcConfigurer解决,关于使用Servlet获取URL地址,多个不同类实现该接口后的配置可以正常运行。在HttpServletRequest类中,有如下六个函数getContextPath获取项目名getServletPath获取Servlet名getPathInfo获取Servlet后的URL名,不包含URL参数getRequestURL获取无参URLgetRequestURI获取无参URI,即去掉协议和服务器名称的URL如下图所示:对应的函数值如下:getContextPath:/ServletTestgetServletPath:/maingetPathInfo:/index/testpage/testgetRequestURL:http://localhost:8080/Servlet...getRequestURI:/ServletTest/main/index/testpage/test
