美文网首页
spring cloud gateway简单教程

spring cloud gateway简单教程

作者: TinyThing | 来源:发表于2020-04-02 17:03 被阅读0次

0x0 构建spring cloud gateway工程

以IDEA为例:

image.png

0x1 简单的反向代理

加入如下bean即可实现反向代理

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        // /gateway/xxx
        return builder.routes()
                //将/aaa代理到http://10.9.65.18:8081/bbb
                .route("path_route", r -> r.path("/aaa/**")
                        .filters(f -> f.rewritePath("/aaa/(?<segment>.*)", "/bbb/${segment}"))
                        .uri("http://10.9.65.18:8081/"))
                //将所有/ccc代理到http://10.9.65.18:8081/ccc
                .route(r -> r.path("/ccc/**")
                        .uri("http://10.9.65.18:8081/"))
                .build();
    }

0x2 权限过滤

根据token过滤权限,常见于jwt校验

    @Bean
    @Order(0)
    public GlobalFilter a() {
        return (exchange, chain) -> {
            log.info("a pre filter");

            ServerHttpRequest request = exchange.getRequest();
            HttpHeaders headers = request.getHeaders();
            String token = headers.getFirst("token");

            //验证通过
            if (validate(token)) {
                return chain.filter(exchange);
            }

            //见证不通过,则返回403
            ServerHttpResponse response = exchange.getResponse();
            response.setRawStatusCode(403);
            return response.setComplete();
        };
    }

重定向到登录页的写法

            //验证不通过,则跳转登录页
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.SEE_OTHER);
            String redirectUrl = request.mutate().path(LOGIN_PATH).build().getURI().toString();

            response.getHeaders().set(HttpHeaders.LOCATION, redirectUrl);

0x1 官网例子

官方网站上给出的例子几乎能够满足我们常见需求:

@SpringBootApplication
public class DemogatewayApplication {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
            .route("path_route", r -> r.path("/get")
                .uri("http://httpbin.org"))
            .route("host_route", r -> r.host("*.myhost.org")
                .uri("http://httpbin.org"))
            .route("rewrite_route", r -> r.host("*.rewrite.org")
                .filters(f -> f.rewritePath("/foo/(?<segment>.*)", "/${segment}"))
                .uri("http://httpbin.org"))
            .route("hystrix_route", r -> r.host("*.hystrix.org")
                .filters(f -> f.hystrix(c -> c.setName("slowcmd")))
                .uri("http://httpbin.org"))
            .route("hystrix_fallback_route", r -> r.host("*.hystrixfallback.org")
                .filters(f -> f.hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")))
                .uri("http://httpbin.org"))
            .route("limit_route", r -> r
                .host("*.limited.org").and().path("/anything/**")
                .filters(f -> f.requestRateLimiter(c -> c.setRateLimiter(redisRateLimiter())))
                .uri("http://httpbin.org"))
            .build();
    }
}

相关文章

网友评论

      本文标题:spring cloud gateway简单教程

      本文链接:https://www.haomeiwen.com/subject/bfjxphtx.html