1. 程式人生 > >spring cloud zuul filter 設置過濾請求

spring cloud zuul filter 設置過濾請求

util addheader type() nco 過濾 strong ger rediscli lean

利用spring cloud zuul 設置spring boot 中的filter ,出現了跨域問題,然後根據設置response可解決跨域,同時完成過濾請求

*********************************************************************************************************************

//主要執行的類
public class PreRequestLogFilter extends ZuulFilter {
/**
* Logger for this class
*/
private static final Logger logger = LoggerFactory.getLogger(PreRequestLogFilter.class);


@Override
public String filterType() {
System.out.println("**************filterType ");
return "pre";
}

@Override
public int filterOrder() {
System.out.println("**************filterOrder");
return 1;
}

@Override
public boolean shouldFilter() {
return true;
}

@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
logger.info("*************send {} request to {}",request.getMethod(),request.getRequestURL().toString());

Map<String, String[]> map= request.getParameterMap();

logger.info("請求request信息:【{}】",JSONObject.toJSONString(request.getParameterMap()));

String[] token = map.get("Token");

if(null == token || token.length == 0){
ctx.setSendZuulResponse(false);// 過濾該請求,不對其進行路由
ctx.setResponseStatusCode(200);// 返回錯誤碼
ctx.setResponseBody("{\"result\":\"token is not empty!\",\"resultCode\":\"1001\",\"resultMsg\":\"缺少授權的token值\"}");// 返回錯誤內容
ctx.set("isSuccess", false);

HttpServletResponse response = ctx.getResponse();
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setLocale(new java.util.Locale("zh","CN"));

}else if(null != token && !"".equals(token[0]) && RedisClientServer.get(token[0]) != null && !"".equals(RedisClientServer.get(token[0]))) {// 如果請求的參數不為空,且值為chhliu時,則通過
ctx.setSendZuulResponse(true);// 對該請求進行路由
ctx.setResponseStatusCode(200);
ctx.set("isSuccess", true);// 設值,讓下一個Filter看到上一個Filter的狀態
RedisClientServer.expire(token[0], 3600);
}else{
ctx.setSendZuulResponse(false);// 過濾該請求,不對其進行路由
ctx.setResponseStatusCode(200);// 返回錯誤碼
ctx.setResponseBody("{\"result\":\"token is not correct!\",\"resultCode\":\"1001\",\"resultMsg\":\"token不存在或會話已過期!\"}");// 返回錯誤內容
ctx.set("isSuccess", false);

HttpServletResponse response = ctx.getResponse();
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setLocale(new java.util.Locale("zh","CN"));
}
return null;

}

*********************************************************************************************************************

//主程序

@EnableZuulProxy
@SpringBootApplication
public class GatewayZuulApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayZuulApplication.class, args);
}

@Bean
public PreRequestLogFilter preRequestLogFilter() {
return new PreRequestLogFilter();
}


}

*********************************************************************************************************************

spring cloud zuul filter 設置過濾請求