Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)

一、创建springBoot项目springcloud-zuul-server项目
Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)

专注于为中小企业提供成都网站设计、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业黄石港免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

二、配置文件

1、pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jane</groupId>
    <artifactId>springcloud-zuul-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springcloud-zuul-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、启动文件

package com.jane.springcloudzuulserver;

import com.jane.springcloudzuulserver.filter.TokenFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class SpringcloudZuulServerApplication {

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

    //将过滤器在spring生成
    @Bean
    public TokenFilter tokenFilter() {
        return new TokenFilter();
    }

}

3、application.properties

server.port=6030
spring.application.name=zuul-gateway
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:6001/eureka/
zuul.routes.springcloud-eureka-customer.path=/springcloud-eureka-customer/**

4、过滤器TokenFilter

package com.jane.springcloudzuulserver.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

import javax.servlet.http.HttpServletRequest;

public class TokenFilter extends ZuulFilter {

    //pre:主要用在路由映射的阶段是寻找路由映射表的
    //routing:具体的路由转发过滤器是在routing路由器,具体的请求转发的时候会调用
    //error:一旦前面的过滤器出错了,会调用error过滤器。
    //post:当routing,error运行完后才会调用该过滤器,是在最后阶段的
    @Override
    public String filterType() {
        return "pre";
    }

    //自定义过滤器执行的顺序,数值越大越靠后执行,越小就越先执行
    @Override
    public int filterOrder() {
        return 0;
    }

    //控制过滤器生效不生效,可以在里面写一串逻辑来控制,true是生效
    @Override
    public boolean shouldFilter() {
        return true;
    }

    //执行过滤逻辑
    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        //如果有授权服务器,这里可以做统一登陆授权校验
        String token = request.getParameter("token");
        if(token == null) {
            context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            context.setResponseBody("no token , Auth deny");
        }
        return null;
    }
}

三、启动文件,结合之前的文章 注册中心 服务提供者 服务消费,全部启动
1、服务如下图:
Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)

2、访问结果如下

没有token的请求:

Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)

有token的请求:

Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)

分享名称:Idea下SpringCloud2实验(四、Zuul网关实现路由转发和过滤)
当前URL:https://www.cdcxhl.com/article28/jgsccp.html

成都网站建设公司_创新互联,为您提供品牌网站制作微信公众号网站导航网站制作全网营销推广

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司