java B2B2C Springboot仿淘宝电子商城系统-spring cloud 跨域访问

1、在网关中配置

@EnableZuulProxy 
@SpringBootApplication
public class ZuulApplication {
	
	@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);
    }
 
 
	public static void main(String[] args) {
		SpringApplication.run(ZuulApplication.class, args);
	}
}

2、在请求的类中的配置

/**
 * @author wujing
 */
@RestController
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
@RequestMapping(value = "api/user")
 
public class ApiUserController {
 
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public User view(@PathVariable int id) {
		User user = new User();
		user.setId(id);
		user.setName("无境-user");
		user.setCreateTime(new Date());
		logger.info("请求接口返回:{}", user);
		return user;
	}
 
}

需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 壹零叁八柒柒肆六二六
java B2B2C Springboot仿淘宝电子商城系统

猜你喜欢

转载自blog.csdn.net/sinat_23107883/article/details/85291905