• Spring Cloud Alibaba Gateway全局token过滤、局部过滤访问时间超过50ms日志提示


    Spring Cloud Alibaba Gateway验证token

    基础搭建
    前篇

    在前篇的基础上加入依赖

    <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.8.18</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在filter包中创建tokenFilter

    @Component
    @Slf4j
    public class TokenFilter implements GlobalFilter, Ordered {
        @Value("${token.key}")
        private String tokenKey;
        private static final String TOKEN = "token";
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
            //生成一个token
    /*        Map map = new HashMap<>();
            map.put("id", 100);
            map.put("nickName", "张三");
            map.put("tel", "10086");
            map.put(JWT.EXPIRES_AT, System.currentTimeMillis() + 2 * 24 * 6 * 6 * 1000);
            String token1 = JWTUtil.createToken(map, tokenKey.getBytes());*/
    
            List<String> strings = exchange.getRequest().getHeaders().get(TOKEN);
            ServerHttpResponse response = exchange.getResponse();
    
            if (ObjectUtil.isEmpty(strings)) {
                log.debug("token为空,不予通行");
    
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
            String token = strings.get(0);
            boolean verify = false;
            try {
                verify = JWTUtil.verify(token, tokenKey.getBytes(StandardCharsets.UTF_8));
            } catch (Exception ex) {
                log.error("出现错误{}", ex.getMessage());
                ex.printStackTrace();
            }
    
            if (!verify) {
                log.error("token不对,不要投机取巧");
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();
            }
    
            JWTPayload payload = JWTUtil.parseToken(token).getPayload();
            log.info("通过,大大滴好!!");
            return chain.filter(exchange);
        }
    
        //配置第一顺位执行
        @Override
        public int getOrder() {
            return 1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    自行访问
    在这里插入图片描述

    Spring Cloud Alibaba Gateway局部过滤

    1.继承AbstractGatewayFilterFactory

    加入配置
    spring.cloud.gateway.routes[0].filters[4] = LogTime=50

    • 这里配置的LogTime 与 类名前面的对应
    • AbstractNameValueGatewayFilterFactory可以仿照这个类写 进入ieda 两下shift
    @Component
    @Slf4j
    public class LogTimeGatewayFilterFactory extends AbstractGatewayFilterFactory<LogTimeGatewayFilterFactory.Config> {
    
        private String TIMESPAN = "timeSpan";
        public LogTimeGatewayFilterFactory() {
            super(LogTimeGatewayFilterFactory.Config.class);
        }
        public List<String> shortcutFieldOrder() {
            return Arrays.asList(TIMESPAN);
        }
        @Override
        public GatewayFilter apply(Config config) {
            return new GatewayFilter() {
                @Override
                public Mono<Void> filter(ServerWebExchange exchange,GatewayFilterChain chain) {
                    long start = System.currentTimeMillis();
                    exchange.getAttributes().put("start", start);
                    return chain.filter(exchange).then(Mono.fromRunnable(()->{
                        long end = System.currentTimeMillis();
                        long start1 = exchange.getAttribute("start");
                        HttpRequest request = exchange.getRequest();
                        URI uri = request.getURI();
                        if(config.timeSpan > (end - start1)){
                            log.info("uri伟{}不错!{}在50ms之内",uri,end - start1);
                        }
                        log.info("uri伟{}不行!太慢啦{}",uri,end - start1);
    
                    }));
                }
            };
        }
        @Validated
        //启用参数验证似乎
        public static class Config{
            private int timeSpan;
    
            public int getTimeSpan() {
                return timeSpan;
            }
    
            public void setTimeSpan(int timeSpan) {
                this.timeSpan = timeSpan;
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2.仿照AddRequestHeaderGatewayFilterFactory

    继承AbstractNameValueGatewayFilterFactory

    @Component
    @Slf4j
    public class LogTime2GatewayFilterFactory extends AbstractNameValueGatewayFilterFactory{
    
        public LogTime2GatewayFilterFactory() {
        }
    
        public GatewayFilter apply(final AbstractNameValueGatewayFilterFactory.NameValueConfig config) {
            return new GatewayFilter() {
                @Override
                public Mono<Void> filter(ServerWebExchange exchange,GatewayFilterChain chain) {
                    long start = System.currentTimeMillis();
                    exchange.getAttributes().put("start", start);
                    return chain.filter(exchange).then(Mono.fromRunnable(()->{
                        long end = System.currentTimeMillis();
                        long start1 = exchange.getAttribute("start");
                        HttpRequest request = exchange.getRequest();
                        URI uri = request.getURI();
                        if(Integer.parseInt(config.getValue()) > (end - start1)){
                            log.info("logTime2 uri伟{}不错!{}在50ms之内",uri,end - start1);
                        }
                        log.info("logTime2 uri伟{}不行!太慢啦{}",uri,end - start1);
    
                    }));
                }
    
                public String toString() {
                    return GatewayToStringStyler.filterToStringCreator(LogTime2GatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();
                }
            };
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    证明参数注入成功
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    成功解决/bin/sh: cc: command not found和/bin/sh: g++: command not found
    APUE 第4章: cp命令实现,同时去除文件中的空洞
    MFC中的类继承图的基本框架
    北邮 数字系统设计 10 Memory
    MySQL 百万级/千万级表 总记录数查询
    06公共方法
    北大C++课后记录:文件读写的I/O流
    java生成验证码返回前端图片,后端通过redis存储和校验
    微服务-gateway跨域配置
    云原生可观测性平台-云监控
  • 原文地址:https://blog.csdn.net/weixin_63558979/article/details/133179935