mirror of
				https://github.com/suyiiyii/SIMS.git
				synced 2025-11-04 15:54:52 +08:00 
			
		
		
		
	feat(cors): 使用CorsFilter来处理跨域问题
This commit is contained in:
		
							parent
							
								
									0fea9664f9
								
							
						
					
					
						commit
						fb37c59810
					
				@ -1,23 +0,0 @@
 | 
				
			|||||||
package top.suyiiyii.sims.common;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.springframework.context.annotation.Bean;
 | 
					 | 
				
			||||||
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 {
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    @Bean
 | 
					 | 
				
			||||||
    public WebMvcConfigurer corsConfigurer() {
 | 
					 | 
				
			||||||
        return new WebMvcConfigurer() {
 | 
					 | 
				
			||||||
            @Override
 | 
					 | 
				
			||||||
            public void addCorsMappings(CorsRegistry registry) {
 | 
					 | 
				
			||||||
                registry.addMapping("/**")
 | 
					 | 
				
			||||||
                        .allowedOrigins("*")
 | 
					 | 
				
			||||||
                        .allowedMethods("*")
 | 
					 | 
				
			||||||
                        .allowedHeaders("*");
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					package top.suyiiyii.sims.common;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 | 
				
			||||||
 | 
					import org.springframework.web.filter.CorsFilter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class CorsConfiguration {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public CorsFilter corsFilter() {
 | 
				
			||||||
 | 
					        org.springframework.web.cors.CorsConfiguration config = new org.springframework.web.cors.CorsConfiguration();
 | 
				
			||||||
 | 
					        config.addAllowedOrigin("*");
 | 
				
			||||||
 | 
					        config.addAllowedMethod("OPTIONS");
 | 
				
			||||||
 | 
					        config.addAllowedMethod("POST");
 | 
				
			||||||
 | 
					        config.addAllowedHeader("*");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
 | 
				
			||||||
 | 
					        configSource.registerCorsConfiguration("/**", config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return new CorsFilter(configSource);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user