import dt.buriedpoint.web.interceptor.InterceptorConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@SpringBootApplication
@ComponentScan
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
/**
* 注册拦截器
*/
@Bean
public WebMvcConfigurerAdapter getInterceptors() {
//注册自定义拦截器,添加拦截路径和排除拦截路径
return new WebMvcConfigurerAdapter() {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new InterceptorConfig())
.addPathPatterns("/*")
.excludePathPatterns("api/path/login");
}
};
}
/**
* 设置全局跨域资源访问
*/
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(true);
}
};
}
}
springboot设置拦截器和跨域资源共享
最新推荐文章于 2025-03-10 17:46:50 发布