Always call function before @RequestMapping function

1.3k Views Asked by At

I'm creating a API spring service using spring framework.

I've several @RequestMapping function for each endpoint that i define. In every @RequestMapping function, i have a function to check several authorization variables before continue.

Can I put this authorization function to a specific function before I forward to the related @RequestMapping function?

5

There are 5 best solutions below

0
On BEST ANSWER

A HandlerInterceptor implementation as suggested elsewhere is what you want. From your comments in response to that answer however you misunderstand the API:

I have read method preHandle in documentation link that you gave.. but it return boolean.. can i return JSON output using this method?

As the API Docs note the boolean only signals (to the framework) whether processing should continue:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html#preHandle-javax.servlet.http.HttpServletRequest-javax.servlet.http.HttpServletResponse-java.lang.Object-

returns true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

So to return JSON:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
 Object handler) throws Exception{

    if(someCondition){
      return true; //continue processing
    }else{
      response.getOutputStream().write("{\"message\" : \"some text\"}");
      response.setContentType("text/json");
      response.setStatus(...); //some http error code?
      response.getoutputStream().flush();
      return false; //i have written some JSON to the response. Processing stops here
    }
}
0
On

There are many ways to achive it.

  1. By writing an old fashioned servlet filters. writing your own filter which validate the request and process the request.
  2. By writing spring filters.
  3. By using spring AOP. (recommended)
  4. By writing your own anonatation and which will validae the request and just give on top of method on which you want to validate. (recommended)

There might be other approach as well.

2
On

You can use HandlerInterceptor. Writing your own interceptor or extend and use existing implementations

0
On

A couple of suggestions...

  1. Use AOP advice for RequestMapping, related example covered in another post How do you get RequestMapping request in AOP advice from a Spring controller?

  2. Create a filter for controlles and do required params checking over there in filter chain

0
On

See this tutorial - Spring Boot - Interceptor. This will help you.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class ServiceInterceptor implements HandlerInterceptor {
   @Override
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
      System.out.println("Pre Handle method is Calling");
      return true;
   }
   @Override
   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
      System.out.println("Post Handle method is Calling");
   }
   @Override
   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
      System.out.println("Request and Response is completed");
   }
}