BAEL-4564 Sample code for article filters vs spring handler interceptor

This commit is contained in:
wlesniak 2021-05-06 16:06:09 +01:00
parent ebe38e710d
commit 9089307991
7 changed files with 126 additions and 0 deletions

View File

@ -9,4 +9,5 @@ This module contains articles about Spring Web MVC in Spring Boot projects.
- [Spring MVC Async vs Spring WebFlux](https://www.baeldung.com/spring-mvc-async-vs-webflux)
- [Differences in @Valid and @Validated Annotations in Spring](https://www.baeldung.com/spring-valid-vs-validated)
- [CharacterEncodingFilter In SpringBoot](https://www.baeldung.com/spring-boot-characterencodingfilter)
[Difference Between Interceptors and Filters in Spring MVC](https://www.baeldung.com/difference-between-interceptors-and-filters-in-spring-mvc/)
- More articles: [[prev -->]](/spring-boot-modules/spring-boot-mvc-2)

View File

@ -0,0 +1,12 @@
package com.baeldung.filtersinterceptors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication(scanBasePackages = "com.baeldung.filtersinterceptors")
public class FilterInterceptorApp {
public static void main(String[] args) {
SpringApplication.run(FilterInterceptorApp.class, args);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.filtersinterceptors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloConroller {
private Logger logger = LoggerFactory.getLogger(HelloConroller.class);
@GetMapping("/hello")
public String hello() {
logger.info("Hello from the controller");
return "hello";
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.filtersinterceptors;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class LogFilter implements Filter {
private Logger logger = LoggerFactory.getLogger(LogFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
logger.info("Hello from: "+request.getLocalAddr());
chain.doFilter(request, response);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.filtersinterceptors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Component
public class LogInterceptor implements HandlerInterceptor{
private Logger logger = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.info("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
logger.info("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("afterCompletion");
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.filtersinterceptors;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final LogInterceptor logInterceptor;
public WebMvcConfig(LogInterceptor logInterceptor) {
this.logInterceptor = new LogInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor).addPathPatterns("/**");
}
}

View File

@ -0,0 +1,10 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Filters vs Interceptors</title>
</head>
<body>
<h3>Hello</h3>
</body>
</html>