Mejmo
Mejmo

Reputation: 2593

Spring boot and checking HTTP header presence

I send along every REST call my custom header, which is (for instance) an authorization token. This token remains the same, as I do not need high security in this case. Can I use some simple way how to check every request coming to RestController whether it has this token among headers?

Upvotes: 1

Views: 1512

Answers (1)

Sanjay
Sanjay

Reputation: 8965

I can see a few ways:

  1. Coding a @ModelAttribute in a @ControllerAdvice class, like this

    @ControllerAdvice
    public class HeaderFetcher {
    
        @ModelAttribute
        public void fetchHeader(@RequestHeader Optional<String> myHeader, Model model) {
          if header is present
            model.addAttribute("myHeader", the header);
          else
            throw exception
        }
    
    }
    

    Haven't tried this, though.

  2. Using a filter
  3. Using AoP

Upvotes: 1

Related Questions