Spring Boot 教程:CORS 支持
发布日期:2021-05-14 00:52:27 浏览次数:17 分类:博客文章

本文共 2629 字,大约阅读时间需要 8 分钟。

������������������������
    ������������������ (CORS) ������������������������������������ web ��������������������������������������������������������� JavaScript ���������������������������������������������������
   ������������������������ 8080 ��������� web ������������������ JavaScript ������������ 9090 ��������� RESTful web ��������������������������������������������� web ������������������������������������������������
   ���������������������������������������
  • RESTful web ���������������������������������������
  • RESTful web ������������������������������ 8080 ������������ API(s)���
   ��������������������������������������������������� RESTful Web ���������������������������������������

��������������������������� CORS

   ������������������������������������������ RESTful web ������������ @CrossOrigin ������������@CrossOrigin ������������������ REST API������������������������������
1 @RequestMapping(value = "/products")2 @CrossOrigin(origins = "http://localhost:8080")3  4 public ResponseEntity getProduct() {5    return null;6 }

������ CORS ������

   ��������� @Bean ������ Spring Boot ������������������ CORS���
1 @Bean2 public WebMvcConfigurer corsConfigurer() {3    return new WebMvcConfigurerAdapter() {4       @Override5       public void addCorsMappings(CorsRegistry registry) {6          registry.addMapping("/products").allowedOrigins("http://localhost:9000");7       }    8    };9 }
   ������ Spring Boot ��������������������� CORS ������������������
1 package com.tutorialspoint.demo; 2   3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.web.servlet.config.annotation.CorsRegistry; 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 9  10 @SpringBootApplication11 public class DemoApplication {12    public static void main(String[] args) {13       SpringApplication.run(DemoApplication.class, args);14    }15    @Bean16    public WebMvcConfigurer corsConfigurer() {17       return new WebMvcConfigurerAdapter() {18          @Override19          public void addCorsMappings(CorsRegistry registry) {20             registry.addMapping("/products").allowedOrigins("http://localhost:8080");21          }22       };23    }24 }
   ��������������������������� Spring Boot web ������������������ 8080 ������������RESTful web ��������������������� 9090 ������������������������������ RESTful Web ���������������������������������������������������������������
 
上一篇:Spring Boot 教程:国际化
下一篇:Spring Boot 教程:消费 Rest Web 服务

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月30日 08时49分01秒