package com.dcit.danger.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.dcit.authority.utils.JsonWebTokenUtil;

import java.util.Arrays;

/**
* <p>Title: SecurityCorsConfiguration</p>
* <p>Description: </p>
* <p>Company: SJZ DC-IT co. LTD</p> 
* @author ZZZ
* @date 2018年9月6日 下午11:12:46
 */
@Configuration
public class SecurityCorsConfiguration {
	
	@Autowired
	private ProjectPath projectPath;

    @SuppressWarnings("unchecked")
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        
        corsConfiguration.setAllowCredentials(true);
        
        System.out.println(projectPath.getSysAllowOriginList().toString());
        
        corsConfiguration.setAllowedOrigins(projectPath.getSysAllowOriginList());
        corsConfiguration.setAllowedHeaders(Arrays.asList(CorsConfiguration.ALL));
        corsConfiguration.setAllowedMethods(Arrays.asList(CorsConfiguration.ALL));
        corsConfiguration.addExposedHeader(JsonWebTokenUtil.TOKEN_HEADRE_KEY);//显示header自定义属性
        source.registerCorsConfiguration("/**", corsConfiguration);
        
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }
}
