What is Spring Interceptor?

Spring Interceptor Interceptor performs similar role to Servlet Filter. Servlet filter provides functionality to intercept and process requests before and after they reach the servlet container. Spring Interceptor provides functionality to intercept and process requests before and after they reach the controller in Spring MVC. Interceptor is implemented using HandlerInterceptor interface. HandlerInterceptor interface provides three methods: preHandle: Method executed before the request reaches the controller postHandle: Method executed after the request has reached the controller, before the view is rendered afterCompletion: Method executed after the view has been rendered Interceptor Implementation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CustomInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // Code to execute before the request reaches the controller // If false is returned, request is not forwarded to the controller return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // Code to execute after the controller is executed normally // Not executed if an exception is thrown } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // Code to execute after the view has been sent to the client // Exception object can be used to check exception information if an exception occurred // Exception information can be checked and logged } } Interceptor Registration To register the interceptor, implement the WebMvcConfigurer interface and override the addInterceptors method. ...

June 4, 2024 · 3 min · 567 words · In-Jun Hwang

What Is a Servlet Filter?

Servlets are Java classes which handle a client’s request. Servlets are used to process HTTP requests and to generate HTTP responses. What is a Servlet Filter? A Servlet filter provides a way to modify the request or response before it is handled by the servlet, or after the servlet has generated it. Why would you want to modify the request or response? There are many reasons to modify the request or response, such as: ...

June 4, 2024 · 5 min · 883 words · In-Jun Hwang

Getting to Know Authentication Methods (Cookies, Sessions, JWTs)

Authentication/Authorization Authentication: Verifying who a user is. Authorization: Granting a user specific permissions. HTTP Features Statelessness: A feature where the client and server sever their connection after making a request and receiving a response. Request/Response: The server forgets the client’s information after the request and response cycle is complete. These features necessitate additional configurations for implementing services requiring authentication. Cookie A small piece of data that the server sends to the client. ...

June 2, 2024 · 7 min · 1304 words · In-Jun Hwang

Baekjoon 27440: Make it One 3 (C++)

This is a solution to Baekjoon 27440: Make it One 3 problem in C++. Problem Statement There are three operations that can be applied to an integer X: If X is divisible by 3, divide it by 3. If X is even, divide it by 2. Subtract 1 from X. Given an integer N, you want to make it 1 using the three operations above. Find the minimum number of operations required. ...

May 29, 2024 · 3 min · 442 words · In-Jun Hwang

Adjusting Git Commit Times

Introduction There may be situations where you need to adjust the time of a Git commit. For example: Organizing commit history across different time zones Maintaining a chronological commit history of a project Adjusting timestamps of restored code from a backup However, adjusting commit times should be done with caution, especially in collaborative projects. Methods to Adjust Commit Times 1. Specify Time When Creating a New Commit When creating a new commit, you can specify a specific time: ...

May 25, 2024 · 2 min · 370 words · In-Jun Hwang