Skip to content

Commit 2681b8a

Browse files
Remove MediaTypes
Signed-off-by: Tran Ngoc Nhan <[email protected]>
1 parent 46ee6ed commit 2681b8a

12 files changed

+39
-74
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/endpoint/DefaultOAuth2TokenRequestHeadersConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import org.springframework.http.HttpHeaders;
2525
import org.springframework.http.MediaType;
2626
import org.springframework.http.RequestEntity;
27-
import org.springframework.security.http.MediaTypes;
2827
import org.springframework.security.oauth2.client.registration.ClientRegistration;
2928
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3029

@@ -45,6 +44,9 @@ public final class DefaultOAuth2TokenRequestHeadersConverter<T extends AbstractO
4544
private static final MediaType APPLICATION_FORM_URLENCODED_UTF8 = new MediaType(
4645
MediaType.APPLICATION_FORM_URLENCODED, StandardCharsets.UTF_8);
4746

47+
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON,
48+
StandardCharsets.UTF_8);
49+
4850
private List<MediaType> accept = List.of(MediaType.APPLICATION_JSON);
4951

5052
private MediaType contentType = MediaType.APPLICATION_FORM_URLENCODED;
@@ -99,7 +101,7 @@ public void setEncodeClientCredentials(boolean encodeClientCredentials) {
99101
*/
100102
static <T extends AbstractOAuth2AuthorizationGrantRequest> DefaultOAuth2TokenRequestHeadersConverter<T> withCharsetUtf8() {
101103
DefaultOAuth2TokenRequestHeadersConverter<T> converter = new DefaultOAuth2TokenRequestHeadersConverter<>();
102-
converter.accept = List.of(MediaTypes.APPLICATION_JSON_UTF8);
104+
converter.accept = List.of(APPLICATION_JSON_UTF8);
103105
converter.contentType = APPLICATION_FORM_URLENCODED_UTF8;
104106
return converter;
105107
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultAuthorizationCodeTokenResponseClientTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.http.HttpMethod;
3535
import org.springframework.http.MediaType;
36-
import org.springframework.security.http.MediaTypes;
3736
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3837
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3938
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -119,7 +118,8 @@ public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() t
119118
Instant expiresAtAfter = Instant.now().plusSeconds(3600);
120119
RecordedRequest recordedRequest = this.server.takeRequest();
121120
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString());
122-
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.APPLICATION_JSON_UTF8_VALUE);
121+
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT))
122+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
123123
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE))
124124
.isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
125125
String formParameters = recordedRequest.getBody().readUtf8();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultClientCredentialsTokenResponseClientTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.http.HttpMethod;
3535
import org.springframework.http.MediaType;
36-
import org.springframework.security.http.MediaTypes;
3736
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3837
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3938
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -122,7 +121,8 @@ public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() t
122121
Instant expiresAtAfter = Instant.now().plusSeconds(3600);
123122
RecordedRequest recordedRequest = this.server.takeRequest();
124123
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString());
125-
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.APPLICATION_JSON_UTF8_VALUE);
124+
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT))
125+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
126126
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE))
127127
.isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
128128
String formParameters = recordedRequest.getBody().readUtf8();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultJwtBearerTokenResponseClientTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,7 +29,6 @@
2929
import org.springframework.http.HttpHeaders;
3030
import org.springframework.http.HttpMethod;
3131
import org.springframework.http.MediaType;
32-
import org.springframework.security.http.MediaTypes;
3332
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3433
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3534
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -116,7 +115,8 @@ public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() t
116115
Instant expiresAtAfter = Instant.now().plusSeconds(3600);
117116
RecordedRequest recordedRequest = this.server.takeRequest();
118117
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString());
119-
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.APPLICATION_JSON_UTF8_VALUE);
118+
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT))
119+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
120120
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE))
121121
.isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
122122
String formParameters = recordedRequest.getBody().readUtf8();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultPasswordTokenResponseClientTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,6 @@
3333
import org.springframework.http.HttpHeaders;
3434
import org.springframework.http.HttpMethod;
3535
import org.springframework.http.MediaType;
36-
import org.springframework.security.http.MediaTypes;
3736
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3837
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3938
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -116,7 +115,8 @@ public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() t
116115
Instant expiresAtAfter = Instant.now().plusSeconds(3600);
117116
RecordedRequest recordedRequest = this.server.takeRequest();
118117
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString());
119-
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.APPLICATION_JSON_UTF8_VALUE);
118+
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT))
119+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
120120
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE))
121121
.isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
122122
String formParameters = recordedRequest.getBody().readUtf8();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/DefaultRefreshTokenTokenResponseClientTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,6 @@
3434
import org.springframework.http.HttpHeaders;
3535
import org.springframework.http.HttpMethod;
3636
import org.springframework.http.MediaType;
37-
import org.springframework.security.http.MediaTypes;
3837
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3938
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
4039
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
@@ -118,7 +117,8 @@ public void getTokenResponseWhenSuccessResponseThenReturnAccessTokenResponse() t
118117
Instant expiresAtAfter = Instant.now().plusSeconds(3600);
119118
RecordedRequest recordedRequest = this.server.takeRequest();
120119
assertThat(recordedRequest.getMethod()).isEqualTo(HttpMethod.POST.toString());
121-
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT)).isEqualTo(MediaTypes.APPLICATION_JSON_UTF8_VALUE);
120+
assertThat(recordedRequest.getHeader(HttpHeaders.ACCEPT))
121+
.isEqualTo(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8");
122122
assertThat(recordedRequest.getHeader(HttpHeaders.CONTENT_TYPE))
123123
.isEqualTo(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8");
124124
assertThat(recordedRequest.getHeader(HttpHeaders.AUTHORIZATION)).startsWith("Basic ");

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/JwtBearerGrantRequestEntityConverterTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.RequestEntity;
28-
import org.springframework.security.http.MediaTypes;
2928
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3029
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3130
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -135,7 +134,8 @@ public void convertWhenGrantRequestValidThenConverts() {
135134
assertThat(requestEntity.getUrl().toASCIIString())
136135
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
137136
HttpHeaders headers = requestEntity.getHeaders();
138-
assertThat(headers.getAccept()).contains(MediaType.valueOf(MediaTypes.APPLICATION_JSON_UTF8_VALUE));
137+
assertThat(headers.getAccept())
138+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
139139
assertThat(headers.getContentType())
140140
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
141141
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).startsWith("Basic ");

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/OAuth2AuthorizationCodeGrantRequestEntityConverterTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@
2828
import org.springframework.http.HttpMethod;
2929
import org.springframework.http.MediaType;
3030
import org.springframework.http.RequestEntity;
31-
import org.springframework.security.http.MediaTypes;
3231
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3332
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3433
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -139,7 +138,8 @@ public void convertWhenGrantRequestValidThenConverts() {
139138
assertThat(requestEntity.getUrl().toASCIIString())
140139
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
141140
HttpHeaders headers = requestEntity.getHeaders();
142-
assertThat(headers.getAccept()).contains(MediaTypes.APPLICATION_JSON_UTF8);
141+
assertThat(headers.getAccept())
142+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
143143
assertThat(headers.getContentType())
144144
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
145145
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0JTNE");
@@ -178,7 +178,8 @@ public void convertWhenPkceGrantRequestValidThenConverts() {
178178
assertThat(requestEntity.getUrl().toASCIIString())
179179
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
180180
HttpHeaders headers = requestEntity.getHeaders();
181-
assertThat(headers.getAccept()).contains(MediaTypes.APPLICATION_JSON_UTF8);
181+
assertThat(headers.getAccept())
182+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
182183
assertThat(headers.getContentType())
183184
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
184185
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).isNull();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/OAuth2ClientCredentialsGrantRequestEntityConverterTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,6 @@
3030
import org.springframework.http.HttpMethod;
3131
import org.springframework.http.MediaType;
3232
import org.springframework.http.RequestEntity;
33-
import org.springframework.security.http.MediaTypes;
3433
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3534
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3635
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -124,7 +123,8 @@ public void convertWhenGrantRequestValidThenConverts() {
124123
assertThat(requestEntity.getUrl().toASCIIString())
125124
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
126125
HttpHeaders headers = requestEntity.getHeaders();
127-
assertThat(headers.getAccept()).contains(MediaTypes.APPLICATION_JSON_UTF8);
126+
assertThat(headers.getAccept())
127+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
128128
assertThat(headers.getContentType())
129129
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
130130
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).startsWith("Basic ");
@@ -153,7 +153,8 @@ public void convertWhenSpecialCharactersThenConvertsWithEncodedClientCredentials
153153
assertThat(requestEntity.getUrl().toASCIIString())
154154
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
155155
HttpHeaders headers = requestEntity.getHeaders();
156-
assertThat(headers.getAccept()).contains(MediaTypes.APPLICATION_JSON_UTF8);
156+
assertThat(headers.getAccept())
157+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
157158
assertThat(headers.getContentType())
158159
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
159160
String urlEncodedClientCredential = URLEncoder.encode(clientCredentialWithAnsiKeyboardSpecialCharacters,

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/endpoint/OAuth2PasswordGrantRequestEntityConverterTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
2525
import org.springframework.http.HttpMethod;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.RequestEntity;
28-
import org.springframework.security.http.MediaTypes;
2928
import org.springframework.security.oauth2.client.registration.ClientRegistration;
3029
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
3130
import org.springframework.security.oauth2.core.AuthorizationGrantType;
@@ -119,7 +118,8 @@ public void convertWhenGrantRequestValidThenConverts() {
119118
assertThat(requestEntity.getUrl().toASCIIString())
120119
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri());
121120
HttpHeaders headers = requestEntity.getHeaders();
122-
assertThat(headers.getAccept()).contains(MediaTypes.APPLICATION_JSON_UTF8);
121+
assertThat(headers.getAccept())
122+
.contains(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8"));
123123
assertThat(headers.getContentType())
124124
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8"));
125125
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).startsWith("Basic ");

0 commit comments

Comments
 (0)