Background information:
I have two different types of authentication happening in one single application in addition to some unsecured endpoints. Let's call these endpoints the following:
/non-oauth/**
/oauth2/**
/unsecured
Non oauth
This is a piece of legacy code that we still need to support for some old clients. These requests contains an Authorization
header with a (Bearer) JWT-token issued by a non OAuth2-server.
We have existing security code implemented as a Spring request filter.
OAuth2
We are using Spring Security for these endpoints. The Spring Security config is as follows:
@Bean
SecurityFilterChain filterChain(HttpSecurity httpSecurity, NTKeycloakAuthProperties ntKeycloakAuthProperties, HandlerExceptionResolver handlerExceptionResolver) throws Exception {
return httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> {
if (ntKeycloakAuthProperties.getPaths() == null) {
log.warn("[Keycloak Auth] No secure paths have been added to configuration");
} else {
ntKeycloakAuthProperties.getPaths().forEach(path ->
authorizationManagerRequestMatcherRegistry.requestMatchers(path).authenticated());
}
authorizationManagerRequestMatcherRegistry.requestMatchers("/**").permitAll();
})
.oauth2ResourceServer(httpSecurityOAuth2ResourceServerConfigurer -> {
httpSecurityOAuth2ResourceServerConfigurer.authenticationManagerResolver(
new JwtIssuerAuthenticationManagerResolver(ntKeycloakAuthProperties.getIssuers()));
httpSecurityOAuth2ResourceServerConfigurer.authenticationEntryPoint(
new BearerTokenProblemDetailsAuthenticationEntryPoint(handlerExceptionResolver));
})
.build();
}
Unsecured
Unsecured endpoints are as named: Unsecured. One can see in the SecurityFilterChain
bean config that we add the following matcher:
authorizationManagerRequestMatcherRegistry.requestMatchers("/**").permitAll();
Expectations
I would expect the code to work as follows:
- When requesting the
/unsecured
path without anAuthorization
-header, the request should be permitted. - When requesting the
/oauth2
path with anAuthorization
-header containing a token issued by our Keycloak server should be permitted - When requesting the
/non-oauth
path with anAuthorization
-header containig a token issued by the legacy server should be permitted
The problem
When requesting the /non-oauth
- or /unsecured
-endpoint with a token issued by the legacy server, Spring Security will deny the request. I believe this happens because Spring Security cannot Authenticate the user token, even though the user is authorized to access the endpoints (due to the "/**").permitAll()
config).
The question
Is there any way to disable the "Authentication-process" of a token on all paths that are not explicitly secured in the config?