zoukankan      html  css  js  c++  java
  • HTTP访问控制(CORS)

    第一步,了解概念:

    https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS

    第二步,java实现注入方式:

     1 @Configuration
     2 public class CorsConfig {
     3 
     4     @Bean
     5     public FilterRegistrationBean corsFilter() {
     6 
     7         final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
     8         final CorsConfiguration config = new CorsConfiguration();
     9         config.setAllowCredentials(true); // 允许cookies跨域
    10         config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
    11         config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
    12         config.setMaxAge(1800L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
    13         config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
    14         source.registerCorsConfiguration("/**", config);
    15         FilterRegistrationBean bean = new FilterRegistrationBean(new org.springframework.web.filter.CorsFilter(source));
    16         bean.setOrder(0);
    17         return bean;
    18     }
    19 }

    注入一个过滤器即可...

  • 相关阅读:
    try catch使用示例
    doxgen生成chm文档和乱码解决方法
    MFC中MessageBox()用法
    UML聚合与组合
    C#网络编程
    单元测试(NUnit)
    Autohotkey
    .NET中的并行
    System.Environment类的使用
    一键VHD
  • 原文地址:https://www.cnblogs.com/huzi007/p/10956125.html
Copyright © 2011-2022 走看看