package jj.tech.paolu.config.security;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
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 Cors {

	@Bean
	CorsFilter corsFilter() {
	    CorsConfiguration corsConfiguration = new CorsConfiguration();
	    corsConfiguration.addAllowedOriginPattern("*");
//	    corsConfiguration.addAllowedOrigin("file:///C:/Users/lenovo/Desktop/x.html");
//	    corsConfiguration.addAllowedOrigin("http://localhost:8080");
//	    corsConfiguration.addAllowedOrigin("http://localhost:80");
//	    corsConfiguration.addAllowedOrigin("http://localhost:5174");
	    corsConfiguration.addAllowedHeader("*");
	    corsConfiguration.addAllowedMethod("*");
	    corsConfiguration.setAllowCredentials(true);
	    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
	    source.registerCorsConfiguration("/**", corsConfiguration);
	    return new CorsFilter(source);
	}

	@Bean
	public  FilterRegistrationBean<CorsFilter> filterRegistrationBean(CorsFilter corsFilter) {
	    FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>(corsFilter);
	    bean.setOrder(1);
	    return bean;
	}

}
