SpringCloud(五)微服务网关Zuul之头信息经过网关丢失解决

解决思路

只要在过滤器中获取到需要的头信息让后再将其向下转发就可以了。
具体的zuul过滤器代码如下:
zuul网关中过滤器内容介绍

package com.diplomaproject.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }
/**
*种类一(无权限验证) --前端
*/
    @Override
    public Object run() throws ZuulException {
        //得到request上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //得到request域
        HttpServletRequest request = currentContext.getRequest();
        //得到头信息(填写你需要的头信息)
        String authorization = request.getHeader("Authorization");
        System.out.println("webfilter中获得的头信息="+authorization);
        //判断是否有
        if(authorization!=null&&"".equals(authorization)) {
            //将头信息继续向下传送
            currentContext.addZuulRequestHeader("Authorization",authorization);
        }
        return null;
    }
    /**
*种类二(含权限验证) --后台管理端
*/
 @Override
    public Object run() throws ZuulException {
        System.out.println("执行managerFilter过滤器。。。");
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
       /* *任何请求经过网关都转发两次
        zuul网关根据路径转发到指定服务的方法OPTIONS
        *请求一个服务时,首先会让OPTIONS方法来根据服务名称分发请求到指定服务,
        * 遇到此方法需要直接通过,因为此请求不带请求头
        **/
        if (request.getMethod().equals("OPTIONS")) {
            return null;
        }
        /**
         *需使登陆直接略过*/

        if (request.getRequestURI().indexOf("login") > 0) {
            return null;
        }


        String authorization = request.getHeader("Authorization");
        if (authorization != null && !"".equals(authorization)) {
            if (authorization.startsWith("Bearer ")) {
                String token = authorization.substring(7);
                try {
                    Claims claims = jwtUtil.parseJWT(token);
                    String role = (String) claims.get("roles");
                    if (role.equals("admin")) {
                        currentContext.addZuulRequestHeader("Authorization", authorization);
                        return null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    //终止运行
                    currentContext.setSendZuulResponse(false);
                }
            }
        }
        currentContext.setSendZuulResponse(false);
        currentContext.setResponseStatusCode(403);
        currentContext.setResponseBody("权限不足!");
        currentContext.getResponse().setContentType("text/html;charset=utf-8");
        return null;

    }
}

你可能感兴趣的:(springcloud)