Skip to content

Commit 7cfcfae

Browse files
Kehrlannjgrandja
authored andcommitted
BearerTokenAuthenticationEntryPoint uses context path
Closes gh-18528 Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent f7f5165 commit 7cfcfae

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/BearerTokenAuthenticationEntryPoint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ public void setRealmName(String realmName) {
9898
}
9999

100100
private static String getResourceMetadataParameter(HttpServletRequest request) {
101+
String path = request.getContextPath()
102+
+ OAuth2ProtectedResourceMetadataFilter.DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI;
101103
// @formatter:off
102104
return UriComponentsBuilder.fromUriString(UrlUtils.buildFullRequestUrl(request))
103-
.replacePath(OAuth2ProtectedResourceMetadataFilter.DEFAULT_OAUTH2_PROTECTED_RESOURCE_METADATA_ENDPOINT_URI)
105+
.replacePath(path)
104106
.replaceQuery(null)
105107
.fragment(null)
106108
.build()

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/BearerTokenAuthenticationEntryPointTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public void commenceWhenNoBearerTokenErrorAndRealmSetThenStatus401AndAuthHeaderW
6565
"Bearer realm=\"test\", resource_metadata=\"http://localhost/.well-known/oauth-protected-resource\"");
6666
}
6767

68+
@Test
69+
public void commenceWhenNoBearerTokenErrorAndContextPathSetThenStatus401AndAuthHeaderWithContextPath() {
70+
MockHttpServletRequest request = new MockHttpServletRequest();
71+
request.setContextPath("/ctx");
72+
MockHttpServletResponse response = new MockHttpServletResponse();
73+
this.authenticationEntryPoint.commence(request, response, new BadCredentialsException("test"));
74+
assertThat(response.getStatus()).isEqualTo(401);
75+
assertThat(response.getHeader("WWW-Authenticate"))
76+
.isEqualTo("Bearer resource_metadata=\"http://localhost/ctx/.well-known/oauth-protected-resource\"");
77+
78+
}
79+
6880
@Test
6981
public void commenceWhenInvalidRequestErrorThenStatus400AndHeaderWithError() throws Exception {
7082
MockHttpServletRequest request = new MockHttpServletRequest();

0 commit comments

Comments
 (0)