spring-projects / spring-projects/spring-security

add filter before or after multiple existing filters

Open
#17,664 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: feedback-provided type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Expected Behavior

I have a custom filter (doing things like user validation) expected to be put after both BearerTokenAuthenticationFilter (Used for OAuth2 ResourceServer) and OAuth2LoginAuthenticationFilter (Used for OAuth2 client).

I'm expecting to have a new addFilterAfter to accept multiple reference filters instead of one. For example:

public HttpSecurity addFilterAfter(Filter filter, Collection<Class? extends Filter>> afterFilters) {}

The expected behavior is to have my custom filter added after the last filter in afterFilters.

Current Behavior

Currently, HttpSecurity has addFilterAfter and addFilterBefore method, both accepts only one existing filter as reference position.

Also, internal field filterOrders is private which means I cannot call its getOrder(filter) to detect order of existing filters.

Context

Since now I cannot put my custom filter to multiple existing filters. Then, I have to relying on options:

  • find out which existing filter is late in the list by running my application and logging their ordering, and then call addFitlerAfter with that filter.

This put my code depends on sequence of filters which is internal to framework. I searched for example, there are no documentation said that BearerTokenAuthenticationFilter will always be after OAuth2LoginAuthenticationFilter, then relying on the ordering of these two filters is a hack and may break in future release.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading HttpSecurity.addFilterAfter and addFilterBefore, then inspect the private filterOrders field and its getOrder behavior. Compare how the named BearerTokenAuthenticationFilter and OAuth2LoginAuthenticationFilter are ordered. Done means the proposed multiple-reference-filter behavior is designed consistently with existing filter ordering APIs and covered by appropriate tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend-api-design, security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.