简体   繁体   English

Spring Boot中的访问请求标头?

[英]Access Request Headers in Spring Boot?

I'm using this project and specifically these 2 classes: ApplicationStartup.java and Application.java . 我正在使用这个项目 ,尤其是这两个类: ApplicationStartup.javaApplication.java

I googled a bit and it looks like all other examples use a specific /endpoint in order to get request headers and I just have my app running on localhost:8080 instead. 我用谷歌搜索了一下,看起来所有其他示例都使用特定的/endpoint来获取请求标头,而我的应用只是在localhost:8080上运行。 But it looks like I do have /graphql endpoint so it should be possible to parse request's header. 但是看起来我确实有/graphql端点,因此应该可以解析请求的标头。

Here are the contents of my application.yml : 这是我的application.yml的内容:

server:
  port: 8080

spring:
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: false

graphql.playground:
  mapping: /playground
  endpoint: /graphql

Any Spring controller can get access to headers as part of the method arguments using @RequestHeader annotation (docs here ): 任何Spring控制器都可以使用@RequestHeader批注( 此处为文档)来访问标头作为方法参数的一部分:

@Controller
public class MyController {
    public ResponseEntity<String> schedule(@RequestBody MyDto request,
                                           @RequestHeader HttpHeaders headers)
{
   ...
}

If you don't have access to the controller code, you can implement a OncePerRequestFilter and access the headers from the underlying servlet request using @gnana's approach. 如果您无权访问控制器代码,则可以实现一次OncePerRequestFilter并使用@gnana的方法访问基础servlet请求中的标头。

If your question is to get the current request header then below code will do that for you. 如果您的问题是获取当前的请求标头,则下面的代码将为您完成此操作。

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()) .getRequest();

request.getHeader("Authorization");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 在请求完成之前启动访问多部分请求(文件上传)标头 - Spring boot access multipart request (file upload) headers before request completes Spring 引导分页与 http 请求标头 - Spring boot pagination with http request headers Angular Spring Boot:重定向错误:在飞行前响应中Access-Control-Allow-Headers不允许请求标头字段Content-Type - Angular Spring Boot: Redirection error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response 调整请求标头以被Spring Boot资源接受 - tune up request headers to be accepted by spring boot resource Spring Boot Controller应该如何处理“丢失”的请求标头? - How should 'missing' request headers be handled by a Spring Boot Controller? Spring Boot 允许 GET 请求标头中的任何内容类型 - Spring boot to allow any content type in headers for GET request 将相同的请求标头发送到响应标头会导致无限循环和不正确的 json 响应 Spring 启动 - Sending same request headers into the response headers cause infinity loop and incorrect json response Spring boot Spring 引导:无法访问 Spring 调度程序中的请求 scope bean - Spring Boot: Unable to access the request scope bean in Spring Scheduler 使用Spring Boot 2 WebClient以线程安全/每个请求的方式,如何在每个请求中发送diff头? - Using Spring Boot 2 WebClient, in threadsafe / per-request manner, how send diff headers every request? 如何在 Spring 引导中使 ThreadPoolTaskExecutor 访问请求范围 bean - How to make ThreadPoolTaskExecutor access request scoped bean in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM