java解决跨域

方法中
response.setHeader("Access-Control-Allow-Origin", "https://ding.taozugong.com"); response.setHeader("Access-Control-Allow-Methods","POST, GET, DELETE, PUT, PATCH, OPTION"); response.setHeader("Access-Control-Allow-Headers","token, Content-Type"); response.setHeader("Access-Control-Allow-Credentials", "true"); Controller层
解决跨域注解 @CrossOrigin(origins
= "*", maxAge = 3600)

 

 1 package com.taozugong.service;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.web.cors.CorsConfiguration;
 6 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 7 import org.springframework.web.filter.CorsFilter;
 8 
 9 /**
10  * @Author: lgy
11  * @Date: 2018/7/2 20:33
12  * @Description:
13  */
14 @Configuration
15 public class CorsConfig {
16     private CorsConfiguration buildConfig() {
17         CorsConfiguration corsConfiguration = new CorsConfiguration();
18         corsConfiguration.setAllowCredentials(true);
19         corsConfiguration.addAllowedOrigin("https://shiyong.taozugong.com");
20         corsConfiguration.addAllowedOrigin("https://zuyong.taozugong.com");
21         corsConfiguration.addAllowedOrigin("https://newadm.taozugong.com");
22         corsConfiguration.addAllowedOrigin("https://ding.taozugong.com");
23         corsConfiguration.addAllowedHeader("*");
24         corsConfiguration.addAllowedMethod("*");
25         corsConfiguration.applyPermitDefaultValues();
26         return corsConfiguration;
27     }
28 
29     @Bean
30     public CorsFilter corsFilter() {
31         UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
32         source.registerCorsConfiguration("/**", buildConfig());
33         return new CorsFilter(source);
34     }
35 }

 

posted @ 2018-08-01 15:11  huohuohiahia  阅读(159)  评论(0编辑  收藏  举报