Skip to main content

Posts

Showing posts with the label Filter

SpringBoot : How to create a Filter in Spring Boot Application

Points To Remember Implement the class Filter . Add @Configuration  annotation to the class to register it as a filter bean. Call method  filterChain.doFilter(resquest,response)  to continue the request flow Call method sendError to send error, ((HttpServletResponse)response).sendError(HttpServletResponse.SC_BAD_REQUEST); Call method sendRedirect to redirect request to error handler ((HttpServletResponse)response).sendRedirect("/errorUrl"); How to create a Filter in Spring Boot Application IN order to make a filter, we have create a class SecurityFilter view plain copy to clipboard print ? package  com.ekiras.filter;      import  org.springframework.core.Ordered;   import  org.springframework.core.annotation.Order;   import  org.springframework.stereotype.Component;      import  javax.servlet.*;   import  javax.servlet.http.HttpServletResponse;   import  ja...

SpringBoot : Read JSON object from POST Request in Filter

Points To Remember Add the following dependency to the gradle project. compile('com.fasterxml.jackson.core:jackson-databind:2.7.1-1') Read JSON in Filter and bind it to POJO class You can read the JSON inside a Filter class in SpringBoot by  Create a BufferedReader object by reading request object. Create an ObjectMapper object. Read JSON from bufferedReader object and bind it to any POJO class. view plain copy to clipboard print ? package  com.ekiras.test;      import  com.fasterxml.jackson.databind.JsonMappingException;   import  com.fasterxml.jackson.databind.ObjectMapper;      import  javax.servlet.*;   import  java.io.BufferedReader;   import  java.io.IOException;      /**    * @author ekansh    * @since 26/3/16    */    public   class  MyFilter  implements  Filter { ...

Spring Boot : How to register a Filter in the Application

Steps : How to register a Filter in the Application Make a class. Make it a spring bean by using  @Component annotation. Implement Filter interface to register it as a Filter in the Filter chain. Use  @Order  annotation to define the order of the filter in the filter chain. Following is the example of how you can implement the Filter using the above mentioned steps

Spring : How to define Filter order in Filter Chain in Spring Boot

Points To Remember From Spring boot version you can define the order of your filters by defining order for your filters using @Order annotation. You can use @Order annotation as follows to make a filter to be first in the filter chain. @Order(1) How to define Filter order in Filter Chain in Spring Boot Following is the example where we have registered two filters Security Filter - To block unauthorized requests Tracking Filter - To log each request coming to the server ###### security filter ###### tracking filter