How to disable the default spring security filter chain and define a custom filter chain. (PART-5)

Tharushka Heshan
3 min readNov 10, 2024

--

Here you will learn

  • How to create a custom filter chain.
  • Inject filters into the custom filter chain.

So now you are going to bypass the default spring security filter chain. To do that you should disable it. Then define a new filter chain to the Spring Application iOC container.

Define a class to include configurations of the new filter chain. `config/SecurityConfig.java`

Then make this class a configuration class and bypass the default filter chain using these annotations.

@Configuration @EnableWebSecurity

Then define the filter chain as a bean to inject it into the IoC container.

SecurityFilterChain is an interface to create an object from which we use an implemented class called HttpSecurity.

Define a custom filter chain

Now the default chain is gone and we haven't defined any custom filters.
But by default in the filter chain, CSRF is enabled.
We aren’t using the CSRF token anymore, so let’s disable it.

Disable CSRF filteration

Now we can call any endpoint even POST methods because CSRF is disabled, Without user credentials.

So now not processing any filtration.
Let’s define our First Filter.

1- Enable user credential authentication filter for all requests.

Enable authentication filter for all requests

Now, the authentication filter is activated, but you can’t call any endpoint because it asks for credentials. However, we have not defined how this should happen.

To enable the login form or enable request header login.

Define how login should be happen

Change the session creation policy if you want.

change session creation policy

With a builder pattern, we can optimize the code.

optimized version of the code

Ok, now you have created your filter chain and added some basic filters.

But still, we didn’t archive our final destination, which is managing different users and roles. So let’s learn about it from the next upcoming posts.

Part 6: How to work with different users in spring security using in memory.

Part 4: What is the filter chain of Spring Security?

Part 3: What is CSRF Token in Spring Security?

Part 2: Change spring security default user name and password.

Part 1: Getting started with spring security.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Tharushka Heshan
Tharushka Heshan

Written by Tharushka Heshan

You must create your dreams, but the determination to achieve them must also come from within you

No responses yet

Write a response