filter hierarchy
This commit is contained in:
parent
b576a4da40
commit
75593037fc
@ -0,0 +1,32 @@
|
|||||||
|
package org.baeldung.security.filter;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||||
|
public class CustomHttpFilter extends GenericCustomHttpFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig config) throws ServletException {
|
||||||
|
System.out.println(config.getInitParameter("first-init-param") +
|
||||||
|
config.getInitParameter("second-init-param"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain
|
||||||
|
chain) throws IOException, ServletException {
|
||||||
|
System.out.println("CustomHttpFilter is in action [the request is going to the server].");
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
System.out.println("CustomHttpFilter is in action [the request has come from the server].");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package org.baeldung.security.filter;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.embedded.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class FilterConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FilterRegistrationBean getCustomFilterRegistrationBean() {
|
||||||
|
FilterRegistrationBean registration = new FilterRegistrationBean();
|
||||||
|
registration.setFilter(new CustomHttpFilter());
|
||||||
|
|
||||||
|
registration.setInitParameters(new HashMap<String, String>() {{
|
||||||
|
put("first-init-param", "Hello from ");
|
||||||
|
put("second-init-param", "Filter!");
|
||||||
|
}});
|
||||||
|
registration.setUrlPatterns(singletonList("/user/registration"));
|
||||||
|
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package org.baeldung.security.filter;
|
||||||
|
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
public abstract class GenericCustomFilter implements Filter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package org.baeldung.security.filter;
|
||||||
|
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class GenericCustomHttpFilter extends GenericCustomFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response,
|
||||||
|
FilterChain chain) throws IOException, ServletException {
|
||||||
|
doFilter((HttpServletRequest) request, (HttpServletResponse) response, chain);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void doFilter(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
FilterChain chain) throws IOException, ServletException;
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user