您好,登錄后才能下訂單哦!
一、創建springBoot項目springcloud-zuul-server項目
二、配置文件
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、服務如下圖:
2、訪問結果如下
沒有token的請求:
有token的請求:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。