Skip to content

Commit 050aab0

Browse files
committed
Work Around HtmlUnitRequestBuilder setting ServletRequest.serverPort = -1
See the following issues: - spring-projects/spring-framework#35709 - spring-projects/spring-boot#47857 Without this workaround we get the following error: ``` org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter$OAuth2AuthorizationRequestException: Bad authority at org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter.doFilterInternal(OAuth2AuthorizationRequestRedirectFilter.java:190) ~[spring-security-oauth2-client-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] Caused by: org.springframework.web.util.InvalidUrlException: Bad authority at org.springframework.web.util.RfcUriParser.fail(RfcUriParser.java:65) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.RfcUriParser.verify(RfcUriParser.java:53) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.RfcUriParser$InternalParser.captureHost(RfcUriParser.java:530) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.RfcUriParser$State$4.handleNext(RfcUriParser.java:199) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.RfcUriParser$InternalParser.parse(RfcUriParser.java:465) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.RfcUriParser.parse(RfcUriParser.java:47) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.UriComponentsBuilder.fromUriString(UriComponentsBuilder.java:208) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.web.util.UriComponentsBuilder.fromUriString(UriComponentsBuilder.java:180) ~[spring-web-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] at org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver.expandRedirectUri(DefaultOAuth2AuthorizationRequestResolver.java:244) ~[spring-security-oauth2-client-7.0.0-SNAPSHOT.jar:7.0.0-SNAPSHOT] ... 160 common frames omitted ```
1 parent 0f34f22 commit 050aab0

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

servlet/spring-boot/java/oauth2/login/src/integTest/java/example/OAuth2LoginApplicationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.springframework.beans.factory.annotation.Autowired;
4242
import org.springframework.boot.test.context.SpringBootTest;
43+
import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
4344
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.Configuration;
@@ -66,6 +67,8 @@
6667
import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
6768
import org.springframework.security.web.SecurityFilterChain;
6869
import org.springframework.test.web.servlet.MockMvc;
70+
import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
71+
import org.springframework.web.util.DefaultUriBuilderFactory;
6972
import org.springframework.web.util.UriComponents;
7073
import org.springframework.web.util.UriComponentsBuilder;
7174

@@ -347,6 +350,14 @@ private WebResponse followLinkDisableRedirects(HtmlAnchor anchorElement) throws
347350
@EnableWebSecurity
348351
public static class SecurityTestConfig {
349352

353+
@Bean
354+
static MockMvcWebClientBuilder webClientBuilder(MockMvc mockMvc) {
355+
356+
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory("http://localhost:8080");
357+
UriBuilderFactoryWebClient delegate = new UriBuilderFactoryWebClient(factory);
358+
return MockMvcWebClientBuilder.mockMvcSetup(mockMvc).withDelegate(delegate);
359+
}
360+
350361
@Bean
351362
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
352363
// @formatter:off

0 commit comments

Comments
 (0)