Create Custom Filter
Spring Security uses the concept of servelet filter to get a lot of work done,
It depends upon the configuration which we are using
Form Login Filter: If we are enabling form based authentication, then spring security will enable a form Login Filter
Basic Authentication Filter: if we are enabling basic authentication filter than it will enable basic authentication filter for csrf
Note: there are several servelet filters like these
we can even write our own servlet filters and configure them in the spring servlet chain of filter
Step 1: Add New class file with implements Filter interface of package jakarta servlet (MySecurityFilter.java)
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
chain.doFilter(request, response);
public class MySecurityFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("Before");
chain.doFilter(request, response);
System.out.println("After");
}
}
Now go to the MySecurityConfig class
To configure the filter https we can use http.addFilter that will add automatically
filter somewhere in the chain we can also have http method that
add the http.addFilterAfter() or http.addFilterBefore() as well
we will add given below filter http.addFilterBefore and provide our custom MySecurityFilter
and we have pass second parameter as BasicAuthenticationFilter this will tell this
filter need to add similarly we can use Form based authenticaion filter or CSRF filter
and so on can we use
@Configuration
public class MySecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.httpBasic();
http.authorizeHttpRequests().requestMatchers("/hello").authenticated();
http.addFilterBefore(new MySecurityFilter(), BasicAuthenticationFilter.class);
return http.build();
}
So now once you hit the api using postman you will get the response with correct user
name and passwod
http://localhost:8080/hello
but in logs you will see Before and After as printed
so it is simple as that to add your own filter to the chanin of spring security filter
GenericFilterBean
Simple base implementation of jakarta.servlet.Filter which treats its config
parameters (init-param entries within the filter tag in web.xml) as bean
properties.
so if we use this, if we want to pass it initialization parameters to this filter
rhrough our web.xml so we can do that
And by simply having getter and setter methods or setter methods on our filter, all
those initialization parameter will be automatically set for you, so you can have a
fields here , for example, private username, etc and thatn automatically this will be
injected at runtime for you from the web.xml that is the benefit of using
GenericFilterBean abstract class, otherwise everying else will be the same as our Filter
interface
so the Unit parameter in web.xml will be automatically injected through setter method
that you create
2. OncePerRequestFilter
OncePerRequestFilter
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
Comments
Post a Comment