spring-cloud-zuul-Gateway Configuration

Source: Internet
Author: User
Tags stub

spring-cloud-zuul-Interface Gateway
1. How to use
1. Add @enablezuulporxy to the startup class
@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:
@Bean
Public Loginfilter Newloginfilter () {
return new Loginfilter ();
}
}
2.application.properties
# routes to ServiceId here the side is bound by the ServiceId address, when the path after adding/api-a/is to access service-a corresponding services.
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>
<!--add support for the 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>
<!--need to register for reference--
<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>
<!--dependency management for managing Spring-cloud dependencies, where CAMDEN.SR3 is the version number--
<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 Filter
1. Filter must inherit Zuulfilter base class
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 logon allow operation");
System.out.println ("Get value from cookie" Normal request!! Value "+ cookie2.getvalue ());
return null;
}
}
SYSTEM.OUT.PRINTLN ("Background information------->");
Requestcontext.setsendzuulresponse (FALSE);
Requestcontext.setresponsestatuscode (401);
Requestcontext.setresponsebody ("The current user is not logged in does not allow other actions");
return null;
}
;//Whether the filter is executed, true here, indicating the need to filter
public Boolean shouldfilter () {
TODO auto-generated Method Stub
return true;
}
Priority is 0, the higher the number, the lower the priority level
@Override
public int Filterorder () {
TODO auto-generated Method Stub
return 0;
}

@Override
Public String FilterType () {
TODO auto-generated Method Stub
return "pre";//Front Filter
}

}

spring-cloud-zuul-Gateway Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.