The "Bearer" keyword should be case-insensitive
The Authorization header was matched for OAuth2 against the "Bearer" keyword in a case sensitive fashion. According to RFC 2617, it should be case insensitive and some oauth clients (including some earlier versions of spring-security) expect it so.
This commit is contained in:
parent
fdc81822ec
commit
63f2b6094f
|
@ -36,7 +36,9 @@ import org.springframework.util.StringUtils;
|
||||||
*/
|
*/
|
||||||
public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
||||||
|
|
||||||
private static final Pattern authorizationPattern = Pattern.compile("^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$");
|
private static final Pattern authorizationPattern = Pattern.compile(
|
||||||
|
"^Bearer (?<token>[a-zA-Z0-9-._~+/]+)=*$",
|
||||||
|
Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
private boolean allowFormEncodedBodyParameter = false;
|
private boolean allowFormEncodedBodyParameter = false;
|
||||||
|
|
||||||
|
@ -87,7 +89,7 @@ public final class DefaultBearerTokenResolver implements BearerTokenResolver {
|
||||||
|
|
||||||
private static String resolveFromAuthorizationHeader(HttpServletRequest request) {
|
private static String resolveFromAuthorizationHeader(HttpServletRequest request) {
|
||||||
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
|
String authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
|
||||||
if (StringUtils.hasText(authorization) && authorization.startsWith("Bearer")) {
|
if (StringUtils.startsWithIgnoreCase(authorization, "bearer")) {
|
||||||
Matcher matcher = authorizationPattern.matcher(authorization);
|
Matcher matcher = authorizationPattern.matcher(authorization);
|
||||||
|
|
||||||
if (!matcher.matches()) {
|
if (!matcher.matches()) {
|
||||||
|
|
|
@ -51,6 +51,14 @@ public class DefaultBearerTokenResolverTests {
|
||||||
assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
|
assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void resolveWhenLowercaseHeaderIsPresentThenTokenIsResolved() {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
request.addHeader("authorization", "bearer " + TEST_TOKEN);
|
||||||
|
|
||||||
|
assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveWhenNoHeaderIsPresentThenTokenIsNotResolved() {
|
public void resolveWhenNoHeaderIsPresentThenTokenIsNotResolved() {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
|
Loading…
Reference in New Issue