zuul返回302与位置标题

时间:2017-07-11 03:04:27

标签: spring-boot netflix-zuul

我有一个简单的用例,如果请求在网址包含 / logout 时进入网关,我需要使用Location标头发送302 HTTP状态。这必须在不路由到后端服务的情况下发生。

以下是我的zuul fillter:

public class LogoutFillter extends ZuulFilter{

    @Override
    public boolean shouldFilter() {         
        if(RequestContext.getCurrentContext().getRequest().getRequestURI().toLowerCase().contains("/logout")){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public String filterType() {
        return "pre";
    }

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

    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();        
        HttpSession excistingSession = context.getRequest().getSession(false);
        if(excistingSession != null){
            excistingSession.invalidate();
            context.unset()
            //context.addZuulResponseHeader("Location", "/abc/def/logout.do"); //notworking
            context.setResponseStatusCode(302);         
        }
        return null;
    }
}

我试图像下面这样做:

HttpServletResponse response = context.getResponse();
response.setStatus(302)
response.setHeader("Location", "/abc/logout.to");
context.unset();
context.setResponse(response);

然而,这也没有奏效。任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

最后,我设法解决了这个问题。以下是我的代码

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();

    HttpSession excistingSession = context.getRequest().getSession(false);
    if(excistingSession != null){
        excistingSession.invalidate();  
        context.setSendZuulResponse(false);
        context.addZuulResponseHeader("Location", "/abc/def/logout.do");
        context.setResponseStatusCode(HttpServletResponse.SC_MOVED_TEMPORARILY);
    }
    return null;
}

在上面的代码中,setSendZuulResponse(false)将停止路由到后端服务,addZuulResponseHeader将添加响应标头。