spring-bootauthenticationspring-securityheaderrequest-headers

Adding custom request header to every API in spring boot


I am trying to add custom request header to every API I can do it by adding a piece of code to every API

`public ResponseDTO setNames(@RequestHeader(value="my-header",required = false HttpServletRequest httpServletRequest) throws Exception {}`

But I want this to be done automatically like we have authorisation header by default when we create a new API.How can this be done can anyone tell me a good solution

enter image description here


Solution

  • You can use the following if you are using springdoc-openapi

    @Configuration
    class CustomizeSpringDoc {
        @Bean
        fun publicApi(): GroupedOpenApi {
            return GroupedOpenApi.builder()
                .group("add-auth-header")
                .addOperationCustomizer { operation, handlerMethod ->
                    operation.parameters.add(
                        HeaderParameter()
                            .name("Authorization")
                            .description("Access Token")
                    )
                    operation
                }
                .build()
        }
    }
    
    

    If you are using spring-fox, you can refer back to the below link, globalRequestParameters: http://springfox.github.io/springfox/docs/current/#quick-start-guides