package com.ruoyi.framework.config;
import com.ruoyi.framework.Interceptor.PlusWebInvokeTimeInterceptor;
import com.yomahub.tlog.web.interceptor.TLogWebInterceptor;
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;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 通用é…ç½®
*
* @author Lion Li
*/
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 全局链路跟踪拦截器
registry.addInterceptor(new TLogWebInterceptor());
// 全局访问性能拦截
registry.addInterceptor(new PlusWebInvokeTimeInterceptor());
}
/**
* 跨域é…ç½®
*/
@Bean
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
// 设置访问æºåœ°å€
config.addAllowedOriginPattern("*");
// 设置访问æºè¯·æ±‚头
config.addAllowedHeader("*");
// 设置访问æºè¯·æ±‚方法
config.addAllowedMethod("*");
// 有效期 1800秒
config.setMaxAge(1800L);
// æ·»åŠ æ˜ å°„è·¯å¾„ï¼Œæ‹¦æˆªä¸€åˆ‡è¯·æ±‚
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
// 返回新的CorsFilter
return new CorsFilter(source);
}
}