I am using Spring Security OAuth2 support to expose multiple API's as Resource Servers from a single web application. We would like to have each API require different "audience" so different programs can access different API's. I was setting up separate SecurityFilterChains for each API (same issuerURI but different audience). With this configuration, each security filter chain has a different Bearer Token Filter. The problem I am seeing is that any OAuth2 incoming request is picked up by one of the security filter chains (based on order) and since the correct corresponding Authentication type, it tries to process. However, if the audience doesn't not match the value for that API, it throws an error and the other API's aren't attempted. It appears that the Bearer Token Filter is before the request matchers so it is processing messages that match another API URL.
After some research, it appears that an alternative implementation of AuthenticationManagerResolver is needed (instead of JwtIssuerAuthenticationManagerResolver) that looks for audience match also. Alternatively, I guess we could look at placing a Request match before the Bearer Token Filter.
Any thoughts or suggestions?
I think that your initial idea with one
SecurityFilterChainbean par "API" is the right one. Maybe you're just lacking asecurityMatcherfor each (but the last one in@Orderso that it acts as default).It is very likely that you could define the security matchers based on just a path-prefix.
Maybe, if path is not enough and if all access tokens are JWTs, you could try to match requests on the access token
audclaim too, but the 1st approach is certainly easier.Here is a sample with the two different approches:
Note that only the filter-chain with the highest order (lowest precedence) hasn't a security matcher.
Also note that
someOtherConditionToMatchTheRequestAsQueryingThePartOfYourApiExposedToAudycan be anything from the request: its path as in the 1st filter-chain, the HTTP verb (likeaudycan do onlyOPTIONSandGEToperations), some headers matching a pattern, etc.