• springcloud<zuul过滤器简单配置与跨域设置>


    package com.wangbiao.config;
    
    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.context.RequestContext;
    import com.netflix.zuul.exception.ZuulException;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
    import org.springframework.stereotype.Component;
    
    import javax.servlet.http.HttpServletRequest;
    
    import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORWARD_TO_KEY;
    import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
    
    /**
     * TODO
     *
     * @author wangbiao
     * @Title TODO
     * @module TODO
     * @description 过滤器
     * @date 2021/4/10 22:21
     */
    @Slf4j
    @Component
    public class TokenFilter  extends ZuulFilter {
        @Override
        public String filterType() {
            return FilterConstants.PRE_TYPE;//pre:路由之前;routing:路由之时;post: 路由之后;error:发送错误调用
        }
    
        @Override
        public int filterOrder() {
          return   FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
    //        return 0;// 优先级0,数字越大,优先级越低
        }
    
        @Override
        public boolean shouldFilter() {
    //        RequestContext ctx = RequestContext.getCurrentContext();
    //        return !ctx.containsKey(FORWARD_TO_KEY) // 判断filter是否被转发
    //                && !ctx.containsKey(SERVICE_ID_KEY); // 判断filter是否定义了serviceId
    
    
            return true;// 是否执行该过滤器,此处为true为过滤
        }
    
    //    shouldFilter() 为true 此方法执行
        @Override
        public Object run() throws ZuulException {
            RequestContext ctx = RequestContext.getCurrentContext();
            ctx.getResponse().setCharacterEncoding("gbk");
    
            HttpServletRequest request = ctx.getRequest();
            log.info(String.format("%s -----------> %s", request.getMethod(), request.getRequestURL().toString()));
    
            Object accessToken = request.getParameter("token");// 获取请求的参数
            if(accessToken == null) {
                ctx.setSendZuulResponse(false);// 是否允许该请求对下游进行路由
                ctx.setResponseStatusCode(401);// 返回错误码
                ctx.setResponseBody("{"status":401,"message":"token为空!"}");//返回错误内容
    
                //或者也可以后端打印下try {ctx.getResponse().getWriter().write("token is null");}catch (Exception e){}
    
                //filter-is-success保存于上下文,作为同类型下游Filter的执行开关
                ctx.set("filter-is-success", false);
                return null;
            }
    //        ctx.addZuulRequestHeader("phone", phone);
    //        ctx.addZuulRequestHeader("customerId", customerId);
            ctx.set("filter-is-success", true);
            return null;
        }
    }
    

     package com.wangbiao.config;

    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;

    /**
    * TODO
    *
    * @author wangbiao
    * @Title TODO
    * @module TODO
    * @description 跨域请求
    * @date 2021/4/11 12:05
    */
    @Configuration
    public class DiyCorsConfiguration {
    @Bean
    public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);//允许cookies跨域
    config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    config.setMaxAge(3600L);
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);
    }
    }

    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    100个精彩的开源游戏
    poj 2104 K-th Number
    Redis源代码分析-内存数据结构intset
    android音乐播放器开发 SweetMusicPlayer 实现思路
    MySQL 二进制日志(Binary Log)
    Node.js 博客实例(六)留言功能
    HBase总结(十二)Java API 与HBase交互实例
    window+Apache 配置虚拟主机(2)
    Web Service那点事
    JSP基本语法
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/14644825.html
Copyright © 2020-2023  润新知