2

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?

3
  • 2
    How about using a servletfilter? Commented Sep 11, 2019 at 5:12
  • If you mean you expect a new header from client for all 500 resources, you can use HandlerInterceptor. See example here stackoverflow.com/a/42325065/432903 - If you need to access the header value inside all 500 resources you have to modify each one. Commented Sep 11, 2019 at 5:23
  • 1
    @prayagupd , thanks alot. It helped. Commented Sep 13, 2019 at 5:28

3 Answers 3

1

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 } } 
Sign up to request clarification or add additional context in comments.

Comments

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 {} } 

2 Comments

Maybe extending from HandlerInterceptorAdapter is better
@vikramsaini , thanks for the help. I'm using HandlerInterceptorAdapter in my code. It is working as expected.
-3
@RestController @RequestMapping("/headerRequestPath) 

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.