package com.chashiyu.configuration;
import com.chashiyu.common.passport.interceptor.MyPassportInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.Arrays;
/**
* 服务端返回报文格式响应头设置
*
* @author shiyu.zha create on 20181024.
* @author shiyu.zha update on 20200102.
* @author shiyu.zha update on 20200107.
*/
@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {
@Bean
public MyPassportInterceptor myPassportInterceptor() {
return new MyPassportInterceptor();
}
@Override//配置拦截器
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myPassportInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
super.addInterceptors(registry);
}
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8); //设置默认的media type
// configurer.favorPathExtension(true); //是否通过请求Url的扩展名来决定media type
// configurer.ignoreAcceptHeader(true); //不检查Accept请求头
// configurer.parameterName("mediaType");
// configurer.mediaType("html", MediaType.TEXT_HTML); //请求以.html结尾的会被当成MediaType.TEXT_HTML
// configurer.mediaType("json", MediaType.APPLICATION_JSON); //请求以.json结尾的会被当成MediaType.APPLICATION_JSON
super.configureContentNegotiation(configurer);
}
@Override//静态资源访问路径配置
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
@Override//设置默认首页
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
@Bean//设置视图解析器
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/static/");
resolver.setSuffix(".html");
return resolver;
}
@Override//解决跨域方式一(因为自定义拦截器优先级较高, 此配置不能解决跨域问题, 需要通过配置CORSFilter解决)
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedOrigins("*")
.allowedHeaders("*")
.allowedMethods("OPTIONS", "GET", "POST", "PUT", "DELETE")
.maxAge(3600);
super.addCorsMappings(registry);
}
@Bean//解决跨域方式二
public CorsFilter corsFilter() {
// 1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
// 1.1 是否发送Cookie信息
config.setAllowCredentials(true);
// 1.2 允许的域,(有的帖子说不要写*,否则cookie就无法使用了,但未验证过)
String[] origins = {"*"};
config.setAllowedOrigins(Arrays.asList(origins));
// 1.3 允许的方法
String[] methods = {"OPTIONS", "GET", "POST", "PUT", "DELETE"};
config.setAllowedMethods(Arrays.asList(methods));
// 1.4 允许的头信息
String[] headers = {"*"};
config.setAllowedHeaders(Arrays.asList(headers));
// 2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
// 3.返回新的CorsFilter.
return new CorsFilter(configSource);
}
}
springboot mvc配置(拦截器,视图解析,跨域)
最新推荐文章于 2023-04-13 20:13:48 发布