java跨域訪問配置


package com.get;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.MultipartConfigElement;

@Configuration
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication

public class EurekaZuulClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaZuulClientApplication.class, args);
    }

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允許cookies跨域
        config.addAllowedOrigin("*");      // 允許向該服務器提交請求的URI,*表示全部允許。。這里盡量限制來源域,比如http://xxxx:8080 ,以降低安全風險。。
        config.addAllowedHeader("*");      // 允許訪問的頭信息,*表示全部
        config.setMaxAge(18000L);          // 預檢請求的緩存時間(秒),即在這個時間段里,對於相同的跨域請求不會再預檢了
        config.addAllowedMethod("*");      // 允許提交請求的方法,*表示全部允許,也可以單獨設置GET、PUT等
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");   // 允許Get的請求方法
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
    /**
     * 文件上傳配置
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  單個數據大小
        factory.setMaxFileSize("10240KB"); // KB,MB
        /// 總上傳數據大小
        factory.setMaxRequestSize("102400KB");
        return factory.createMultipartConfig();
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM