码迷,mamicode.com
首页 > 编程语言 > 详细

Spring-Cloud-Zuul-网关配置

时间:2018-05-04 20:17:03      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:说明   相关信息   name   ide   rate   err   out   enable   访问   

Spring-Cloud-Zuul-接口网关
1.如何使用
1.在启动类中增加@EnableZuulPorxy
@EnableZuulProxy
@SpringBootApplication
public class ZuulApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ZuulApp.class).web(true).run(args);
}
//在创建Zuul过滤器时需要创建如下内容
@Bean
public LoginFilter newLoginFilter(){
return new LoginFilter();
}
}
2.application.properties
# routes to serviceId 这里边是通过serviceid来绑定地址,当在路径后添加/api-a/ 则是访问service-A对应的服务。
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接口网关 -->
<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("进入网关过滤器======");
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("当前用户登录允许操作");
System.out.println("从 cookie中获取到值啦 正常请求!!value " + cookie2.getValue());
return null;
}
}
System.out.println("后台未获取到相关信息------->");
requestContext.setSendZuulResponse(false);
requestContext.setResponseStatusCode(401);
requestContext.setResponseBody("当前用户未登录不允许其他操作");
return null;
}
//;// 是否执行该过滤器,此处为true,说明需要过滤
public boolean shouldFilter() {
// TODO Auto-generated method stub
return true;
}
// 优先级为0,数字越大,优先级越低
@Override
public int filterOrder() {
// TODO Auto-generated method stub
return 0;
}

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

}

Spring-Cloud-Zuul-网关配置

标签:说明   相关信息   name   ide   rate   err   out   enable   访问   

原文地址:https://www.cnblogs.com/lvwqq/p/8992193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!