SpringCloud网关部分初学,设置了网关过滤器,但是访问该路由无法跳转

发布于 2022-09-12 23:56:13 字数 3872 浏览 18 评论 0

问题描述

SpringCloud网关部分初学,设置了网关过滤器,但是访问该路由无法跳转

相关文件

BulldozerGatewayApplication.java

package com.bulldozer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication

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

JwtCheckGatewayFilterFactory

package com.bulldozer.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * JWT验证的过滤器
 *
 * @author 
 * @create 2018-09-09 下午10:05
 **/
public class JwtCheckGatewayFilterFactory  extends AbstractGatewayFilterFactory<JwtCheckGatewayFilterFactory.Config> {

    public JwtCheckGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            String jwtToken = exchange.getRequest().getHeaders().getFirst("Authorization");
            //校验jwtToken的合法性
            if (jwtToken != null) {
                // 合法
                // 将用户id作为参数传递下去
                return chain.filter(exchange);
            }

            //不合法(响应未登录的异常)
            ServerHttpResponse response = exchange.getResponse();
            //设置headers
            HttpHeaders httpHeaders = response.getHeaders();
            httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
            httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
            //设置body
            String warningStr = "未登录或登录超时";
            DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes());

            return response.writeWith(Mono.just(bodyDataBuffer));
        };
    }

    public static class Config {
        //Put the configuration properties for your filter here
    }
}

AppConfig

package com.bulldozer.config;

import com.bulldozer.filter.JwtCheckGatewayFilterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public JwtCheckGatewayFilterFactory jwtCheckGatewayFilterFactory(){
        return new JwtCheckGatewayFilterFactory();
    }
}

application.yml

server:
  port: 9093
spring:
  application:
    name: bulldozer-cloud-gateway
  cloud:
    gateway:
      routes:
        #netty 路由过滤器,http或https开头 lb://代表是eureka服务的名称  predicates:表示会过滤掉的请求头
        - id: gateway-route
          uri: lb://server-client
          predicates:
            - Path=/api/**,/route-api/**
    #处理跨域请求问题
    globalcors:
      cors-configurations:
        '[/**]':
          allowedOrigins: "*"
          allowedMethods: "*"
  #redis配置
  redis:
    host:
    port: 6379
    password:
    database: 1
    timeout: 60s
    ## springboot2.0之后将连接池由jedis改为lettuce
    lettuce:
      pool:
        max-idle: 30
        max-active: 8
        max-wait: 10000
        min-idle: 10

jwt.properties

jwt.ignoreUrlList=/route-api/login,/route-api/refresh

#token过期时间:30分钟
token.expire.time=1800000

#refreshToken过期时间:12小时
refresh.token.expire.time=43200000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文