Spring-Cloud-Zuul-网关配置
Posted 钢巴得
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-Cloud-Zuul-网关配置相关的知识,希望对你有一定的参考价值。
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-网关配置的主要内容,如果未能解决你的问题,请参考以下文章
如何Spring Cloud Zuul作为网关的分布式系统中整合Swagger文档在同一个页面上
JAVA踩坑记录spring-cloud-zuul文件上传中文名乱码