10 changed files with 108 additions and 65 deletions
@ -0,0 +1,47 @@ |
|||||
|
package com.qs.cost.common.conf; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
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.multipart.MultipartResolver; |
||||
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2022/2/24 |
||||
|
*/ |
||||
|
@AllArgsConstructor |
||||
|
@Configuration(proxyBeanMethods = false) |
||||
|
public class SpringMvcConfig implements WebMvcConfigurer { |
||||
|
|
||||
|
|
||||
|
@Bean |
||||
|
public CorsFilter corsFilter() { |
||||
|
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); |
||||
|
final CorsConfiguration corsConfiguration = new CorsConfiguration(); |
||||
|
/*是否允许请求带有验证信息*/ |
||||
|
corsConfiguration.setAllowCredentials(false); |
||||
|
/*允许访问的客户端域名*/ |
||||
|
corsConfiguration.addAllowedOrigin("*"); |
||||
|
/*允许服务端访问的客户端请求头*/ |
||||
|
corsConfiguration.addAllowedHeader("*"); |
||||
|
/*允许访问的方法名,GET POST等*/ |
||||
|
corsConfiguration.addAllowedMethod("*"); |
||||
|
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); |
||||
|
return new CorsFilter(urlBasedCorsConfigurationSource); |
||||
|
} |
||||
|
|
||||
|
//@Bean
|
||||
|
public MultipartResolver multipartResolver(){ |
||||
|
CommonsMultipartResolver resolver = new CommonsMultipartResolver(); |
||||
|
resolver.setMaxInMemorySize(5120); |
||||
|
resolver.setMaxInMemorySize(300 * 1024 * 1024); |
||||
|
resolver.setDefaultEncoding("UTF-8"); |
||||
|
return resolver; |
||||
|
} |
||||
|
|
||||
|
} |
@ -1,42 +0,0 @@ |
|||||
package com.qs.cost.common.framework.mvc; |
|
||||
|
|
||||
import com.qs.cost.common.framework.interceptor.ForbiddenInterceptor; |
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean; |
|
||||
import org.springframework.context.annotation.Bean; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
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.WebMvcConfigurer; |
|
||||
|
|
||||
/** |
|
||||
* @Author JcYen |
|
||||
* @Date 2019/7/17 |
|
||||
* @Version 1.0 |
|
||||
*/ |
|
||||
@Component |
|
||||
public class SecurityMvcComponent implements WebMvcConfigurer { |
|
||||
|
|
||||
@Override |
|
||||
public void addInterceptors(InterceptorRegistry registry) { |
|
||||
//注册TestInterceptor拦截器
|
|
||||
//registry.addInterceptor(new ForbiddenInterceptor()).addPathPatterns("/**").excludePathPatterns("/api/**");
|
|
||||
} |
|
||||
|
|
||||
@Bean |
|
||||
public CorsFilter corsFilter() { |
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
|
||||
CorsConfiguration config = new CorsConfiguration(); |
|
||||
config.addAllowedOrigin("*"); |
|
||||
config.setAllowCredentials(true); |
|
||||
config.addAllowedHeader("*"); |
|
||||
config.addAllowedMethod("*"); |
|
||||
source.registerCorsConfiguration("/**", config); |
|
||||
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); |
|
||||
bean.setOrder(0); |
|
||||
return new CorsFilter(source); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
} |
|
Loading…
Reference in new issue