title: Best way for simple token authentication in Spring Boot before execution of rest api
body: <p>Our system (which is quite old) has a token based authentication; a token is returned after a login request (username+pasword) and users pass that token as a parameter for any requests.</p>
  <p>I am now creating a very simple rest api for something, started using Spring Boot (I am really a newbie for that).</p>
  <p>Now, instead of our classic authentication and authorization process (every web service method has a authentication and authorization part, I mean; token is checked, if an user is found, user role and some service permissions are checked etc. and this code has been copied and pasted to every web service, every api method for years), I am trying to make that authentication/authorization code to run before any execution.</p>
  <p>I have implemented that solution - <a href="https://stackoverflow.com/a/35458670/4728677">https://stackoverflow.com/a/35458670/4728677</a> added that HandlerInterceptor and @ControllerAdvice code to my application, it runs before execution and checks token just as I wanted.</p>
  <p>But is this solution is the right solution? This code works nice but is there a better way; better Spring Boot classes, annotations etc. My project structure is over simple at the moment; Application class, A few repository classes and model classes for db entities, that's it. I just copied/pasted that code in the solution to my Application class where the RestController is also there, should I create some other classes?</p>
  <p>I am a newbie and since I am also checking for other solutions, they re either too complicated for my simple application (And also too complicated for a newbie like me, I would try them as well if I was sure that would not be overkill for a simple application) or needs some other libraries like Spring Security, MVC etc..</p>
  <p>Or is this solution just more than enough, no need to change anything? Well, it checks the token before execution, is simple the best? (Maybe there is a newer and simplier way??)</p>
  <p>Please give me some advices, code examples or sources. Thank you all.</p>
  <p>Edit: My simple new application has to work with our old monolith system's authentication api. Here is the scenario:</p>
  <p>User sends username, password to old system's authentication api
  (success) old system returns a token (not jwt, just some random alphanumeric)</p>
  <p>user makes some requests to old system's some other api with this token
  (success) our old system tries to find this token in our monolithic database's authentication table then responses</p>
  <p>(now my code's turn) user makes a request to my web service
  my code has to check this ugly token in that monolithic database's authentication table, so it connects there like it's ancestoral legacy web services.
  my code uses @ControllerAdvice and HandlerInterceptor to automatically authenticate/authorize that ugly token before any code execution. (the old legacy code had that authentication logic copied/pasted in start of every web service method, this solution is still good)
  my code is still curious about if there is a better solution in this scenario than using HandlerInterceptor.</p>