From 66213e5b2e954bf4d687e6622b96ede446401dfa Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Fri, 18 Mar 2022 12:53:55 -0600 Subject: [PATCH] Add Default Test to HttpBasicConfigurerTests Issue gh-10973 --- .../configurers/HttpBasicConfigurerTests.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurerTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurerTests.java index 58f83e9d08..99594611dc 100644 --- a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurerTests.java +++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpBasicConfigurerTests.java @@ -25,6 +25,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -33,13 +34,19 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur import org.springframework.security.config.test.SpringTestContext; import org.springframework.security.config.test.SpringTestContextExtension; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -48,6 +55,7 @@ import static org.mockito.Mockito.verify; import static org.springframework.security.config.Customizer.withDefaults; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -118,6 +126,13 @@ public class HttpBasicConfigurerTests { this.mvc.perform(rememberMeRequest).andExpect(cookie().exists("remember-me")); } + @Test + public void httpBasicWhenDefaultsThenAcceptsBasicCredentials() throws Exception { + this.spring.register(HttpBasic.class, Users.class, Home.class).autowire(); + this.mvc.perform(get("/").with(httpBasic("user", "password"))).andExpect(status().isOk()) + .andExpect(content().string("user")); + } + @EnableWebSecurity static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter { @@ -269,6 +284,37 @@ public class HttpBasicConfigurerTests { @Override @Bean public UserDetailsService userDetailsService() { + return new InMemoryUserDetailsManager( + // @formatter:off + org.springframework.security.core.userdetails.User.withDefaultPasswordEncoder() + .username("user") + .password("password") + .roles("USER") + .build() + // @formatter:on + ); + } + + } + + @EnableWebSecurity + static class HttpBasic { + + @Bean + SecurityFilterChain web(HttpSecurity http) throws Exception { + http.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated()) + .httpBasic(Customizer.withDefaults()); + + return http.build(); + } + + } + + @Configuration + static class Users { + + @Bean + UserDetailsService userDetailsService() { return new InMemoryUserDetailsManager( // @formatter:off User.withDefaultPasswordEncoder() @@ -282,4 +328,15 @@ public class HttpBasicConfigurerTests { } + @EnableWebMvc + @RestController + static class Home { + + @GetMapping("/") + String home(@AuthenticationPrincipal UserDetails user) { + return user.getUsername(); + } + + } + }