用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

it绿萝    -  云代码空间

——

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

2018-12-28|375阅||

摘要:1、在网关中配置 需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码:壹零叁八柒柒肆六二六 @EnableZuulProxy  @SpringBootApplication public class ZuulApplication { @

1、在网关中配置


需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码:壹零叁八柒柒肆六二六


@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 B2B2C Springboot仿淘宝电子商城系统

顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: it绿萝
    • 等级: 高级设计师
    • 积分: 5730
    • 代码: 0 个
    • 文章: 192 篇
    • 随想: 0 条
    • 访问: 15 次
    • 关注

    人气代码

      最新提问

        站长推荐