springboot mvc配置(拦截器,视图解析,跨域)

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);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值