current position:Home>Front and rear end cross domain issues

Front and rear end cross domain issues

2022-06-24 09:48:48Like the evening wind

Just document the problem and the solution
Reference article
 Insert picture description here
Solution :


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;

public class CorsConfig {

    //  Maximum effective duration of current cross domain request . Default here 1 God 
    private static final long MAX_AGE = 24 * 60 * 60;

    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("http://localhost:8080"); // 1  Set access source address 
        corsConfiguration.addAllowedHeader("*"); // 2  Set access source request header 
        corsConfiguration.addAllowedMethod("*"); // 3  Set access source request method 
        source.registerCorsConfiguration("/**", corsConfiguration); // 4  Configure cross domain settings for interfaces 
        return new CorsFilter(source);

copyright notice
author[Like the evening wind],Please bring the original link to reprint, thank you.

Random recommended