添加过滤器
This commit is contained in:
@@ -1,34 +1,39 @@
|
||||
package com.micro.fast.gateway.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
/**
|
||||
* 网关web配置类
|
||||
* @author lsy
|
||||
*/
|
||||
@Configuration
|
||||
public class WebConfig extends WebMvcConfigurerAdapter{
|
||||
|
||||
public class WebConfig{
|
||||
/**
|
||||
* 网关支持跨域配置,跨域问题需要在网关解决
|
||||
* @param registry
|
||||
* 网关支持跨域配置,跨域问题需要在网关解决,cors需要浏览器和服务端同时解决.
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry
|
||||
//允许请求的路径
|
||||
.addMapping("/**")
|
||||
//能给这个服务器发起请求的url
|
||||
.allowedOrigins("*")
|
||||
//允许的请求头为全部
|
||||
.allowedHeaders("*")
|
||||
//允许cookie跨域
|
||||
.allowCredentials(true)
|
||||
//允许的请求类型
|
||||
.allowedMethods("GET", "POST", "DELETE", "PUT")
|
||||
//预请求的校验时间间隔为6小时
|
||||
.maxAge(60*60*6L);
|
||||
@Bean
|
||||
@Order(Integer.MAX_VALUE)
|
||||
public CorsFilter corsFilter() {
|
||||
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
|
||||
final CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
//允许cookie跨域,在cookie跨域的时候,服务端进行设置,浏览器端也要进行显式设置是否发送。
|
||||
corsConfiguration.setAllowCredentials(true);
|
||||
//能给这个服务器发起请求的url,当这个值设置为*的时候,cookie就不能随着请求进行发送了。因为cookie必须是同源的。
|
||||
corsConfiguration.addAllowedOrigin("*");
|
||||
//允许的请求头为全部
|
||||
corsConfiguration.addAllowedHeader("*");
|
||||
//允许的请求类型
|
||||
corsConfiguration.addAllowedMethod("*");
|
||||
//预请求的校验时间间隔为6小时
|
||||
corsConfiguration.setMaxAge(60*60*6L);
|
||||
//允许请求的路径
|
||||
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
|
||||
return new CorsFilter(urlBasedCorsConfigurationSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.micro.fast.gateway.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 对options类型的跨域预请求进行快速处理,未启动
|
||||
* @author lsy
|
||||
*/
|
||||
public class PreRequestFilter extends ZuulFilter {
|
||||
|
||||
/**
|
||||
* 对options类型的跨域预请求进行快速处理
|
||||
*/
|
||||
private static final String OPTIONS = "OPTIONS";
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return "pre";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
HttpServletRequest request = requestContext.getRequest();
|
||||
String method = request.getMethod();
|
||||
if (OPTIONS.equals(method)){
|
||||
//不进行后续操作,直接过滤请求,返回200状态码
|
||||
requestContext.setSendZuulResponse(false);
|
||||
requestContext.setResponseStatusCode(HttpStatus.OK.value());
|
||||
}else{
|
||||
//对请求进行后续处理
|
||||
requestContext.setSendZuulResponse(true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user