Skip to content

Commit 7ca22b2

Browse files
authored
Polish BearerTokenAuthenticationEntryPoint (#15)
There were a couple of comments that were missed in the PR merge, namely one from @jzheaux regarding setRealmName as well as a quick change to pull WWW-Authenticate from Spring's HttpHeader constant. Issue spring-projects/spring-security#5125
1 parent f80dff8 commit 7ca22b2

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616

1717
package org.springframework.security.oauth2.resourceserver.web;
1818

19-
import java.io.IOException;
20-
import java.util.LinkedHashMap;
21-
import java.util.Map;
22-
import java.util.stream.Collectors;
23-
24-
import javax.servlet.http.HttpServletRequest;
25-
import javax.servlet.http.HttpServletResponse;
26-
19+
import org.springframework.http.HttpHeaders;
2720
import org.springframework.http.HttpStatus;
2821
import org.springframework.security.core.AuthenticationException;
2922
import org.springframework.security.oauth2.resourceserver.BearerTokenAuthenticationException;
3023
import org.springframework.security.oauth2.resourceserver.BearerTokenError;
3124
import org.springframework.security.web.AuthenticationEntryPoint;
32-
import org.springframework.util.Assert;
25+
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
import java.io.IOException;
29+
import java.util.LinkedHashMap;
30+
import java.util.Map;
31+
import java.util.stream.Collectors;
3332

3433
/**
3534
* An {@link AuthenticationEntryPoint} implementation used to commence authentication of protected resource requests
@@ -83,7 +82,7 @@ public void commence(HttpServletRequest request, HttpServletResponse response,
8382
.map(attribute -> attribute.getKey() + "=\"" + attribute.getValue() + "\"")
8483
.collect(Collectors.joining(", ", " ", ""));
8584
}
86-
response.addHeader("WWW-Authenticate", wwwAuthenticate);
85+
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticate);
8786
response.sendError(httpStatus.value(), httpStatus.getReasonPhrase());
8887
}
8988

@@ -92,7 +91,6 @@ public void commence(HttpServletRequest request, HttpServletResponse response,
9291
* @param realmName the realm name
9392
*/
9493
public void setRealmName(String realmName) {
95-
Assert.hasText(realmName, "realmName must not be null");
9694
this.realmName = realmName;
9795
}
9896

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616

1717
package org.springframework.security.oauth2.resourceserver.web;
1818

19-
import java.io.IOException;
20-
2119
import org.junit.Before;
2220
import org.junit.Test;
23-
2421
import org.springframework.http.HttpStatus;
2522
import org.springframework.mock.web.MockHttpServletRequest;
2623
import org.springframework.mock.web.MockHttpServletResponse;
@@ -29,8 +26,10 @@
2926
import org.springframework.security.oauth2.resourceserver.BearerTokenError;
3027
import org.springframework.security.oauth2.resourceserver.BearerTokenErrorCodes;
3128

29+
import java.io.IOException;
30+
3231
import static org.assertj.core.api.Assertions.assertThat;
33-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
32+
import static org.assertj.core.api.Assertions.assertThatCode;
3433

3534
/**
3635
* Tests for {@link BearerTokenAuthenticationEntryPoint}.
@@ -172,9 +171,9 @@ public void commenceWhenInsufficientScopeAndRealmSetThenStatus403AndHeaderWithEr
172171
}
173172

174173
@Test
175-
public void setRealmNameWhenNullRealmNameThenIllegalArgumentException() {
176-
assertThatThrownBy(() -> this.authenticationEntryPoint.setRealmName(null))
177-
.isInstanceOf(IllegalArgumentException.class).hasMessage("realmName must not be null");
174+
public void setRealmNameWhenNullRealmNameThenNoExceptionThrown() {
175+
assertThatCode(() -> this.authenticationEntryPoint.setRealmName(null))
176+
.doesNotThrowAnyException();
178177
}
179178

180179
}

0 commit comments

Comments
 (0)