spring-security: Unexpected behavior when adding a custom authentication filter in place of UsernamePasswordAuthenticationFilter.
Describe the bug
I extended the UsernamePasswordAuthenticationFilter with a filter that parses a JSON payload that contains the username and password.
And added the filter at UsernamePasswordAuthenticationFilter using a custom dsl
It is added in the log below
2023-02-07T09:41:16.941+03:00 INFO 6192 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@4f0cab0a, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@fe7b6b0, org.springframework.security.web.context.SecurityContextHolderFilter@4c86da0c, org.springframework.security.web.header.HeaderWriterFilter@7d0d91a1, org.springframework.security.web.authentication.logout.LogoutFilter@655a01d8, com.example.jsonpayloadauth.JsonUsernamePasswordAuthenticationFilter@7ab4ae59, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6732726, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4190bc8a, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@77681ce4, org.springframework.security.web.access.ExceptionTranslationFilter@1dbd580, org.springframework.security.web.access.intercept.AuthorizationFilter@196ae579]
All the configuration is working so far and when I send a request to /login I get 404 which means the user was authenticated since I don’t have any endpoint at /. The issue comes after the authentication when requesting any protected resource. For example, sending a request to /hello-auth respond with
{
"timestamp": "2023-02-07T06:42:36.466+00:00",
"status": 403,
"error": "Forbidden",
"path": "/hello-auth"
}
When debugging my custom filter I found out that the user was authenticated. But it seems the user authentication is reset to anonymousUser after finishing the request to /login.
To Reproduce
Add a custom authentication filter at UsernamePasswordAuthenticationFilter
Expected behavior Implementing a custom authentication filter should work.
Sample
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (5 by maintainers)
Hi @yousefbinali, I have a few questions based on the provided sample:
AuthenticationProviderorUserDetailsService. When you callthis.getAuthenticationManager().authenticate(authRequest);there is only theAnonymousAuthenticationProviderin place.SecurityContextRepositorythat’s being used is theRequestAttributeSecurityContextRepository(see here). The defaults have been changed in Spring Security 6 as explained here, but since you are not using the DSL, you have to change the repository yourself, like so:Please try the above suggestions and see if it works.
Yes, we don’t upgrade Spring Security on production becasuse it’s unstable and buggy. Spring made a decision to boot a security and now everybody have to use old stable versions. Meanwhile manuals on https://spring.io covers only very basic not real life cases. It looks like they benefit hackers job making it easy.