spring cloud zuul filter 设置过滤请求

利用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();
    }


}

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

文章来自:http://www.cnblogs.com/wonder2636/p/7490319.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3