springboot设置跨域 方法拦截器

springboot跨越设置

package com.softwaer.road.configuration;


import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
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.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * springboot跨越设置
 * @author GYN
 *
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration corsConfiguration = new CorsConfiguration();
//        /*是否允许请求带有验证信息*/
//        corsConfiguration.setAllowCredentials(true);
//        /*允许访问的客户端域名*/
//        corsConfiguration.addAllowedOrigin("*");
//        /*允许服务端访问的客户端请求头*/
//        corsConfiguration.addAllowedHeader("*");
//        /*允许访问的方法名,GET POST等*/
//        corsConfiguration.addAllowedMethod("*");
//    
//   
//        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
//        return new CorsFilter(urlBasedCorsConfigurationSource);
//    }
    @Override  
    public void addCorsMappings(CorsRegistry registry) {  
        registry.addMapping("/**").allowedOrigins("*")
        .allowedMethods("*").allowedHeaders("*")
        .allowCredentials(true)
        .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);

    }
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        List list = new ArrayList<>();
        list.add("*");
        corsConfiguration.setAllowedOrigins(list);
        /*
        // 请求常用的三种配置,*代表允许所有,当时你也可以自定义属性(比如header只能带什么,只能是post方式等等)
        */
        corsConfiguration.addAllowedOrigin("*"); 
        corsConfiguration.addAllowedHeader("*"); 
        corsConfiguration.addAllowedMethod("*"); 
        return corsConfiguration;
      }
    @Bean
    public CorsFilter corsFilter() {
      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
      source.registerCorsConfiguration("/**", buildConfig());
      return new CorsFilter(source);
    }
    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor( new SessionInterceptor())
                .addPathPatterns( "/**" ).excludePathPatterns(
                		"/user/login"
                		
                		,"/wehcat/**"

                		,"/testController/**"
                		,"/adminController/**"
 
                		,"/xcxCrm/**"
                		); 
        registry.addInterceptor( new SessionInterceptorAdmin())
        .addPathPatterns( "/adminController/**" ).excludePathPatterns(
        		"/adminController/login"
        		
        		
        		
        		   
        		,"/adminController/updateNews" 
        		,"/adminController/getNewsId", 
        		"/adminController/newsById",
        		"/adminController/uploadNews",
        		"/adminController/getTourismProjectType",
        		"/adminController/getNewsType"
        		
        		 
        		
        		);
        
    }

}


你可能感兴趣的:(springboot,跨域,js跨域)