解决跨域问题:
创建对应的包
package com.example.adminspringboot.config;标签:springboot,corsConfiguration,org,springframework,CorsConfiguration,017,import,跨域 From: https://www.cnblogs.com/Zhangzhiq/p/17036512.html
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
/**
* @author: zzq
* @date:2023/1/9
* @function: 解决数据跨域问题
*/
@Configuration
public class CorsConfig {
private static final long MAX_AGE = 24 * 60 * 60;
private CorsConfiguration buildConfig(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("http://localhost:8080"); //1. 设置访问源地址
corsConfiguration.addAllowedHeader("*"); //2. 设置访问请求头
corsConfiguration.addAllowedMethod("*"); //3. 设置访问请求方式
corsConfiguration.setMaxAge(MAX_AGE);
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());//4. 对接口配置跨域设置
return new CorsFilter(source);
}
}