1

I'm using Spring boot version 2.0.5.RELEASE in my application. I have got more than 500 Restful APIs in the application. To these APIs, a new request header needs to be added. Is there a way to add header in one place and can be used by all the 500 APIs?

Zeeshan
  • 177
  • 1
  • 15
  • 2
    How about using a servletfilter? – Scary Wombat Sep 11 '19 at 05:12
  • If you mean you expect a new header from client for all 500 resources, you can use [HandlerInterceptor](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/handler/HandlerInterceptorAdapter.html). See example here https://stackoverflow.com/a/42325065/432903 - If you need to access the header value inside all 500 resources you have to modify each one. – prayagupa Sep 11 '19 at 05:23
  • 1
    @prayagupd , thanks alot. It helped. – Zeeshan Sep 13 '19 at 05:28

3 Answers3

0

Yes you can write an interceptor for every request at root level and append your headers to that request.You can use prehandle as

  • This is used to perform operations before sending the request to the controller.

Below is the code snippet

    @Component
public class ProductServiceInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(
      HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

      return true;
   }
   @Override
   public void postHandle(
      HttpServletRequest request, HttpServletResponse response, Object handler, 
      ModelAndView modelAndView) throws Exception {}

   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
      Object handler, Exception exception) throws Exception {}
}
Vikram Saini
  • 2,713
  • 1
  • 16
  • 33
0

For future travellers: I solved this by creating custom filter. When some other filter stops the filter chain, for some reason (like authorization check etc.) the request might not reach the HandlerInterceptor. Filter solves this.

Create the component:

@Component
class VersionFilter() : OncePerRequestFilter() {
  override fun doFilterInternal(request: HttpServletRequest, response: HttpServletResponse, filterChain: FilterChain) {
    response.addHeader("X-Tolgee-Version", VersionProvider.version)
    filterChain.doFilter(request, response)
  }
}

And register it in WebSecurityConfig:

@Configuration
class WebSecurityConfig @Autowired constructor(
  ...
  private val versionFilter: VersionFilter,
) : WebSecurityConfigurerAdapter() {
  override fun configure(http: HttpSecurity) {
    http
      .csrf().disable().cors().and().headers().frameOptions().sameOrigin().and()
      .addFilterBefore(versionFilter, UsernamePasswordAuthenticationFilter::class.java)
      ...
      .and()
    return
  }

}

Jan Cizmar
  • 372
  • 2
  • 11
-2
@RestController
@RequestMapping("/headerRequestPath)

Add this at the begining of your code file. That way all path's will be appended with header 'headerRequestPath'

Rahul SK
  • 350
  • 3
  • 23