@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/templates/",
"classpath:/static/", "classpath:/public/" };
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INFO/jsp");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//super.addViewControllers(registry);
//设置默认访问login页面
registry.addViewController("/").setViewName("login");
registry.addViewController("/index11.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
/*
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyLoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/dashboard","/login","/hello");
}
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/templates/**")
.addResourceLocations(
CLASSPATH_RESOURCE_LOCATIONS);
}
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// 设置允许跨域请求的域名
config.addAllowedOrigin("*");
// 是否允许证书 不再默认开启
// config.setAllowCredentials(true);
// 设置允许的方法
config.addAllowedMethod("*");
// 允许任何头
config.addAllowedHeader("*");
config.addExposedHeader("token");
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
return new CorsFilter(configSource);
}
}
实验结果乱七八糟的。真恶心。我为此要花很多天的时间。现在先把拦截器关掉。
|