spring-cloud / spring-cloud-netflix

Integration with Netflix OSS components
http://cloud.spring.io/spring-cloud-netflix/
Apache License 2.0
4.87k stars 2.44k forks source link

zuul disable origin routing #2108

Closed ojith97 closed 7 years ago

ojith97 commented 7 years ago

i have a use case where if "/logout" request came to the filter i do not want to redirect it to the back-end service is there anyway to archive this below is my filler class

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 session = context.getRequest().getSession(false);
        if(session != null){
            session.invalidate();
            context.addZuulResponseHeader("Location", "/abcd");
            context.setResponseStatusCode(302);         
        }
        return null;
    }
}

so far i have tried "zuul.ignored-services" and "zuul.ignored-patterns" but both of them didn't work. what happens is that back-end service get called and it return 404. glad to hear any suggestions.

ryanjbaxter commented 7 years ago

Can you provide your configuration as well? When you tried zuul.ignored-services, what did that look like?

ojith97 commented 7 years ago

zuul.ignored-services="*"

ryanjbaxter commented 7 years ago

Just so I understand, /logout is not a service right? If so than I can see why zuul.ignored-services didnt work. And you are saying the filter you published above is not redirecting the request either?

ojith97 commented 7 years ago

while i was searching for a solution i found the below link

https://stackoverflow.com/questions/37243381/netflix-zuul-block-request-routing

according to the above link i have manage to route the request back to the client without routing to the back-end service. but the problem now i'm having is how to set the location header in the response. below is my current code

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

but the context.addZuulResponseHeader is not sending the header to the client

ojith97 commented 7 years ago

Tried the below HTTPResponse as well

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

and that didn't work either

ryanjbaxter commented 7 years ago

Maybe you could take a look at this filter as an example https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilter.java#L83

ojith97 commented 7 years ago

im not sure how this will solve my problem, because this is a post filter which will call after the request is routed to the back-end (as for my understanding). and also i want to send the location header in the response that comes in the request not by redirecting it.

ojith97 commented 7 years ago

finally i have manage to resolve this issue by my self below is my code

        @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;
    }