解决跨域问题

https://blog.csdn.net/saytime/article/details/74937204

 

package com.xhsr.reward.configurator.web.filter;

import com.xhsr.cloud.config.client.ConfigClient;
import com.xhsr.commons.filter.http.HttpFilterAPI;
import com.xhsr.commons.filter.http.chain.cors.CorsProxyFilter;
import com.xhsr.commons.filter.http.chain.token.jwt.JsonWebTokenFilter;
import com.xhsr.commons.filter.http.configuration.DynamicConfiguration;
import com.xhsr.reward.configurator.web.constants.Constants;
import com.xhsr.reward.configurator.web.constants.PropKeyConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author Han
 * 注册过滤器
 */
@Configuration
public class FilterRegister {

    @Autowired
    @Qualifier("configClient")
    private ConfigClient configClient;

    /**
     * @return
     * @Title registeringJsonWebTokenFilter
     * @Description 注册 Json Web Token 拦截器
     * @author Han
     * @date 2017年10月25日 上午11:54:35
     * @version V1.0.0
     * @history 版本 作者 时间 注释
     */
    @Bean
    public FilterRegistrationBean registeringJsonWebTokenFilter() {
        LinkedHashSet urlPatterns = new LinkedHashSet<>();
        for (String urlPattern : configClient.getProperty(PropKeyConstants.TOKEN_FILTER_URL_PATTERNS, String[].class, Constants.DEFAULT_TOKEN_FILTER_URL_PATTERNS)) {
            urlPatterns.add(urlPattern);
        }
        JsonWebTokenFilter.Builder builder = JsonWebTokenFilter.builder()
                .exclusionsIPs(new DynamicConfiguration>() {
                    @Override
                    public Set value() {
                        return configClient.getProperty(PropKeyConstants.TOKEN_FILTER_EXCLUSIONS, Set.class);
                    }
                })
                .enable(new DynamicConfiguration() {
                    @Override
                    public Boolean value() {
                        return configClient.getProperty(PropKeyConstants.TOKEN_FILTER_ENABLE, Boolean.class);
                    }
                })
                .publicKey(configClient.getProperty(PropKeyConstants.TOKEN_FILTER_PUBLIC_KEY))
                .urlPatterns(urlPatterns);
        return HttpFilterAPI.createJsonWebTokenFilterRegistration(builder);
    }



    /**
     * @return
     * @Title registeringCORSFilter
     * @Description 注册跨域处理过滤器(接受跨域请求)
     * @author Han
     * @date 2017年10月25日 上午11:54:35
     * @version V1.0.0
     * @history 版本 作者 时间 注释
     */
    @Bean
    public FilterRegistrationBean registeringCORSFilter() {
        return HttpFilterAPI.createCorsProxyFilterRegistration(CorsProxyFilter.builder());
    }

}

 

 

 

 

你可能感兴趣的:(解决跨域问题)