Skip to content

Commit 77d08fb

Browse files
committed
Replace stream usage with for loops
Closes spring-projectsgh-401
1 parent 298b77d commit 77d08fb

File tree

15 files changed

+116
-110
lines changed

15 files changed

+116
-110
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/InMemoryOAuth2AuthorizationService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ public OAuth2Authorization findById(String id) {
9797
@Override
9898
public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType tokenType) {
9999
Assert.hasText(token, "token cannot be empty");
100-
return this.authorizations.values().stream()
101-
.filter(authorization -> hasToken(authorization, token, tokenType))
102-
.findFirst()
103-
.orElse(null);
100+
for (OAuth2Authorization authorization : this.authorizations.values()) {
101+
if (hasToken(authorization, token, tokenType)) {
102+
return authorization;
103+
}
104+
}
105+
return null;
104106
}
105107

106108
private static boolean hasToken(OAuth2Authorization authorization, String token, @Nullable OAuth2TokenType tokenType) {

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2Authorization.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ public <T extends OAuth2Token> Token<T> getToken(Class<T> tokenType) {
150150
@SuppressWarnings("unchecked")
151151
public <T extends OAuth2Token> Token<T> getToken(String tokenValue) {
152152
Assert.hasText(tokenValue, "tokenValue cannot be empty");
153-
Token<?> token = this.tokens.values().stream()
154-
.filter(t -> t.getToken().getTokenValue().equals(tokenValue))
155-
.findFirst()
156-
.orElse(null);
157-
return token != null ? (Token<T>) token : null;
153+
for (Token<?> token : this.tokens.values()) {
154+
if (token.getToken().getTokenValue().equals(tokenValue)) {
155+
return (Token<T>) token;
156+
}
157+
}
158+
return null;
158159
}
159160

160161
/**

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2AuthorizationConsent.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Objects;
2222
import java.util.Set;
2323
import java.util.function.Consumer;
24-
import java.util.stream.Collectors;
2524

2625
import org.springframework.lang.NonNull;
2726
import org.springframework.security.core.GrantedAuthority;
@@ -91,11 +90,13 @@ public Set<GrantedAuthority> getAuthorities() {
9190
* @return the {@code scope}s granted to the client by the principal.
9291
*/
9392
public Set<String> getScopes() {
94-
return getAuthorities().stream()
95-
.map(GrantedAuthority::getAuthority)
96-
.filter(authority -> authority.startsWith(AUTHORITIES_SCOPE_PREFIX))
97-
.map(scope -> scope.replaceFirst(AUTHORITIES_SCOPE_PREFIX, ""))
98-
.collect(Collectors.toSet());
93+
Set<String> authorities = new HashSet<>();
94+
for (GrantedAuthority authority : getAuthorities()) {
95+
if (authority.getAuthority().startsWith(AUTHORITIES_SCOPE_PREFIX)) {
96+
authorities.add(authority.getAuthority().replaceFirst(AUTHORITIES_SCOPE_PREFIX, ""));
97+
}
98+
}
99+
return authorities;
99100
}
100101

101102
@Override

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeRequestAuthenticationProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.security.Principal;
1919
import java.time.Instant;
2020
import java.time.temporal.ChronoUnit;
21-
import java.util.Arrays;
2221
import java.util.Base64;
2322
import java.util.Collections;
2423
import java.util.HashMap;
@@ -448,7 +447,10 @@ private static boolean isLoopbackAddress(String host) {
448447
return false;
449448
}
450449
try {
451-
int[] address = Arrays.stream(ipv4Octets).mapToInt(Integer::parseInt).toArray();
450+
int[] address = new int[ipv4Octets.length];
451+
for (int i=0; i < ipv4Octets.length; i++) {
452+
address[i] = Integer.parseInt(ipv4Octets[i]);
453+
}
452454
return address[0] == 127 && address[1] >= 0 && address[1] <= 255 && address[2] >= 0 &&
453455
address[2] <= 255 && address[3] >= 1 && address[3] <= 255;
454456
} catch (NumberFormatException ex) {

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2ClientCredentialsAuthenticationProvider.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.LinkedHashSet;
1919
import java.util.Set;
2020
import java.util.function.Consumer;
21-
import java.util.stream.Collectors;
2221

2322
import org.springframework.beans.factory.annotation.Autowired;
2423
import org.springframework.security.authentication.AuthenticationProvider;
@@ -34,12 +33,12 @@
3433
import org.springframework.security.oauth2.jwt.Jwt;
3534
import org.springframework.security.oauth2.jwt.JwtClaimsSet;
3635
import org.springframework.security.oauth2.jwt.JwtEncoder;
36+
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
3737
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
3838
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
39+
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
3940
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
4041
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
41-
import org.springframework.security.oauth2.server.authorization.JwtEncodingContext;
42-
import org.springframework.security.oauth2.server.authorization.OAuth2TokenCustomizer;
4342
import org.springframework.util.Assert;
4443
import org.springframework.util.CollectionUtils;
4544

@@ -112,11 +111,10 @@ public Authentication authenticate(Authentication authentication) throws Authent
112111

113112
Set<String> authorizedScopes = registeredClient.getScopes(); // Default to configured scopes
114113
if (!CollectionUtils.isEmpty(clientCredentialsAuthentication.getScopes())) {
115-
Set<String> unauthorizedScopes = clientCredentialsAuthentication.getScopes().stream()
116-
.filter(requestedScope -> !registeredClient.getScopes().contains(requestedScope))
117-
.collect(Collectors.toSet());
118-
if (!CollectionUtils.isEmpty(unauthorizedScopes)) {
119-
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
114+
for (String requestedScope : clientCredentialsAuthentication.getScopes()) {
115+
if (!registeredClient.getScopes().contains(requestedScope)) {
116+
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_SCOPE));
117+
}
120118
}
121119
authorizedScopes = new LinkedHashSet<>(clientCredentialsAuthentication.getScopes());
122120
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2TokenIntrospectionEndpointFilter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package org.springframework.security.oauth2.server.authorization.web;
1717

1818
import java.io.IOException;
19+
import java.util.HashMap;
1920
import java.util.Map;
20-
import java.util.stream.Collectors;
2121

2222
import javax.servlet.FilterChain;
2323
import javax.servlet.ServletException;
@@ -161,14 +161,13 @@ public Authentication convert(HttpServletRequest request) {
161161
throwError(OAuth2ErrorCodes.INVALID_REQUEST, OAuth2ParameterNames.TOKEN_TYPE_HINT);
162162
}
163163

164-
// @formatter:off
165-
Map<String, Object> additionalParameters = parameters
166-
.entrySet()
167-
.stream()
168-
.filter(e -> !e.getKey().equals(OAuth2ParameterNames.TOKEN) &&
169-
!e.getKey().equals(OAuth2ParameterNames.TOKEN_TYPE_HINT))
170-
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
171-
// @formatter:on
164+
Map<String, Object> additionalParameters = new HashMap<>();
165+
parameters.forEach((key, value) -> {
166+
if (!key.equals(OAuth2ParameterNames.TOKEN) &&
167+
!key.equals(OAuth2ParameterNames.TOKEN_TYPE_HINT)) {
168+
additionalParameters.put(key, value.get(0));
169+
}
170+
});
172171

173172
return new OAuth2TokenIntrospectionAuthenticationToken(
174173
token, clientPrincipal, tokenTypeHint, additionalParameters);

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/DelegatingAuthenticationConverter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.Collections;
1919
import java.util.LinkedList;
2020
import java.util.List;
21-
import java.util.Objects;
2221

2322
import javax.servlet.http.HttpServletRequest;
2423

@@ -56,12 +55,12 @@ public DelegatingAuthenticationConverter(List<AuthenticationConverter> converter
5655
@Override
5756
public Authentication convert(HttpServletRequest request) {
5857
Assert.notNull(request, "request cannot be null");
59-
// @formatter:off
60-
return this.converters.stream()
61-
.map(converter -> converter.convert(request))
62-
.filter(Objects::nonNull)
63-
.findFirst()
64-
.orElse(null);
65-
// @formatter:on
58+
for (AuthenticationConverter converter : this.converters) {
59+
Authentication authentication = converter.convert(request);
60+
if (authentication != null) {
61+
return authentication;
62+
}
63+
}
64+
return null;
6665
}
6766
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2AuthorizationCodeAuthenticationConverter.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization.web.authentication;
1717

18+
import java.util.HashMap;
1819
import java.util.Map;
19-
import java.util.stream.Collectors;
2020

2121
import javax.servlet.http.HttpServletRequest;
2222

@@ -78,16 +78,15 @@ public Authentication convert(HttpServletRequest request) {
7878
OAuth2EndpointUtils.ACCESS_TOKEN_REQUEST_ERROR_URI);
7979
}
8080

81-
// @formatter:off
82-
Map<String, Object> additionalParameters = parameters
83-
.entrySet()
84-
.stream()
85-
.filter(e -> !e.getKey().equals(OAuth2ParameterNames.GRANT_TYPE) &&
86-
!e.getKey().equals(OAuth2ParameterNames.CLIENT_ID) &&
87-
!e.getKey().equals(OAuth2ParameterNames.CODE) &&
88-
!e.getKey().equals(OAuth2ParameterNames.REDIRECT_URI))
89-
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
90-
// @formatter:on
81+
Map<String, Object> additionalParameters = new HashMap<>();
82+
parameters.forEach((key, value) -> {
83+
if (!key.equals(OAuth2ParameterNames.GRANT_TYPE) &&
84+
!key.equals(OAuth2ParameterNames.CLIENT_ID) &&
85+
!key.equals(OAuth2ParameterNames.CODE) &&
86+
!key.equals(OAuth2ParameterNames.REDIRECT_URI)) {
87+
additionalParameters.put(key, value.get(0));
88+
}
89+
});
9190

9291
return new OAuth2AuthorizationCodeAuthenticationToken(
9392
code, clientPrincipal, redirectUri, additionalParameters);

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2AuthorizationCodeRequestAuthenticationConverter.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package org.springframework.security.oauth2.server.authorization.web.authentication;
1717

1818
import java.util.Arrays;
19+
import java.util.HashMap;
1920
import java.util.HashSet;
2021
import java.util.Map;
2122
import java.util.Set;
22-
import java.util.stream.Collectors;
2323

2424
import javax.servlet.http.HttpServletRequest;
2525

@@ -139,17 +139,16 @@ public Authentication convert(HttpServletRequest request) {
139139
throwError(OAuth2ErrorCodes.INVALID_REQUEST, PkceParameterNames.CODE_CHALLENGE_METHOD, PKCE_ERROR_URI);
140140
}
141141

142-
// @formatter:off
143-
Map<String, Object> additionalParameters = parameters
144-
.entrySet()
145-
.stream()
146-
.filter(e -> !e.getKey().equals(OAuth2ParameterNames.RESPONSE_TYPE) &&
147-
!e.getKey().equals(OAuth2ParameterNames.CLIENT_ID) &&
148-
!e.getKey().equals(OAuth2ParameterNames.REDIRECT_URI) &&
149-
!e.getKey().equals(OAuth2ParameterNames.SCOPE) &&
150-
!e.getKey().equals(OAuth2ParameterNames.STATE))
151-
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
152-
// @formatter:on
142+
Map<String, Object> additionalParameters = new HashMap<>();
143+
parameters.forEach((key, value) -> {
144+
if (!key.equals(OAuth2ParameterNames.RESPONSE_TYPE) &&
145+
!key.equals(OAuth2ParameterNames.CLIENT_ID) &&
146+
!key.equals(OAuth2ParameterNames.REDIRECT_URI) &&
147+
!key.equals(OAuth2ParameterNames.SCOPE) &&
148+
!key.equals(OAuth2ParameterNames.STATE)) {
149+
additionalParameters.put(key, value.get(0));
150+
}
151+
});
153152

154153
return OAuth2AuthorizationCodeRequestAuthenticationToken.with(clientId, principal)
155154
.authorizationUri(authorizationUri)

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2ClientCredentialsAuthenticationConverter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package org.springframework.security.oauth2.server.authorization.web.authentication;
1717

1818
import java.util.Arrays;
19+
import java.util.HashMap;
1920
import java.util.HashSet;
2021
import java.util.Map;
2122
import java.util.Set;
22-
import java.util.stream.Collectors;
2323

2424
import javax.servlet.http.HttpServletRequest;
2525

@@ -75,14 +75,13 @@ public Authentication convert(HttpServletRequest request) {
7575
Arrays.asList(StringUtils.delimitedListToStringArray(scope, " ")));
7676
}
7777

78-
// @formatter:off
79-
Map<String, Object> additionalParameters = parameters
80-
.entrySet()
81-
.stream()
82-
.filter(e -> !e.getKey().equals(OAuth2ParameterNames.GRANT_TYPE) &&
83-
!e.getKey().equals(OAuth2ParameterNames.SCOPE))
84-
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
85-
// @formatter:on
78+
Map<String, Object> additionalParameters = new HashMap<>();
79+
parameters.forEach((key, value) -> {
80+
if (!key.equals(OAuth2ParameterNames.GRANT_TYPE) &&
81+
!key.equals(OAuth2ParameterNames.SCOPE)) {
82+
additionalParameters.put(key, value.get(0));
83+
}
84+
});
8685

8786
return new OAuth2ClientCredentialsAuthenticationToken(
8887
clientPrincipal, requestedScopes, additionalParameters);

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/authentication/OAuth2RefreshTokenAuthenticationConverter.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package org.springframework.security.oauth2.server.authorization.web.authentication;
1717

1818
import java.util.Arrays;
19+
import java.util.HashMap;
1920
import java.util.HashSet;
2021
import java.util.Map;
2122
import java.util.Set;
22-
import java.util.stream.Collectors;
2323

2424
import javax.servlet.http.HttpServletRequest;
2525

@@ -85,15 +85,14 @@ public Authentication convert(HttpServletRequest request) {
8585
Arrays.asList(StringUtils.delimitedListToStringArray(scope, " ")));
8686
}
8787

88-
// @formatter:off
89-
Map<String, Object> additionalParameters = parameters
90-
.entrySet()
91-
.stream()
92-
.filter(e -> !e.getKey().equals(OAuth2ParameterNames.GRANT_TYPE) &&
93-
!e.getKey().equals(OAuth2ParameterNames.REFRESH_TOKEN) &&
94-
!e.getKey().equals(OAuth2ParameterNames.SCOPE))
95-
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().get(0)));
96-
// @formatter:on
88+
Map<String, Object> additionalParameters = new HashMap<>();
89+
parameters.forEach((key, value) -> {
90+
if (!key.equals(OAuth2ParameterNames.GRANT_TYPE) &&
91+
!key.equals(OAuth2ParameterNames.REFRESH_TOKEN) &&
92+
!key.equals(OAuth2ParameterNames.SCOPE)) {
93+
additionalParameters.put(key, value.get(0));
94+
}
95+
});
9796

9897
return new OAuth2RefreshTokenAuthenticationToken(
9998
refreshToken, clientPrincipal, requestedScopes, additionalParameters);

oauth2-authorization-server/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/authorization/OAuth2AuthorizationCodeGrantTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import java.time.Instant;
2525
import java.time.temporal.ChronoUnit;
2626
import java.util.Base64;
27+
import java.util.HashSet;
2728
import java.util.List;
2829
import java.util.Set;
29-
import java.util.stream.Collectors;
3030

3131
import com.nimbusds.jose.jwk.JWKSet;
3232
import com.nimbusds.jose.jwk.source.JWKSource;
@@ -273,9 +273,11 @@ public void requestWhenTokenRequestValidThenReturnAccessTokenResponse() throws E
273273
Jwt jwt = this.jwtDecoder.decode(accessTokenResponse.getAccessToken().getTokenValue());
274274
List<String> authoritiesClaim = jwt.getClaim(AUTHORITIES_CLAIM);
275275
Authentication principal = authorization.getAttribute(Principal.class.getName());
276-
Set<String> userAuthorities = principal.getAuthorities().stream()
277-
.map(GrantedAuthority::getAuthority)
278-
.collect(Collectors.toSet());
276+
Set<String> userAuthorities = new HashSet<>();
277+
for (GrantedAuthority authority : principal.getAuthorities()) {
278+
userAuthorities.add(authority.getAuthority());
279+
}
280+
279281
assertThat(authoritiesClaim).containsExactlyInAnyOrderElementsOf(userAuthorities);
280282
}
281283

@@ -612,9 +614,10 @@ OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
612614
if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(context.getAuthorizationGrantType()) &&
613615
OAuth2TokenType.ACCESS_TOKEN.equals(context.getTokenType())) {
614616
Authentication principal = context.getPrincipal();
615-
Set<String> authorities = principal.getAuthorities().stream()
616-
.map(GrantedAuthority::getAuthority)
617-
.collect(Collectors.toSet());
617+
Set<String> authorities = new HashSet<>();
618+
for (GrantedAuthority authority : principal.getAuthorities()) {
619+
authorities.add(authority.getAuthority());
620+
}
618621
context.getClaims().claim(AUTHORITIES_CLAIM, authorities);
619622
}
620623
};

0 commit comments

Comments
 (0)