1、HttpHeaders.readOnlyHttpHeaders() 是 Spring 中 org.springframework.http.HttpHeaders 的一个静态方法,作用是创建一个只读的 HTTP 头集合
2、用法
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth("token123");
// 传给下游时,不允许对方修改
HttpHeaders readOnly = HttpHeaders.readOnlyHttpHeaders(headers);
如果下游想尝试修改,readOnly.add("X-Custom", "value"); 则会抛出异常(UnsupportedOperationException)
小结:
【1】HttpHeaders.readOnlyHttpHeaders() 的作用是将传入的 HttpHeaders 包装成一个只读对象
【2】不支持下游修改操作,如果尝试写则会抛异常:UnsupportedOperationException