Skip to content

Document generic AuthenticationFilter class #9228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
merusso opened this issue Dec 1, 2020 · 4 comments
Open

Document generic AuthenticationFilter class #9228

merusso opened this issue Dec 1, 2020 · 4 comments
Labels
in: docs An issue in Documentation or samples type: enhancement A general enhancement

Comments

@merusso
Copy link

merusso commented Dec 1, 2020

A generic AuthenticationFilter class was added in #6506 to v5.2, but there still isn't any documentation to explain why or how to use it.

Would it be ideal for a web API that uses a form of username/password authentication using custom headers (Client-ID, Client-Secret)? This feels like a simple use case but one that still appears to require a custom filter.

@merusso merusso added status: waiting-for-triage An issue we've not yet triaged type: enhancement A general enhancement labels Dec 1, 2020
@rwinch rwinch added in: docs An issue in Documentation or samples and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 3, 2020
@rwinch rwinch removed their assignment Dec 3, 2020
@MasatoshiTada
Copy link

Is there any progress on this issue?👀
I'm wondering what this filter is. It seems any class doesn't call this filter.

@abccbaandy
Copy link

2025 now...still no update 😢

I have see @Kehrlann 's spring-security-the-good-parts use this class, but I don't find any document for it.
https://github.com/Kehrlann/spring-security-the-good-parts/blob/main/src/main/java/wf/garnier/spring/security/thegoodparts/RobotAuthenticationFilter.java

And no spring class is extends that class, the famous filter UsernamePasswordAuthenticationFilter is extends AbstractAuthenticationProcessingFilter.

Also these two filter looks almost same, is there any design reason?

@Kehrlann
Copy link
Contributor

Kehrlann commented Jan 23, 2025

Let me think about how to best document this. In the meantime, for those wondering about this class:

The AuthenticationFilter is a user helper class, a commodity abstraction. Similar to AuthorityUtils, in a way. Neither of those are mentioned in the javadoc, and they are seldom[1] used in Spring Security itself.

The AuthenticationFilter is a "backport" to the servlet stack of the AuthenticationWebFilter that was introduced with the reactive stack. It represents the "usual steps" in an authentication flow, and is built by composing building blocks:

  1. Check if the request should be processed (requestMatcher)
  2. Convert the request object into an Authentication (authenticationConverter)
  3. Authenticate (authenticationManager, or authenticationManagerResolver for complex use-cases)
  4. Handle authentication result:
    a. Authentication success (authenticationSuccessHandler)
    b. or authentication failure (authenticationFailureHandler)

And that's it! There's actually very little code in the filter, it's mostly stringing the building blocks together, handling errors, saving authentication to the security context, and changing the session id. The user provides 5 functions and they will be correctly wired, instead of writing the same imperative logic.

AbstractAuthenticationProcessingFilter, is different because, as the javadoc mentions, it is meant for browser-based http authentication, and supports, for example, simpler configuration for URL redirects on authentication failures, remember-me services and session management. It is not really designed for, say, machine-to-machine authentication. But mostly ... it's an older abstraction that most of spring security was built on top of (2004!)

Today, I'd very likely go with AuthenticationFilter, unless I want to use the support of AbstractAuthenticationFilterConfigurer.

Hope this is helpful.


[1] Since 6.4, there is one filter that uses AuthenticationFilter in the one-time-token login flow, but it is harder to discover because it is a direct instance instead of a subclass (source) . It might be the wrong abstraction in this case, maybe we should have used AbstractAuthenticationProcessingFilter instead.

@merusso
Copy link
Author

merusso commented Jan 24, 2025

Really helpful explanation, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: docs An issue in Documentation or samples type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants