How to define 3 authentication ways config - httpBasic, userDetailsService, JsonLoginFilter()
I'm creating an app where a user can authenticate using three ways httpBasic, userDetailsService, JsonLoginFilter().
A user should be authenticated using Spring's default
/login
path. But the type is one of three:
httpBasic:
There must be 2 users in memory database for testing. Simple user with user role and admin user with its role.
userDetailsService:
User and Role entities are read or saved to MySQL database. Authentication is possible when using UserDetailsService
implementation bean.
JsonLoginFilter():
A user sends a request body but not as x-www-form-urlencoded
or form-data
. The body is raw JSON consisting of a pair <key>: <value> entries. Username and password. And JsonLoginFilter()
is the filter that intercepts it.
This is my configuration:12 Replies
⌛
This post has been reserved for your question.
Hey @Tomasm21! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
It executes well but in console I get:
Problem:
and
As a result I can't authenticate in either way. I get an exception:
I want to have all three ways to authenticate. Did I defined it wrong in my
SecurityConfiguration
?
How should it be defined? Why Spring doesn't accept my changed AuthenticationManager
with two DaoAuthenticationProvider
providers that each provides different way to authenticate? One for in memory db and another using real MySql db.
JsonLoginFilter():
Try to create two AuthenticationProvider, one for each UserDetailsService and then register both of them with @Bean
I think that this way you can separate the two forms of login and still delegate the management of beans to Spring Security as it should be.
Or something like that
Name the beans and then use @Qualifier to specify which authenticationProvider you want to inject
But would it mean that if I will qualify the
databaseProvider
bean and then in postman I will try to autheticate using Basic Auth then what happens next? Due to qualifiers it will always try to athenticate me using databaseProvider
instead of the inMemoryProvider
. And perhaps I won't be able to auntheticate.
There should be a way.
These problems are not new.Yes, indeed. But you can get around this by creating two SecurityFilterChain and specifying which authenticationProvider to use.
Ok I will try
The problem with this approach that I mentioned is that it is not possible for the same route to be accessed by more than one form of authentication. For more flexibility, you also should create a filter that processes the user credentials, creates an Authentication object and then based on the obtained data, delegate the authentication to an AuthenticationProvider
But maybe this might not be a problem in your case
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.