How to Send an HTTP Header With Every Request With Spring RestTemplate

简介: In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforces the existence of a special HTTP request header.

 

In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforces the existence of a special HTTP request header.

From a client perspective, it would be nice to send this header automatically, instead of having to set the header manually with every request. This post shows how we can use Spring’s RestTemplate and ClientHttpRequestInterceptor to accomplish this.

First we need to implement a ClientHttpRequestInterceptor:

package com.myapp.connector;

import java.io.IOException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

public class XUserAgentInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(
            HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {

        HttpHeaders headers = request.getHeaders();
        headers.add("X-User-Agent", "My App v2.1");
        return execution.execute(request, body);
    }
}

Now we need to configure our RestTemplate to use it. Here I’m using Spring’s Java-based configuration, but you can implement the same thing with the XML-based configuration too:

package com.myapp;

import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
import com.myapp.connector.XUserAgentInterceptor;

@Configuration
public class MyAppConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
        restTemplate.setInterceptors(Collections.singletonList(new XUserAgentInterceptor()));
        return restTemplate;
    }
}

With this configuration, any requests you make through the RestTemplate will automatically carry the desired HTTP request header.

To learn how to set request timeouts automatically, see my post How to Set HTTP Request Timeouts With Spring RestTemplate.

http://springinpractice.com/2013/10/27/how-to-send-an-http-header-with-every-request-with-spring-resttemplate/

 

When creating a web service, it’s often useful to know which apps are hitting it. I don’t mean which users, but instead which apps. The reason is that you may want to coordinate with some other team on something. For example, maybe the team is being too aggressive about request retries, or maybe you want to alert the team to a change in the API. Whatever the reason, it’s good to know which apps are calling your service.

HTTP has a User-Agent header that can help here. One possible approach is to make that a required header. Unfortunately, this approach isn’t ideal. The problem is that HTTP client libraries usually set that header automatically. So if the client application forgets to set the header explicitly, you end up with user agents like Apache-HttpClient/release (java 1.5), which isn’t much help at all.

An approach I like better is to define a custom header and make it required. I use X-User-Agent, since it really is a user agent we’re talking about here.

Here’s how to implement this with a servlet filter. No Spring involved here at all; it’s just servlet stuff.

package com.myapp.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class XUserAgentFilter implements Filter {
    private static final String X_USER_AGENT = "X-User-Agent";

    private String errorJson;

    public XUserAgentFilter() {
        String message = 
            "HTTP header '" + X_USER_AGENT + "' is required. Please set it to your application name so we know " +
            "who to contact if there's an issue.";
        this.errorJson = "{ " + wrap("message") + " : " + wrap(message) + " }";
    }

    private String wrap(String s) { return "\"" + s + "\""; }

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

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

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        if (httpRequest.getHeader(X_USER_AGENT) == null) {//不走chain.doFilter即结束
            httpResponse.setStatus(422);
            httpResponse.getWriter().println(errorJson);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() { }
}

Of course, you need to configure this filter and a filter mapping in your web.xml file.

In the next post I’ll show you how to set up your Spring RestTemplate to send the X-User-Agent header with each request automatically.

http://springinpractice.com/2013/10/25/know-which-apps-are-hitting-your-web-service/

In How to Send an HTTP Header With Every Request With Spring RestTemplate, we looked at how to send a given HTTP header automatically with every request. Here we’ll do something similar, which is to automatically apply a request timeout to every request.

Here’s the Java-based configuration for this technique:

package com.myapp;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class MyAppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(clientHttpRequestFactory());
    }

    private ClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setReadTimeout(2000);
        factory.setConnectTimeout(2000);
        return factory;
    }
}

The XML-based configuration is this:

<bean class="org.springframework.web.client.RestTemplate">
    <constructor-arg>
        <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"
            p:readTimeout="2000"
            p:connectTimeout="2000" />
    </constructor-arg>
</bean>

http://springinpractice.com/2013/10/27/how-to-set-http-request-timeouts-with-spring-resttemplate/

Basic Auth:

// Set the username and password for creating a Basic Auth request
HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

try {
    // Make the HTTP GET request to the Basic Auth protected URL
    ResponseEntity<Message> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
    return response.getBody();
} catch (HttpClientErrorException e) {
    Log.e(TAG, e.getLocalizedMessage(), e);
    // Handle 401 Unauthorized response
}

http://docs.spring.io/autorepo/docs/spring-android/1.0.x/reference/html/rest-template.html

 

相关文章
|
11月前
|
存储 C++
UE5 C++:自定义Http节点获取Header数据
综上,通过为UE5创建一个自定义HTTP请求类并覆盖GetResult方法,就能成功地从HTTP响应的Header数据中提取信息。在项目中使用自定义类,不仅可以方便地访问响应头数据,也可随时使用这些信息。希望这种方法可以为你的开发过程带来便利和效益。
438 35
|
11月前
|
缓存 安全 Java
深入解析HTTP请求方法:Spring Boot实战与最佳实践
这篇博客结合了HTTP规范、Spring Boot实现和实际工程经验,通过代码示例、对比表格和架构图等方式,系统性地讲解了不同HTTP方法的应用场景和最佳实践。
1011 5
|
11月前
|
Java 测试技术 API
在 Spring 中 Mock RestTemplate
本文介绍了两种在单元测试中 mock RestTemplate 调用的方法,避免真实 HTTP API 调用以提高测试可控性。一是使用 Mockito 模拟库,通过 @Mock 和 when/then 方法定义模拟行为;二是借助 Spring Test 提供的 MockRestServiceServer,创建模拟服务器定义请求响应交互。文中结合具体代码示例展示了两种方法的实现细节,并强调了 RestTemplate 实例的一致性配置。适用于需要模拟外部 HTTP 调用的集成测试场景。
293 1
|
缓存 JSON 安全
Http自定义Header导致的跨域问题
在Web开发中,正确处理跨域问题是确保应用安全和性能的重要环节。通过在服务器端设置适当的CORS头信息,处理预检请求,并遵循最佳实践,可以有效解决自定义Header导致的跨域问题,提高应用的安全性和用户体验。理解并掌握这些技巧,对于构建高效、可靠的Web应用至关重要。
981 11
|
存储 算法 数据安全/隐私保护
基于 HTTP Header 传输签名参数
基于 HTTP Header 传输签名参数
366 13
|
安全 应用服务中间件 网络安全
修复HTTPS升级后出现 Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insecure frame 'http://xxx'. This request has been blocked; the content must be served over HTTPS. 的问题
修复HTTPS升级后出现 Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insecure frame 'http://xxx'. This request has been blocked; the content must be served over HTTPS. 的问题
|
负载均衡 Java 开发者
Spring Cloud 远程调用:为何选择 HTTP 而非 RPC?
【10月更文挑战第1天】在微服务架构中,远程服务调用是一个核心环节。面对HTTP和RPC(Remote Procedure Call,远程过程调用)这两种通信协议,Spring Cloud 选择了HTTP作为其主要通信手段。本文将深入探讨Spring Cloud选择HTTP而非RPC的原因,以及这一选择在实际工作中的优势。
534 0
|
负载均衡 Java API
深度解析SpringCloud微服务跨域联动:RestTemplate如何驾驭HTTP请求,打造无缝远程通信桥梁
【8月更文挑战第3天】踏入Spring Cloud的微服务世界,服务间的通信至关重要。RestTemplate作为Spring框架的同步客户端工具,以其简便性成为HTTP通信的首选。本文将介绍如何在Spring Cloud环境中运用RestTemplate实现跨服务调用,从配置到实战代码,再到注意事项如错误处理、服务发现与负载均衡策略,帮助你构建高效稳定的微服务系统。
499 2
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
755 3