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 :

package com.wwr.like.config;

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;

@Configuration
public class CorsConfig {
    

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

    @Bean
    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 
        corsConfiguration.setMaxAge(MAX_AGE);
        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.
https://en.cdmana.com/2022/175/202206240841064893.html

Random recommended