Replace Streams with Loops
First version of replacing streams fix wwwAuthenticate and codestyle fix errors in implementation to pass tests Fix review notes Remove uneccessary final to align with cb Short circuit way to authorize Simplify error message, make code readably Return error while duplicate key found Delete check for duplicate, checkstyle issues Return duplicate error Fixes gh-7154
This commit is contained in:
committed by
Josh Cummings
parent
d6d0d89ff8
commit
f6c650db47
+10
-4
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.security.web.header.writers;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@@ -69,7 +66,7 @@ public final class ClearSiteDataHeaderWriter implements HeaderWriter {
|
||||
public ClearSiteDataHeaderWriter(String ...sources) {
|
||||
Assert.notEmpty(sources, "sources cannot be empty or null");
|
||||
this.requestMatcher = new SecureRequestMatcher();
|
||||
this.headerValue = Stream.of(sources).map(this::quote).collect(Collectors.joining(", "));
|
||||
this.headerValue = joinQuotes(sources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,6 +81,15 @@ public final class ClearSiteDataHeaderWriter implements HeaderWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private String joinQuotes(String ...sources) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < sources.length-1; i++) {
|
||||
sb.append(quote(sources[i])).append(", ");
|
||||
}
|
||||
sb.append(quote(sources[sources.length-1]));
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static final class SecureRequestMatcher implements RequestMatcher {
|
||||
public boolean matches(HttpServletRequest request) {
|
||||
return request.isSecure();
|
||||
|
||||
+6
-4
@@ -23,8 +23,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Delegates to a collection of {@link ServerAuthenticationSuccessHandler} implementations.
|
||||
@@ -43,7 +42,10 @@ public class DelegatingServerAuthenticationSuccessHandler implements ServerAuthe
|
||||
@Override
|
||||
public Mono<Void> onAuthenticationSuccess(WebFilterExchange exchange,
|
||||
Authentication authentication) {
|
||||
Stream<Mono<Void>> results = this.delegates.stream().map(delegate -> delegate.onAuthenticationSuccess(exchange, authentication));
|
||||
return Mono.when(results.collect(Collectors.toList()));
|
||||
List<Mono<Void>> results = new ArrayList<>();
|
||||
for (ServerAuthenticationSuccessHandler delegate : delegates) {
|
||||
results.add(delegate.onAuthenticationSuccess(exchange, authentication));
|
||||
}
|
||||
return Mono.when(results);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -20,8 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -50,10 +48,12 @@ public class DelegatingServerLogoutHandler implements ServerLogoutHandler {
|
||||
|
||||
@Override
|
||||
public Mono<Void> logout(WebFilterExchange exchange, Authentication authentication) {
|
||||
return Mono.when(this.delegates.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(delegate -> delegate.logout(exchange, authentication))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
List<Mono<Void>> results = new ArrayList<>();
|
||||
for (ServerLogoutHandler delegate : delegates) {
|
||||
if (delegate != null) {
|
||||
results.add(delegate.logout(exchange, authentication));
|
||||
}
|
||||
}
|
||||
return Mono.when(results);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -20,9 +20,6 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* <p>Writes the {@code Clear-Site-Data} response header when the request is secure.</p>
|
||||
*
|
||||
@@ -81,9 +78,12 @@ public final class ClearSiteDataServerHttpHeadersWriter implements ServerHttpHea
|
||||
}
|
||||
|
||||
private String transformToHeaderValue(Directive... directives) {
|
||||
return Stream.of(directives)
|
||||
.map(Directive::getHeaderValue)
|
||||
.collect(Collectors.joining(", "));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < directives.length - 1; i++) {
|
||||
sb.append(directives[i].headerValue).append(", ");
|
||||
}
|
||||
sb.append(directives[directives.length - 1].headerValue);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean isSecure(ServerWebExchange exchange) {
|
||||
|
||||
+6
-5
@@ -17,8 +17,7 @@ package org.springframework.security.web.server.header;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
@@ -43,8 +42,10 @@ public class CompositeServerHttpHeadersWriter implements ServerHttpHeadersWriter
|
||||
|
||||
@Override
|
||||
public Mono<Void> writeHttpHeaders(ServerWebExchange exchange) {
|
||||
Stream<Mono<Void>> results = writers.stream().map( writer -> writer.writeHttpHeaders(exchange));
|
||||
return Mono.when(results.collect(Collectors.toList()));
|
||||
List<Mono<Void>> results = new ArrayList<>();
|
||||
for (ServerHttpHeadersWriter writer : writers) {
|
||||
results.add(writer.writeHttpHeaders(exchange));
|
||||
}
|
||||
return Mono.when(results);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user