本篇作为学习记录,仅供参考。
项目结构如下:
在com.example.userdemo目录下创建configure,并在其中创建一个CorsConfig类,具体代码如下:
package com.example.userdemo.configure;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOriginPatterns("*")
.allowCredentials(true)
.maxAge(3600);
}
}
说明:本人使用的jdk是17版本,mysql版本是8.0.33。