title: How to create a Spring Interceptor for Spring RESTful web services
body: <p>I have some Spring RESTful (RestControllers) web services with no web.xml and I am using Spring boot to start the services. </p>
  
  <p>I want to add authorization layer for the web services and wanted to route all the http requests to one front controller before actually calling the web service itself. (I have a code to simulate sessions behavior at the autherisation layer, to validate a user based on a generated key that I send with each of the httpRequest from the client). </p>
  
  <p>Is there any Standard Spring solution on routing all the requests to a filter /front controller? </p>
  
  <p>Thanks in advance,
  Praneeth</p>
  
  <p>Edit:
  Adding my code </p>
  
  <p>Controller:
  `</p>
  
  <pre><code>@RestController
  public class UserService {
      UserDAO userDAO = new UserDAO();
  
      @RequestMapping(value="/login", method = RequestMethod.POST)
      @LoginRequired
      public String login(@RequestParam(value="user_name") String userName, @RequestParam(value="password") String password, HttpServletRequest request){
          return userDAO.login(userName, password);
      }
  }`
  </code></pre>
  
  <p>Interceptor:</p>
  
  <p>`</p>
  
  <pre><code>public class AuthenticationInterceptor implements HandlerInterceptor  {
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
          throws Exception {
          System.out.println("In Interceptor");
          //return super.preHandle(request, response, handler);
          return true;
      }
      @Override
      public void postHandle( HttpServletRequest request, HttpServletResponse response,
              Object handler, ModelAndView modelAndView) throws Exception {
          System.out.println("---method executed---");
      }
      @Override
      public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
              Object handler, Exception ex) throws Exception {
          System.out.println("---Request Completed---");
      }
  }
  </code></pre>
  
  <p>`</p>
  
  <p>Interface.
  `</p>
  
  <pre><code>@Target({ElementType.METHOD, ElementType.TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  public @interface LoginRequired {
  }
  </code></pre>
  
  <p>`</p>