zuul,过滤器

阿潇个人最懂版本

  • 启动类加注解 @EnableZuulProxy
    若使用redis则加入(开启spring-session和redis的注解)@EnableRedisHttpSession

  • zuul 依赖

        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
  • zuul 完整的pom.xml
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
        
    
    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    


    

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.springframework.session
            spring-session-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

    

    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  • application.yml
#注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8762/eureka/

#端口号
server:
  port: 8899

#名字
spring:
  application:
    name: ec-zuul

#配置redis
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 12345
    jedis:
      pool:
        max-active: 8
    timeout: 50000ms


#路由转发
# 结合eureka 使用
zuul:
  routes:
#   可以随便起,但是规范是与服务名一样
    ec-user:
#     拦截的路径 可以随便起,但是规范是与服务名一样
      path: /ec-user/**
#     具体服务可以小写
      serviceId: EC-USER
#设置cookie可以传递
  sensitive-headers:
  • 过滤器创建类AuthFilter(类名必须为AuthFilter)
package com.mengmaec.eczuul;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVLET_DETECTION_FILTER_ORDER;

/**
 * @Auther: 18358
 * @Date: 2018/12/18 15:35
 * @Description:
 */
@Component
public class AuthFilter extends ZuulFilter {

    //    过滤器类型
    @Override
    public String filterType() {
        return PRE_TYPE;
    }

//    过滤的顺序 越小越靠前
    @Override
    public int filterOrder() {
        return SERVLET_DETECTION_FILTER_ORDER - 1;
    }

    //    是否执行 具体的过滤逻辑 返回true==执行 ,反之
//    默认true 中间可写具体需要拦截的服务
    @Override
    public boolean shouldFilter() {
        //        获取用户请求地址,判断是否是需要过滤的地址

        //共享RequestContext,上下文对象
//        RequestContext requestContext = RequestContext.getCurrentContext();
//        HttpServletRequest request = requestContext.getRequest();
//        System.out.println(request.getRequestURI());
//        String uri = request.getRequestURI();
//        if (uri.equals("/ec-user/user/get_user_info")||uri.equals("/ec-user/user/logout")) {
//            return true;
//        }
//        return false;
        
        return true;
    }

    //    过滤逻辑
    @Override
    public Object run() throws ZuulException {
        //        判断用户具有什么权限
        //JWT
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();

        String uri = request.getRequestURI();
        HttpSession session = request.getSession();

        //需要权限校验的接口
        if (uri.equals("/ec-user/user/get_user_info")||uri.equals("/ec-user/user/logout"))
            //空 代表没有登录
            if (session.getAttribute("currentUser") == null) {
                // 过滤该请求,不对其进行路由
                requestContext.setSendZuulResponse(false);
                //返回错误代码
                requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
            }
        return null;
    }

}

你可能感兴趣的:(zuul,过滤器)