Skip to content

Commit ea6a6c4

Browse files
committed
Proposed fix for missing WWW-Authenticate header
Current implementation does not include the WWW-Authenticate header when returning a 401 for missing/invalid credentials when attempting to access the token endpoints. This PR would change to use the standard BasicAuthenticationEntryPoint in order to populate this header correctly. Signed-off-by: Lucian Holland <[email protected]> Fixes-468
1 parent b76300b commit ea6a6c4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationServerConfigurer.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.context.event.GenericApplicationListenerAdapter;
2828
import org.springframework.context.event.SmartApplicationListener;
2929
import org.springframework.http.HttpMethod;
30-
import org.springframework.http.HttpStatus;
3130
import org.springframework.security.config.Customizer;
3231
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3332
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
@@ -48,8 +47,8 @@
4847
import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings;
4948
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenGenerator;
5049
import org.springframework.security.oauth2.server.authorization.web.NimbusJwkSetEndpointFilter;
51-
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
5250
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
51+
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
5352
import org.springframework.security.web.context.SecurityContextHolderFilter;
5453
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
5554
import org.springframework.security.web.util.matcher.OrRequestMatcher;
@@ -87,6 +86,8 @@ public final class OAuth2AuthorizationServerConfigurer
8786

8887
private RequestMatcher endpointsMatcher;
8988

89+
private String realm = "oauth2/client";
90+
9091
/**
9192
* Returns a new instance of {@link OAuth2AuthorizationServerConfigurer} for
9293
* configuring.
@@ -277,6 +278,16 @@ public OAuth2AuthorizationServerConfigurer oidc(Customizer<OidcConfigurer> oidcC
277278
return this;
278279
}
279280

281+
/**
282+
* Configures the default realm value to be return in the WWW-Authenticate header
283+
* @param realm the authentication realm for this server
284+
* @return the {@link OAuth2AuthorizationServerConfigurer} for further configuration
285+
*/
286+
public OAuth2AuthorizationServerConfigurer realm(String realm) {
287+
this.realm = realm;
288+
return this;
289+
}
290+
280291
/**
281292
* Returns a {@link RequestMatcher} for the authorization server endpoints.
282293
* @return a {@link RequestMatcher} for the authorization server endpoints
@@ -344,7 +355,9 @@ public void init(HttpSecurity httpSecurity) throws Exception {
344355
ExceptionHandlingConfigurer<HttpSecurity> exceptionHandling = httpSecurity
345356
.getConfigurer(ExceptionHandlingConfigurer.class);
346357
if (exceptionHandling != null) {
347-
exceptionHandling.defaultAuthenticationEntryPointFor(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
358+
var entryPoint = new BasicAuthenticationEntryPoint();
359+
entryPoint.setRealmName(this.realm);
360+
exceptionHandling.defaultAuthenticationEntryPointFor(entryPoint,
348361
new OrRequestMatcher(getRequestMatcher(OAuth2TokenEndpointConfigurer.class),
349362
getRequestMatcher(OAuth2TokenIntrospectionEndpointConfigurer.class),
350363
getRequestMatcher(OAuth2TokenRevocationEndpointConfigurer.class),

0 commit comments

Comments
 (0)