Spring Boot 处理 HTTP Headers

本文介绍 Spring Boot 2 读写 HTTP Headers 的方法。


目录

  • 读取 HTTP Headers
    • 读取指定名称的 HTTP Header
    • 读取全部 HTTP Header
  • 写入 HTTP Headers
    • 使用 HttpServletResponse 对象
    • 使用 ResponseEntity 返回值
    • 自定义过滤器为所有响应统一添加

读取 HTTP Headers

使用注解 @RequestHeader 获取特定名称 HTTP Header 的值。

读取指定名称的 HTTP Header
@GetMapping("/read")
public String read(@RequestHeader(name = HEADER_AUTHORIZATION, required = false) String authorization) {
    return "Read HTTP Header[" + HEADER_AUTHORIZATION + "]: " + authorization;
}
读取全部 HTTP Header

读取全部 HTTP Header 可选参数类型包括:MapMultiValueMapHttpHeaders

@GetMapping("/read/all1")
public String readAll1(@RequestHeader Map headers) {
    StringBuilder content = new StringBuilder();
    headers.forEach((key, value) -> content.append(key).append("=").append(value).append(";"));
    return "Read HTTP Headers: " + content.toString();
}

@GetMapping("/read/all2")
public String readAll2(@RequestHeader MultiValueMap headers) {
    StringBuilder content = new StringBuilder();
    headers.forEach((key, value) -> content.append(key).append("=").append(value).append(";"));
    return "Read HTTP Headers: " + content.toString();
}

@GetMapping("/read/all3")
public String readAll3(@RequestHeader HttpHeaders headers) {
    InetSocketAddress host = headers.getHost();
    System.out.println("URL: " + host.getHostName() + ":" + host.getPort());
    StringBuilder content = new StringBuilder();
    headers.forEach((key, value) -> content.append(key).append("=").append(value).append(";"));
    return "Read HTTP Headers: " + content.toString();
}

@RequestHeader 注解属性说明:

  • required 属性默认为 true,如果请求中不含指定的 HTTP Header,则方法会返回 400 Bad Request 错误。
  • defaultValue 用于设置 HTTP Header 的缺省值。

写入 HTTP Headers

使用 HttpServletResponse 对象
@GetMapping("/write/HttpServletResponse")
public String writeByHttpServletResponse() {
    response.addHeader(HEADER_AUTHORIZATION, "By HttpServletResponse");
    return "Write HTTP Header[" + HEADER_AUTHORIZATION + "]";
}
使用 ResponseEntity 返回值
@GetMapping("/write/ResponseEntity1")
public ResponseEntity writeByResponseEntity1() {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set(HEADER_AUTHORIZATION, "By ResponseEntity1");
    return ResponseEntity.ok()
            .headers(responseHeaders)
            .body("Write HTTP Header[" + HEADER_AUTHORIZATION + "]");
}

@GetMapping("/write/ResponseEntity2")
public ResponseEntity writeByResponseEntity2() {
    return ResponseEntity.ok()
            .header(HEADER_AUTHORIZATION, "By ResponseEntity2")
            .body("Write HTTP Header[" + HEADER_AUTHORIZATION + "]");
}
自定义过滤器为所有响应统一添加
package tutorial.spring.boot.mvc.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@WebFilter
public class HttpHeaderFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.addHeader("Valid", "1");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

你可能感兴趣的:(Spring Boot 处理 HTTP Headers)