2
2
.
.
1
1
.
.
8
8
A
A
p
p
p
p
l
l
y
y
F
F
i
i
l
l
t
t
e
e
r
r
o
o
n
n
l
l
y
y
f
f
o
o
r
r
s
s
p
p
e
e
c
c
i
i
f
f
i
i
c
c
U
U
R
R
L
L
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to create Filter that will be applied only to specific Endpoint/URL.
This is defined while registering Filter.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_filter_create (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
Create Package: filters (inside main package)
– Create Class: MyFilter.java (inside controllers package)
MyController.java
package com.ivoronline.springboot_filter_urlspecific.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/NotFiltered")
public String notFiltered() {
System.out.println("CONTROLLER: Hello from NOT Filtered Endpoint");
return "Hello from NOT Filtered Endpoint";
}
@ResponseBody
@RequestMapping("/Filtered")
public String filtered() {
System.out.println("CONTROLLER: Hello from Filtered Endpoint");
return "Hello from Filtered Endpoint";
}
}
MyController
"/Filtered"
http://localhost:8080/Filtered
Tomcat
MyFilter
"/Filtered"
MyController
"/NotFiltered"
http://localhost:8080/NotFiltered
MyFilter.java
package com.ivoronline.springboot_filter_urlspecific.filters;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Component
public class MyFilter extends OncePerRequestFilter {
//===================================================================
// DO FILTER
//===================================================================
@Override
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("FILTER : Hello from Filter");
chain.doFilter(request, response);
}
//===================================================================
// FILTER REGISTRATION BEAN
//===================================================================
@Bean
public FilterRegistrationBean<MyFilter> loggingFilter(){
//CREATE REGISTRATION BEAN
FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/Filtered");
//RETURN REGISTRATION BEAN
return registrationBean;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Console
MyFilter : Code for HTTP Request
Controller: Code from Controller
MyFilter : Code for HTTP Response
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>