Browse Source

Work Around HtmlUnitRequestBuilder setting ServletRequest.serverPort = -1

See the following issues:

- https://github.com/spring-projects/spring-framework/issues/35709
- https://github.com/spring-projects/spring-boot/issues/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
```
Rob Winch 1 month ago
parent
commit
050aab05a4

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

@@ -40,6 +40,7 @@ import org.junit.jupiter.api.Test;
 
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.htmlunit.UriBuilderFactoryWebClient;
 import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
 import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Configuration;
@@ -66,6 +67,8 @@ import org.springframework.security.oauth2.core.user.OAuth2User;
 import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
 import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
 import org.springframework.security.web.SecurityFilterChain;
 import org.springframework.security.web.SecurityFilterChain;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder;
+import org.springframework.web.util.DefaultUriBuilderFactory;
 import org.springframework.web.util.UriComponents;
 import org.springframework.web.util.UriComponents;
 import org.springframework.web.util.UriComponentsBuilder;
 import org.springframework.web.util.UriComponentsBuilder;
 
 
@@ -347,6 +350,14 @@ public class OAuth2LoginApplicationTests {
 	@EnableWebSecurity
 	@EnableWebSecurity
 	public static class SecurityTestConfig {
 	public static class SecurityTestConfig {
 
 
+		@Bean
+		static MockMvcWebClientBuilder webClientBuilder(MockMvc mockMvc) {
+
+			DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory("http://localhost:8080");
+			UriBuilderFactoryWebClient delegate = new UriBuilderFactoryWebClient(factory);
+			return MockMvcWebClientBuilder.mockMvcSetup(mockMvc).withDelegate(delegate);
+		}
+
 		@Bean
 		@Bean
 		public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
 		public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
 			// @formatter:off
 			// @formatter:off