@Configuration public class InterceptorConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); super.addResourceHandlers(registry); } @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(jwtInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/user/login", "/user/register", "/**/export", "/**/import", "/file/**") .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html"); super.addInterceptors(registry); } @Bean public JwtInterceptor jwtInterceptor() { return new JwtInterceptor(); } }
时间: 2023-06-20 07:09:05 浏览: 69
这是一个Java Spring Boot项目的拦截器配置类。在这个类中,我们首先重写了`addResourceHandlers()`方法来添加静态资源的处理器,这里是Swagger UI的资源,以便我们可以访问Swagger UI页面。
然后,我们重写`addInterceptors()`方法,并添加了一个自定义的JwtInterceptor拦截器。该拦截器将会拦截所有的请求(除了一些特定的排除路径),并检查请求头中是否包含了JWT Token,如果没有,则拒绝请求。这种方式可以保证只有经过认证的用户才能够访问我们的API接口。
最后,我们通过使用`@Bean`注解将JwtInterceptor类实例化为一个Bean。这样,我们就可以在其他地方通过依赖注入的方式来使用该拦截器了。
相关问题
package com.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import com.interceptor.AuthorizationInterceptor; @Configuration public class InterceptorConfig extends WebMvcConfigurationSupport{ @Bean public AuthorizationInterceptor getAuthorizationInterceptor() { return new AuthorizationInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getAuthorizationInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**"); super.addInterceptors(registry); } /** * springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,要访问静态资源需要重写addResourceHandlers方法 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/admin/") .addResourceLocations("classpath:/front/") .addResourceLocations("classpath:/public/"); super.addResourceHandlers(registry); } }
这是一个 Java Spring 的配置类,用于配置拦截器和静态资源访问路径。其中,@Configuration 注解表示这是一个配置类,@Bean 注解表示该方法返回一个 Bean 对象,getAuthorizationInterceptor() 方法返回一个 AuthorizationInterceptor 对象,用于验证用户登录状态。addInterceptors() 方法用于添加拦截器,这里将 AuthorizationInterceptor 对象添加到所有路径上,但排除了 /static/ 路径。addResourceHandlers() 方法用于配置静态资源访问路径,这里将 classpath:/resources/、classpath:/static/、classpath:/admin/、classpath:/front/、classpath:/public/ 这几个路径下的静态资源都配置为可访问的。
package com.itheima.reggie.config; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 设置静态资源映射 * @param registry */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("开始进行静态资源映射..."); registry.addResourceHandler("/backend/").addResourceLocations("classpath:/backend/"); registry.addResourceHandler("/front/").addResourceLocations("classpath:/front/"); } }
这是一段用于在Spring项目中配置静态资源映射的代码。它继承了WebMvcConfigurationSupport类并重写了addResourceHandlers()方法,在方法中使用ResourceHandlerRegistry类的addResourceHandler()和addResourceLocations()方法将"/backend/"和"/front/"映射到classpath下的"/backend/"和"/front/"文件夹。这样可以使访问这两个路径的请求都能访问到对应的静态资源。