paulrda
paulrda

Reputation: 61

Zuul proxy add parameter to request url

I use ZuulFilter to intercept request calls. I need to change the url of request call by adding additional parameter like this.

original request

Http://localhost:8080/home?username=Paul

after adding new parameter

Http://localhost:8080/home?username=Paul&authenticated=true

I don't need to add this additional parameter to the header. I tried using setRequestQueryParams to add my new parameter to request url but it didn't work.

Could you tell me how to add new parameter to url using zuul proxy?

Upvotes: 2

Views: 5183

Answers (3)

MrG
MrG

Reputation: 11

RequestContext ctx = RequestContext.getCurrentContext();
    String auth = "useeerrr" + ":" + "passsss";
    ctx.addZuulRequestHeader("Authorization", "Basic " +
            Base64Variants.MIME_NO_LINEFEEDS.encode(auth.getBytes(StandardCharsets.US_ASCII)));
    ctx.addZuulRequestHeader("X-USERNAME-HEADER","xxx");

    Map<String, List<String>> newParameterMap = new HashMap<>();
    Map<String, String[]> parameterMap = ctx.getRequest().getParameterMap();
    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
        String key = entry.getKey();
        String[] values = entry.getValue();
        newParameterMap.put(key, Arrays.asList(values));
    }
   

    HttpServletRequest request = ctx.getRequest();

    logger.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

    return null;

Upvotes: 0

pan
pan

Reputation: 1967

You could also use the ZuulFilter for this. Create ZuulFilter class:

public class AuthenticatedFilter extends ZuulFilter{

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

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

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    Map<String, List<String>> newParameterMap = new HashMap<>();
    Map<String, String[]> parameterMap = context.getRequest().getParameterMap();
    //getting the current parameter
    for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
      String key = entry.getKey();
      String[] values = entry.getValue();
      newParameterMap.put(key, Arrays.asList(values));
    }
    //add a new parameter
    String authenticatedKey = "authenticated";
    String authenticatedValue = "true";
    newParameterMap.put(authenticatedKey,Arrays.asList(authenticatedValue));
    context.setRequestQueryParams(newParameterMap);
    return null;
  }
}

And in your @SpringBootApplication add the filter as a bean

  @Bean
  public AuthenticatedFilter authenticatedFilter() {
    return new AuthenticatedFilter();
  }

Upvotes: 2

Fang.X
Fang.X

Reputation: 1

create a filter.

public class SimpleCORSFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        Map<String, String[]> qp = new HashMap<String, String[]>();
        for(String key : request.getParameterMap().keySet()){
            qp.put(key, new String[]{request.getParameterMap().get(key)[0]});
        }
        ......
        qp.put("authenticated", new String[]{""});
        chain.doFilter(new PrettyFacesWrappedRequest(request, qp), resp);
    }

    @Override
    public void destroy() {}
}

IN Application.java

@Bean
public SimpleCORSFilter simpleCORSFilter(){
    return new SimpleCORSFilter();
}

Upvotes: 0

Related Questions