【Gateway】网关

在这里插入图片描述
在这里插入图片描述
请添加图片描述
在这里插入图片描述
请添加图片描述
在这里插入图片描述
在这里插入图片描述


<dependencies>

    <dependency>

        <groupId>com.atguigu</groupId>

        <artifactId>common_utils</artifactId>

        <version>0.0.1-SNAPSHOT</version>

    </dependency>


    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

    </dependency>


    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-gateway</artifactId>

    </dependency>


    <!--gson-->

    <dependency>

        <groupId>com.google.code.gson</groupId>

        <artifactId>gson</artifactId>

    </dependency>


    <!--服务调用-->

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-openfeign</artifactId>

    </dependency>

</dependencies>

在这里插入图片描述

# 服务端口

server.port=8222

# 服务名

spring.application.name=service-gateway


# nacos服务地址

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848


#使用服务发现路由

spring.cloud.gateway.discovery.locator.enabled=true

#服务路由名小写

#spring.cloud.gateway.discovery.locator.lower-case-service-id=true


#设置路由id

spring.cloud.gateway.routes[0].id=service-acl

#置路由的uri

spring.cloud.gateway.routes[0].uri=lb://service-acl

#设置路由断言,代理servicerId为auth-service的/auth/路径

spring.cloud.gateway.routes[0].predicates= Path=/*/acl/**


#配置service-edu服务

spring.cloud.gateway.routes[1].id=service-edu

spring.cloud.gateway.routes[1].uri=lb://service-edu

spring.cloud.gateway.routes[1].predicates= Path=/eduservice/**


#配置service-ucenter服务

spring.cloud.gateway.routes[2].id=service-ucenter

spring.cloud.gateway.routes[2].uri=lb://service-ucenter

spring.cloud.gateway.routes[2].predicates= Path=/ucenterservice/**


#配置service-ucenter服务

spring.cloud.gateway.routes[3].id=service-cms

spring.cloud.gateway.routes[3].uri=lb://service-cms

spring.cloud.gateway.routes[3].predicates= Path=/cmsservice/**

spring.cloud.gateway.routes[4].id=service-msm

spring.cloud.gateway.routes[4].uri=lb://service-msm

spring.cloud.gateway.routes[4].predicates= Path=/edumsm/**

spring.cloud.gateway.routes[5].id=service-order

spring.cloud.gateway.routes[5].uri=lb://service-order

spring.cloud.gateway.routes[5].predicates= Path=/orderservice/**

spring.cloud.gateway.routes[6].id=service-order

spring.cloud.gateway.routes[6].uri=lb://service-order

spring.cloud.gateway.routes[6].predicates= Path=/orderservice/**

spring.cloud.gateway.routes[7].id=service-oss

spring.cloud.gateway.routes[7].uri=lb://service-oss

spring.cloud.gateway.routes[7].predicates= Path=/eduoss/**


spring.cloud.gateway.routes[8].id=service-statistic

spring.cloud.gateway.routes[8].uri=lb://service-statistic

spring.cloud.gateway.routes[8].predicates= Path=/staservice/**


spring.cloud.gateway.routes[9].id=service-vod

spring.cloud.gateway.routes[9].uri=lb://service-vod

spring.cloud.gateway.routes[9].predicates= Path=/eduvod/**


spring.cloud.gateway.routes[10].id=service-edu

spring.cloud.gateway.routes[10].uri=lb://service-edu

spring.cloud.gateway.routes[10].predicates= Path=/eduuser/**

在这里插入图片描述


server:

  port: 8222


spring:

  application:

  cloud:

    gateway:

      discovery:

        locator:

          enabled: true

      routes:
      - id: SERVICE-ACL
        uri: lb://SERVICE-ACL
        predicates:
        - Path=/*/acl/** # 路径匹配
      - id: SERVICE-EDU
        uri: lb://SERVICE-EDU
        predicates:
        - Path=/eduservice/** # 路径匹配
      - id: SERVICE-UCENTER
        uri: lb://SERVICE-UCENTER
        predicates:
        - Path=/ucenter/** # 路径匹配
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

在这里插入图片描述
在这里插入图片描述

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {

        CorsConfiguration config = new CorsConfiguration();

        config.addAllowedMethod("*");

        config.addAllowedOrigin("*");

        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());

        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);

    }
}

在这里插入图片描述

import com.google.gson.JsonObject;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;

import org.springframework.cloud.gateway.filter.GlobalFilter;

import org.springframework.core.Ordered;

import org.springframework.core.io.buffer.DataBuffer;

import org.springframework.http.server.reactive.ServerHttpRequest;

import org.springframework.http.server.reactive.ServerHttpResponse;

import org.springframework.stereotype.Component;

import org.springframework.util.AntPathMatcher;

import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

import java.util.List;

/**

 * <p>

 * 全局Filter,统一处理会员登录与外部不允许访问的服务

 * </p>

 */

@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {


    private AntPathMatcher antPathMatcher = new AntPathMatcher();


    @Override

    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        ServerHttpRequest request = exchange.getRequest();

        String path = request.getURI().getPath();

        //谷粒学院api接口,校验用户必须登录

        if(antPathMatcher.match("/api/**/auth/**", path)) {

            List<String> tokenList = request.getHeaders().get("token");
            
            if(null == tokenList) {

                ServerHttpResponse response = exchange.getResponse();

                return out(response);

            } else {

//                Boolean isCheck = JwtUtils.checkToken(tokenList.get(0));

//                if(!isCheck) {

                    ServerHttpResponse response = exchange.getResponse();

                    return out(response);

//                }

            }

        }

        //内部服务接口,不允许外部访问

        if(antPathMatcher.match("/**/inner/**", path)) {

            ServerHttpResponse response = exchange.getResponse();

            return out(response);

        }

       return chain.filter(exchange);

    }


    @Override
    public int getOrder() {

        return 0;

    }

    private Mono<Void> out(ServerHttpResponse response) {

        JsonObject message = new JsonObject();

        message.addProperty("success", false);

        message.addProperty("code", 28004);

        message.addProperty("data", "鉴权失败");

        byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8);

        DataBuffer buffer = response.bufferFactory().wrap(bits);

        //response.setStatusCode(HttpStatus.UNAUTHORIZED);

        //指定编码,否则在浏览器中会中文乱码

        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");

        return response.writeWith(Mono.just(buffer));

    }

}

在这里插入图片描述


import org.springframework.beans.factory.ObjectProvider;

import org.springframework.boot.autoconfigure.web.ResourceProperties;

import org.springframework.boot.autoconfigure.web.ServerProperties;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.boot.web.reactive.error.ErrorAttributes;

import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.Ordered;

import org.springframework.core.annotation.Order;

import org.springframework.http.codec.ServerCodecConfigurer;

import org.springframework.web.reactive.result.view.ViewResolver;
import java.util.Collections;
import java.util.List;

/**

 * 覆盖默认的异常处理

 *

 */

@Configuration

@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})

public class ErrorHandlerConfig {

    private final ServerProperties serverProperties;

    private final ApplicationContext applicationContext;

    private final ResourceProperties resourceProperties;


    private final List<ViewResolver> viewResolvers;


    private final ServerCodecConfigurer serverCodecConfigurer;


    public ErrorHandlerConfig(ServerProperties serverProperties,

                                     ResourceProperties resourceProperties,

                                     ObjectProvider<List<ViewResolver>> viewResolversProvider,

                                        ServerCodecConfigurer serverCodecConfigurer,

                                     ApplicationContext applicationContext) {

        this.serverProperties = serverProperties;

        this.applicationContext = applicationContext;

        this.resourceProperties = resourceProperties;

        this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);

        this.serverCodecConfigurer = serverCodecConfigurer;

    }


    @Bean

    @Order(Ordered.HIGHEST_PRECEDENCE)

    public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {

        JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(

                errorAttributes,

                this.resourceProperties,

                this.serverProperties.getError(),

                this.applicationContext);

        exceptionHandler.setViewResolvers(this.viewResolvers);

        exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());

        exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());

        return exceptionHandler;

    }

}

在这里插入图片描述


import org.springframework.boot.autoconfigure.web.ErrorProperties;

import org.springframework.boot.autoconfigure.web.ResourceProperties;

import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;

import org.springframework.boot.web.reactive.error.ErrorAttributes;

import org.springframework.context.ApplicationContext;

import org.springframework.http.HttpStatus;

import org.springframework.web.reactive.function.server.*;

import java.util.HashMap;

import java.util.Map;

/**

 * 自定义异常处理

 *

 * <p>异常时用JSON代替HTML异常信息<p>

 *

 */

public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {

    public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,

                                ErrorProperties errorProperties, ApplicationContext applicationContext) {

        super(errorAttributes, resourceProperties, errorProperties, applicationContext);

    }


    /**

     * 获取异常属性

     */

    @Override
    protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
        Map<String, Object> map = new HashMap<>();

        map.put("success", false);

        map.put("code", 20005);

        map.put("message", "网关失败");

        map.put("data", null);

        return map;

    }

    /**

     * 指定响应处理方法为JSON处理的方法

     * @param errorAttributes

     */

    @Override

    protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {

        return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);

    }

    /**

     * 根据code获取对应的HttpStatus

     * @param errorAttributes

     */

    @Override

    protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {

        return HttpStatus.OK;

    }

}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_树先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值