Spring-Cloud-Zuul-Gateway Configuration

Spring-Cloud-Zuul-Interface Gateway
1. How to use
1. Add @EnableZuulPorxy
@EnableZuulProxy
@SpringBootApplication
public class ZuulApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApp.class).web( true).run(args);
}
//When creating a Zuul filter, you need to create the following content
@Bean
public LoginFilter newLoginFilter(){
return new LoginFilter();
}
}
2.application.properties
# routes to serviceId Here is through The serviceid is used to bind the address. When /api-a/ is added after the path, the service corresponding to service-A is accessed.
zuul.routes.api-a.path=/api-zuul/**
zuul.routes.api-a.serviceId=server-ribbonservice
zuul.routes.api-b.path=/api-zuul1/**
zuul.routes.api-b.serviceId=server-ribbonservice1
3.pom.xml
<dependencies>
<!-- 添加对Web的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 要注册的需要引用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId><dependency><!-- Api interface gateway-->
</dependency>


<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4.Zuul过滤器
1.过滤器需继承ZuulFilter基类
package com.cn.filter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

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

public class LoginFilter extends ZuulFilter{

public Object run() {
System.out.println("Enter gateway filter======");
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest httpServletRequest = requestContext.getRequest();
// TODO Auto-generated method stub
Cookie[] cookie = httpServletRequest.getCookies();
for (Cookie cookie2 : cookie) {
System.out.println(" key " + cookie2.getName() + " Value " + cookie2.getValue());
if( "token".equals(cookie2.getName())){
requestContext.setSendZuulResponse(true);
requestContext.setResponseStatusCode(200);
// requestContext.setResponseBody("Current user login allows operation");
System.out.println(" Get the value from the cookie, normal request!! value " + cookie2.getValue());
return null;
}
}
System.out.println("The relevant information is not obtained in the background------->");
requestContext.setSendZuulResponse(false);
requestContext.setResponseStatusCode(401);
requestContext.setResponseBody("The current user is not logged in and not allowed Other operations");
return null;
}
//;// Whether to execute the filter, here is true, indicating that the
public boolean needs to be filtered shouldFilter() {
// TODO Auto-generated method stub
return true;
}
// Priority 0, the higher the number, the lower the priority
@Override
public int filterOrder() {
// TODO Auto-generated method stub
return 0;
}

@Override
public String filterType() {
// TODO Auto-generated method stub
return "pre";// pre-filter
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297480&siteId=291194637