title: How to add a custom header in Spring WebFilter?
body: <p>I'm trying to add a custom filter before I invoke my REST Service. In this below class, I'm trying to add the custom filter in the HttpRequest but I'm getting error :-</p>
  
  <p><strong>java.lang.UnsupportedOperationException: null
      at java.util.Collections$UnmodifiableMap.computeIfAbsent(Collections.java:1535) ~[na:1.8.0_171]
      at org.springframework.util.CollectionUtils$MultiValueMapAdapter.add(CollectionUtils.java:459) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]</strong></p>
  
  <pre><code>public class AuthenticationWebFilter implements WebFilter {
      private static final Logger LOGGER = LoggerFactory.getLogger(AuthenticationWebFilter.class);
  
      @Autowired
      private TokenServiceRequest tokenServiceRequest;
  
      @Autowired
      private AuthenticationProvider authenticationProvider;
  
      public AuthenticationWebFilter(TokenServiceRequest tokenServiceRequest, AuthenticationProvider authenticationProvider) {
          super();
          this.tokenServiceRequest = tokenServiceRequest;
          this.authenticationProvider = authenticationProvider;
      }
  
      @Override
      public Mono&lt;Void&gt; filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
          HttpHeaders requestHeaders = serverWebExchange.getRequest().getHeaders();
          HttpHeaders responseHeaders = serverWebExchange.getResponse().getHeaders();
          LOGGER.info("Response HEADERS: "+responseHeaders);
          LOGGER.info("Request HEADERS: "+serverWebExchange.getRequest().getHeaders());
  
          tokenServiceRequest.setUsername(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.USERNAME));
          tokenServiceRequest.setPassword(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.PASSWORD));
          tokenServiceRequest.setClientId(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.CLIENT_ID));
          tokenServiceRequest.setSecretClient(serverWebExchange.getRequest().getHeaders().getFirst(CommerceConnectorConstants.SECRET_CLIENT));
          LOGGER.info("Token Received: " + authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
  
          //responseHeaders.set(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
          //responseHeaders.add(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
  
          //This below code is not working
          serverWebExchange.getRequest().getQueryParams().add("test", "value");
  
          //This below code is not working
          //serverWebExchange.getRequest().getHeaders().add(CommerceConnectorConstants.X_AUTH_TOKEN, authenticationProvider.getUserAccessToken(tokenServiceRequest).getTokenId());
          LOGGER.info("Exiting filter@AuthenticationWebFilter");
          return webFilterChain.filter(serverWebExchange);
      }
      }
  </code></pre>
  
  <p>In HTTPResponse, I can set the custom headers but my requirement is to add the custom header in the HTTPRequest. Please advise.</p>