4.zuul网关

时间:2018-04-24 17:05:02 阅读:171次
zuul网关

pom.xml

<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>
  <groupId>com.henan28.zuul</groupId>
  <artifactId>firstzuul</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    	<parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>2.0.0.RELEASE</version>
	</parent>
	
	  
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	
	<dependencyManagement>
	    <dependencies>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-dependencies</artifactId>
	            <version>Finchley.M9</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
	    </dependencies>
	</dependencyManagement>

	<dependencies>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-eureka</artifactId>
	        <version>2.0.0.M2</version>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-starter-zuul</artifactId>
	        <version>2.0.0.M2</version>
	    </dependency>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
		 </dependency>	 
	</dependencies>
	<repositories>
    	<repository>
	        <id>spring-milestones</id>
	        <name>Spring Milestones</name>
	        <url>https://repo.spring.io/milestone</url>
	        <snapshots>
	            <enabled>false</enabled>
	        </snapshots>
	    </repository>
	</repositories>
</project>


application.properties

spring.application.name=firstzuul
server.port=8769
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
zuul.routes.api-a.path=/api-member/**
zuul.routes.api-a.service-id=service-member

zuul.routes.api-b.path=/api-order/**
zuul.routes.api-b.service-id=service-order


app

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApp {
	public static void main(String[] args) {
		SpringApplication.run(ZuulApp.class, args);
	}
}


参数拦截及过滤

@Component
public class MyFilter extends ZuulFilter {
	public Object run() {
		System.out.println("------------网关-----------");
		RequestContext context = RequestContext.getCurrentContext();
		HttpServletRequest request =  context.getRequest();
		Object accessToken = request.getParameter("token");
		//不为空则放行
		if(accessToken!=null) {
			return null;
		}
		
		System.out.println("------------网关不通过-----------");
		//token为空  返回错误信息
		context.setSendZuulResponse(false);
		context.setResponseStatusCode(401);
		try {
			context.getResponse().getWriter().write("token is empty");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public boolean shouldFilter() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public int filterOrder() {
		// TODO Auto-generated method stub
		return 0;
	}

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

}


扫描二维码关注程序员爱笔记,接收更多资讯

评论

快速评论