{"id":5618,"date":"2024-08-01T22:47:04","date_gmt":"2024-08-01T14:47:04","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/?p=5618"},"modified":"2024-08-02T16:08:37","modified_gmt":"2024-08-02T08:08:37","slug":"spring-boot-3%e8%a7%a3%e5%86%b3%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring-boot\/spring-boot-3%e8%a7%a3%e5%86%b3%e8%b7%a8%e5%9f%9f%e9%97%ae%e9%a2%98%e7%9a%84%e5%87%a0%e7%a7%8d%e6%96%b9%e5%bc%8f\/","title":{"rendered":"Spring Boot 3\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"<div class=\"wxsyncmain\">\n<section style=\"margin-bottom: 8px;\"><em>\u70b9\u51fb\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u66f4\u591a<strong>\u8d44\u8baf<\/strong>\u53ca\u65f6\u63a8\u9001\u2193<\/em><\/section>\n<section class=\"mp_profile_iframe_wrp\"><\/section>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u8de8<\/span><span style=\"font-size: 16px;\">\u57df\u8d44\u6e90\u5171\u4eab\uff08CORS\uff0cCross-Origin Resource Sharing\uff09\u662f\u4e00\u4e2a\u6d4f\u89c8\u5668\u5b89\u5168\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u7f51\u9875\u4ece\u4e00\u4e2a\u57df\u8bf7\u6c42\u5230\u53e6\u4e00\u4e2a\u57df\u7684\u8d44\u6e90\u3002\u5728\u5f00\u53d1\u57fa\u4e8e\u5fae\u670d\u52a1\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u8de8\u57df\u8bf7\u6c42\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5728Spring Boot 3\u4e2d\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/span><\/section>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>1. \u4ec0\u4e48\u662f\u8de8\u57df<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u8de8\u57df\u95ee\u9898\u51fa\u73b0\u5728\u6d4f\u89c8\u5668\u8bd5\u56fe\u8bbf\u95ee\u4e0d\u540c\u6e90\uff08\u57df\u3001\u534f\u8bae\u3001\u7aef\u53e3\uff09\u7684\u8d44\u6e90\u65f6\u3002\u4e3a\u4e86\u5b89\u5168\uff0c\u6d4f\u89c8\u5668\u9ed8\u8ba4\u963b\u6b62\u8fd9\u79cd\u8bbf\u95ee\u3002\u8fd9\u79cd\u9650\u5236\u5bf9\u524d\u540e\u7aef\u5206\u79bb\u5f00\u53d1\u9020\u6210\u4e86\u4e00\u5b9a\u7684\u56f0\u6270\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u67d0\u4e9b\u65b9\u5f0f\u6765\u5141\u8bb8\u8de8\u57df\u8bf7\u6c42\u3002<\/span><\/section>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>2. Spring Boot 3\u89e3\u51b3\u8de8\u57df\u95ee\u9898\u7684\u65b9\u6cd5\u6982\u8ff0<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 16px;\">Spring Boot\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\u8de8\u57df\u95ee\u9898\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/span><\/section>\n<ul class=\"list-paddingleft-1\">\n<li>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 16px;\">\u4f7f\u7528@CrossOrigin\u6ce8\u89e3<\/span><\/section>\n<\/li>\n<li>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 16px;\">\u5168\u5c40\u914d\u7f6e\u8de8\u57df<\/span><\/section>\n<\/li>\n<li>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 16px;\">\u4f7f\u7528\u8fc7\u6ee4\u5668\uff08Filter\uff09<\/span><\/section>\n<\/li>\n<li>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u4f7f\u7528CORS\u914d\u7f6e\u7c7b<\/span><\/section>\n<\/li>\n<\/ul>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>3. \u4f7f\u7528@CrossOrigin\u6ce8\u89e3\u89e3\u51b3\u8de8\u57df\u95ee\u9898<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">@CrossOrigin\u6ce8\u89e3\u53ef\u4ee5\u76f4\u63a5\u5728\u63a7\u5236\u5668\u6216\u8005\u65b9\u6cd5\u4e0a\u4f7f\u7528\uff0c\u5141\u8bb8\u7279\u5b9a\u7684\u57df\u540d\u8bbf\u95ee\u3002<\/span><\/section>\n<h4 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/span><\/h4>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"kotlin\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@RestController<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@RequestMapping(<span class=\"code-snippet__meta-string\">\"\/api\"<\/span>)<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">ExampleController<\/span> <\/span>{<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@CrossOrigin(origins = <span class=\"code-snippet__meta-string\">\"http:\/\/example.com\"<\/span>)<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@GetMapping(<span class=\"code-snippet__meta-string\">\"\/greeting\"<\/span>)<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">public<\/span> ResponseEntity&lt;String&gt; greeting() {<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">return<\/span> ResponseEntity.ok(<span class=\"code-snippet__string\">\"Hello, World!\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u4e0a\u8ff0\u4ee3\u7801\u5141\u8bb8http:\/\/example.com\u57df\u540d\u8bbf\u95ee\/api\/greeting\u63a5\u53e3\u3002<\/span><\/section>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>4. \u5168\u5c40\u914d\u7f6e\u8de8\u57df<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u5982\u679c\u9700\u8981\u4e3a\u6240\u6709\u63a7\u5236\u5668\u7edf\u4e00\u914d\u7f6e\u8de8\u57df\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0\u4e00\u4e2aWebMvcConfigurer\u63a5\u53e3\u6765\u8fbe\u5230\u76ee\u7684\u3002<\/span><\/section>\n<h4 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/span><\/h4>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"css\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__selector-tag\">import<\/span> <span class=\"code-snippet__selector-tag\">org<\/span><span class=\"code-snippet__selector-class\">.springframework<\/span><span class=\"code-snippet__selector-class\">.context<\/span><span class=\"code-snippet__selector-class\">.annotation<\/span><span class=\"code-snippet__selector-class\">.Bean<\/span>;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__selector-tag\">import<\/span> <span class=\"code-snippet__selector-tag\">org<\/span><span class=\"code-snippet__selector-class\">.springframework<\/span><span class=\"code-snippet__selector-class\">.context<\/span><span class=\"code-snippet__selector-class\">.annotation<\/span><span class=\"code-snippet__selector-class\">.Configuration<\/span>;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__selector-tag\">import<\/span> <span class=\"code-snippet__selector-tag\">org<\/span><span class=\"code-snippet__selector-class\">.springframework<\/span><span class=\"code-snippet__selector-class\">.web<\/span><span class=\"code-snippet__selector-class\">.servlet<\/span><span class=\"code-snippet__selector-class\">.config<\/span><span class=\"code-snippet__selector-class\">.annotation<\/span><span class=\"code-snippet__selector-class\">.CorsRegistry<\/span>;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__selector-tag\">import<\/span> <span class=\"code-snippet__selector-tag\">org<\/span><span class=\"code-snippet__selector-class\">.springframework<\/span><span class=\"code-snippet__selector-class\">.web<\/span><span class=\"code-snippet__selector-class\">.servlet<\/span><span class=\"code-snippet__selector-class\">.config<\/span><span class=\"code-snippet__selector-class\">.annotation<\/span><span class=\"code-snippet__selector-class\">.WebMvcConfigurer<\/span>;<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">@<span class=\"code-snippet__keyword\">Configuration<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">public class WebConfig implements WebMvcConfigurer {<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    @<span class=\"code-snippet__keyword\">Override<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    public void addCorsMappings(CorsRegistry registry) {<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__selector-tag\">registry<\/span><span class=\"code-snippet__selector-class\">.addMapping<\/span>(\"<span class=\"code-snippet__comment\">\/**\")<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">                .allowedOrigins(\"http:\/\/example.com\")<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">                .allowedMethods(\"GET\", \"POST\", \"PUT\", \"DELETE\")<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">                .allowedHeaders(\"*\")<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">                .allowCredentials(true);<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">    }<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__comment\">}<\/span><\/span><\/code><\/pre>\n<\/section>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u6b64\u914d\u7f6e\u5141\u8bb8http:\/\/example.com\u57df\u540d\u901a\u8fc7GET\u3001POST\u3001PUT\u548cDELETE\u65b9\u6cd5\u8bbf\u95ee\u6240\u6709\u8def\u5f84\u3002<\/span><\/section>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>5. \u4f7f\u7528\u8fc7\u6ee4\u5668\u89e3\u51b3\u8de8\u57df\u95ee\u9898<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u4f7f\u7528\u8fc7\u6ee4\u5668\u53ef\u4ee5\u5728Spring Security\u4e2d\u914d\u7f6e\u8de8\u57df\u8bf7\u6c42\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u66f4\u590d\u6742\u7684\u5b89\u5168\u914d\u7f6e\u573a\u666f\u3002<\/span><\/section>\n<h4 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/span><\/h4>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"kotlin\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.context.<span class=\"code-snippet__keyword\">annotation<\/span>.Configuration;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.filter.CorsFilter;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.context.<span class=\"code-snippet__keyword\">annotation<\/span>.Bean;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.core.Ordered;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.core.<span class=\"code-snippet__keyword\">annotation<\/span>.Order;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.cors.CorsConfiguration;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.cors.UrlBasedCorsConfigurationSource;<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@Configuration<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">CorsConfig<\/span> <\/span>{<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@Bean<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@Order(Ordered.HIGHEST_PRECEDENCE)<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__keyword\">public<\/span> CorsFilter corsFilter() {<\/span><\/code><code><span class=\"code-snippet_outer\">        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();<\/span><\/code><code><span class=\"code-snippet_outer\">        CorsConfiguration config = new CorsConfiguration();<\/span><\/code><code><span class=\"code-snippet_outer\">        config.setAllowCredentials(<span class=\"code-snippet__literal\">true<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedOrigin(<span class=\"code-snippet__string\">\"http:\/\/example.com\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedHeader(<span class=\"code-snippet__string\">\"*\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedMethod(<span class=\"code-snippet__string\">\"*\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        source.registerCorsConfiguration(<span class=\"code-snippet__string\">\"\/**\"<\/span>, config);<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">return<\/span> new CorsFilter(source);<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<section style=\"margin-bottom: 0px;\"><span style=\"font-size: 16px;\">\u6b64\u914d\u7f6e\u5141\u8bb8http:\/\/example.com\u57df\u540d\u901a\u8fc7\u4efb\u610fHTTP\u65b9\u6cd5\u8bbf\u95ee\u6240\u6709\u8def\u5f84\u3002<\/span><\/section>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>6. \u4f7f\u7528CORS\u914d\u7f6e\u7c7b\u89e3\u51b3\u8de8\u57df\u95ee\u9898<\/strong><\/span><\/h3>\n<section style=\"margin-bottom: 8px;\"><span style=\"font-size: 16px;\">Spring Security 5\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b0\u7684\u8de8\u57df\u914d\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5b89\u5168\u914d\u7f6e\u4e2d\u76f4\u63a5\u5b9a\u4e49CORS\u89c4\u5219\u3002<\/span><\/section>\n<h4 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/span><\/h4>\n<section class=\"code-snippet__fix code-snippet__js\">\n<pre class=\"code-snippet__js\" data-lang=\"java\"><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.context.annotation.Bean;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.context.annotation.Configuration;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.security.config.annotation.web.builders.HttpSecurity;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.security.web.SecurityFilterChain;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.cors.CorsConfiguration;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.cors.UrlBasedCorsConfigurationSource;<\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">import<\/span> org.springframework.web.filter.CorsFilter;<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@EnableWebSecurity<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__meta\">@Configuration<\/span><\/span><\/code><code><span class=\"code-snippet_outer\"><span class=\"code-snippet__keyword\">public<\/span> <span class=\"code-snippet__class\"><span class=\"code-snippet__keyword\">class<\/span> <span class=\"code-snippet__title\">SecurityConfig<\/span> <\/span>{<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@Bean<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> SecurityFilterChain <span class=\"code-snippet__title\">securityFilterChain<\/span><span class=\"code-snippet__params\">(HttpSecurity http)<\/span> <span class=\"code-snippet__keyword\">throws<\/span> Exception <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        http.cors().and().csrf().disable();<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">return<\/span> http.build();<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__meta\">@Bean<\/span><\/span><\/code><code><span class=\"code-snippet_outer\">    <span class=\"code-snippet__function\"><span class=\"code-snippet__keyword\">public<\/span> CorsFilter <span class=\"code-snippet__title\">corsFilter<\/span><span class=\"code-snippet__params\">()<\/span> <\/span>{<\/span><\/code><code><span class=\"code-snippet_outer\">        UrlBasedCorsConfigurationSource source = <span class=\"code-snippet__keyword\">new<\/span> UrlBasedCorsConfigurationSource();<\/span><\/code><code><span class=\"code-snippet_outer\">        CorsConfiguration config = <span class=\"code-snippet__keyword\">new<\/span> CorsConfiguration();<\/span><\/code><code><span class=\"code-snippet_outer\">        config.setAllowCredentials(<span class=\"code-snippet__keyword\">true<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedOrigin(<span class=\"code-snippet__string\">\"http:\/\/example.com\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedHeader(<span class=\"code-snippet__string\">\"*\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        config.addAllowedMethod(<span class=\"code-snippet__string\">\"*\"<\/span>);<\/span><\/code><code><span class=\"code-snippet_outer\">        source.registerCorsConfiguration(<span class=\"code-snippet__string\">\"\/**\"<\/span>, config);<\/span><\/code><code><span class=\"code-snippet_outer\">        <span class=\"code-snippet__keyword\">return<\/span> <span class=\"code-snippet__keyword\">new<\/span> CorsFilter(source);<\/span><\/code><code><span class=\"code-snippet_outer\">    }<\/span><\/code><code><span class=\"code-snippet_outer\">}<\/span><\/code><\/pre>\n<\/section>\n<p><span style=\"font-size: 16px;\">\u5728\u6b64\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u7981\u7528\u4e86CSRF\u4fdd\u62a4\uff08\u4ec5\u5728\u786e\u5b9e\u9700\u8981\u65f6\uff09\uff0c\u7136\u540e\u914d\u7f6e\u4e86CORS\u8fc7\u6ee4\u5668\u3002<\/span><\/p>\n<h3 style=\"margin-top: 16px; margin-bottom: 8px;\"><span style=\"font-size: 18px;\"><strong>7. \u603b\u7ed3<\/strong><\/span><\/h3>\n<p><span style=\"font-size: 16px;\">\u5728Spring Boot 3\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u89e3\u51b3\u8de8\u57df\u95ee\u9898\uff0c\u5305\u62ec\u4f7f\u7528@CrossOrigin\u6ce8\u89e3\u3001\u5168\u5c40\u914d\u7f6e\u3001\u8fc7\u6ee4\u5668\u548cCORS\u914d\u7f6e\u7c7b\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u9879\u76ee\u7684\u590d\u6742\u6027\u3002<\/span><\/p>\n<p style=\"text-align: center; margin-bottom: 0px;\"><img decoding=\"async\" class=\"rich_pages wxw-img js_insertlocalimg lazyload\" style=\"height: auto !important;\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-imgfileid=\"100001253\" data-ratio=\"0.3794642857142857\" data-s=\"300,640\" data-type=\"png\" data-w=\"896\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img js_insertlocalimg\" style=\"height: auto !important;\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png\" data-imgfileid=\"100001253\" data-ratio=\"0.3794642857142857\" data-s=\"300,640\" data-type=\"png\" data-w=\"896\" \/><\/noscript><\/p>\n<section style=\"margin: 0px 0px 8px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; letter-spacing: 0.544px; white-space: pre-line; line-height: 30px; color: #4a4a4a; font-family: Avenir, -apple-system-font, \u5fae\u8f6f\u96c5\u9ed1, sans-serif;\"><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #78acfe; font-size: 15px;\">\u83b7\u53d6\u65b9\u5f0f\uff1a\u70b9\u201c<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; line-height: 30px; color: #3da742; font-size: 18px;\">\u5728\u770b<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #78acfe; font-size: 15px;\">\u201d\uff0c\u5173\u6ce8\u516c\u4f17\u53f7\u5e76\u56de\u590d\u3010<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #3da742; font-size: 18px;\">Java\u30011024\u3001AI\u3001\u6e38\u620f<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #78acfe; font-size: 15px;\">\u3011<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #78acfe; font-size: 15px;\">\u9886\u53d6\uff0c<\/span><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #78acfe; font-size: 15px;\">\u66f4\u591a\u5185\u5bb9\u9646\u7eed\u5949\u4e0a\u3002<\/span><\/section>\n<section style=\"margin: 0px 0px 10px; padding: 8px 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; white-space: normal; font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-align: right; line-height: 1.6; color: #3f3f3f;\"><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; font-size: 15px; color: rgba(0, 0, 0, 0.8); font-family: Optima-Regular, PingFangTC-light;\">PS\uff1a\u56e0\u516c\u4f17\u53f7\u5e73\u53f0\u66f4\u6539\u4e86\u63a8\u9001\u89c4\u5219\uff0c\u5982\u679c\u4e0d\u60f3\u9519\u8fc7\u5185\u5bb9\uff0c\u8bb0\u5f97\u8bfb\u5b8c\u70b9\u4e00\u4e0b<\/span><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; color: rgba(0, 0, 0, 0.8); font-family: Optima-Regular, PingFangTC-light; font-size: 12px;\">\u201c<span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #ff0000;\">\u5728\u770b<\/span>\u201d<\/strong><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; font-size: 15px; color: rgba(0, 0, 0, 0.8); font-family: Optima-Regular, PingFangTC-light;\">\uff0c\u52a0\u4e2a<\/span><strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; color: rgba(0, 0, 0, 0.8); font-family: Optima-Regular, PingFangTC-light; font-size: 12px;\">\u201c<span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; color: #ff0000;\">\u661f\u6807<\/span>\u201d<\/strong><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; font-size: 15px; color: rgba(0, 0, 0, 0.8); font-family: Optima-Regular, PingFangTC-light;\">\uff0c\u8fd9\u6837\u6bcf\u6b21\u65b0\u6587\u7ae0\u63a8\u9001\u624d\u4f1a\u7b2c\u4e00\u65f6\u95f4\u51fa\u73b0\u5728\u4f60\u7684\u8ba2\u9605\u5217\u8868\u91cc\u3002<\/span><\/section>\n<p style=\"margin: 10px 0px 0px; padding: 8px 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; white-space: normal; font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif; letter-spacing: 0.544px; text-align: right; line-height: 1.6; color: #3f3f3f;\"><span style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; letter-spacing: 0.544px; text-align: left; font-size: 15px; font-family: Optima-Regular, PingFangTC-light; visibility: visible; color: #ff0000;\">\u70b9<strong style=\"margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;\">\u201c\u5728\u770b\u201d<\/strong>\u652f\u6301\u7801\u519c\u5440\uff0c\u8c22\u8c22\u5566<\/span><\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" class=\"rich_pages wxw-img lazyload\" style=\"width: 578px; height: auto !important;\" data-src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222119450.gif\" src=\"https:\/\/s.nmxc.ltd\/sakurairo_vision\/@2.6\/load_svg\/inload.svg\" onerror=\"imgError(this)\" data-galleryid=\"\" data-imgfileid=\"100001252\" data-ratio=\"0.06442577030812324\" data-type=\"gif\" data-w=\"1071\" \/><noscript><img decoding=\"async\" class=\"rich_pages wxw-img\" style=\"width: 578px; height: auto !important;\" src=\"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222119450.gif\" data-galleryid=\"\" data-imgfileid=\"100001252\" data-ratio=\"0.06442577030812324\" data-type=\"gif\" data-w=\"1071\" \/><\/noscript><\/p>\n<p style=\"display: none;\">\n<blockquote><p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7: \u65b0\u751f\u4ee3\u7801\u519c<\/p><\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u70b9\u51fb\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u66f4\u591a\u8d44\u8baf\u53ca\u65f6\u63a8\u9001\u2193 \u8de8\u57df\u8d44\u6e90\u5171\u4eab\uff08CORS\uff0cCross-Origin Resource Sharing\uff09\u662f\u4e00\u4e2a\u6d4f\u89c8 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":5607,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[7],"tags":[],"class_list":["post-5618","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450-150x150.jpeg","content_first_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png","post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450-1024x435.jpeg","post_frist_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png","post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222056450.jpeg","post_all_images":[{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png","id":"image0"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222105557.png","id":"image1"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222119450.gif","id":"image2"},{"imagesurl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/08\/20240801222119450.gif","id":"image3"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring Boot","post_date":"2024-08-01","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":361,"next_post_id":5480,"next_post_title":"\u4e3a\u4f55\u5185\u5b58\u4e0d\u591f\u7528\uff1f\u5fae\u670d\u52a1\u6539\u9020\u542f\u52a8\u591a\u4e2a SpringBoot \u7684\u9677\u9631\u4e0e\u89e3\u51b3\u65b9\u6848","previous_post_id":5711,"previous_post_title":"14 \u4e2a SpringBoot \u4f18\u5316\u5c0f\u5999\u62db\uff0c\u5199\u4ee3\u7801\u50cf\u5199\u8bd7","_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/5618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5618"}],"version-history":[{"count":2,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/5618\/revisions"}],"predecessor-version":[{"id":6443,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/5618\/revisions\/6443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/5607"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}