|
36 | 36 | import org.springframework.context.ConfigurableApplicationContext;
|
37 | 37 | import org.springframework.context.annotation.Bean;
|
38 | 38 | import org.springframework.context.annotation.Configuration;
|
| 39 | +import org.springframework.http.HttpStatus; |
39 | 40 | import org.springframework.http.MediaType;
|
40 | 41 | import org.springframework.mock.web.MockFilterChain;
|
41 | 42 | import org.springframework.mock.web.MockHttpServletRequest;
|
|
85 | 86 | import org.springframework.security.oauth2.jwt.JwtDecoderFactory;
|
86 | 87 | import org.springframework.security.oauth2.jwt.TestJwts;
|
87 | 88 | import org.springframework.security.web.FilterChainProxy;
|
| 89 | +import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
88 | 90 | import org.springframework.security.web.context.HttpRequestResponseHolder;
|
89 | 91 | import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
|
90 | 92 | import org.springframework.security.web.context.SecurityContextRepository;
|
| 93 | +import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher; |
91 | 94 | import org.springframework.test.web.servlet.MockMvc;
|
92 | 95 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
93 | 96 |
|
@@ -401,6 +404,30 @@ public void oauth2LoginWithOneClientConfiguredAndRequestXHRNotAuthenticatedThenD
|
401 | 404 | assertThat(this.response.getRedirectedUrl()).doesNotMatch("http://localhost/oauth2/authorization/google");
|
402 | 405 | }
|
403 | 406 |
|
| 407 | + @Test |
| 408 | + public void oauth2LoginWithHttpBasicOneClientConfiguredAndRequestXHRNotAuthenticatedThenUnauthorized() |
| 409 | + throws Exception { |
| 410 | + loadConfig(OAuth2LoginWithHttpBasicConfig.class); |
| 411 | + String requestUri = "/"; |
| 412 | + this.request = new MockHttpServletRequest("GET", requestUri); |
| 413 | + this.request.setServletPath(requestUri); |
| 414 | + this.request.addHeader("X-Requested-With", "XMLHttpRequest"); |
| 415 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 416 | + assertThat(this.response.getStatus()).isEqualTo(401); |
| 417 | + } |
| 418 | + |
| 419 | + @Test |
| 420 | + public void oauth2LoginWithXHREntryPointOneClientConfiguredAndRequestXHRNotAuthenticatedThenUnauthorized() |
| 421 | + throws Exception { |
| 422 | + loadConfig(OAuth2LoginWithXHREntryPointConfig.class); |
| 423 | + String requestUri = "/"; |
| 424 | + this.request = new MockHttpServletRequest("GET", requestUri); |
| 425 | + this.request.setServletPath(requestUri); |
| 426 | + this.request.addHeader("X-Requested-With", "XMLHttpRequest"); |
| 427 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.filterChain); |
| 428 | + assertThat(this.response.getStatus()).isEqualTo(401); |
| 429 | + } |
| 430 | + |
404 | 431 | // gh-9457
|
405 | 432 | @Test
|
406 | 433 | public void oauth2LoginWithOneAuthorizationCodeClientAndOtherClientsConfiguredThenRedirectForAuthorization()
|
@@ -896,6 +923,45 @@ ClientRegistrationRepository clientRegistrationRepository() {
|
896 | 923 |
|
897 | 924 | }
|
898 | 925 |
|
| 926 | + @EnableWebSecurity |
| 927 | + static class OAuth2LoginWithHttpBasicConfig extends CommonWebSecurityConfigurerAdapter { |
| 928 | + |
| 929 | + @Override |
| 930 | + protected void configure(HttpSecurity http) throws Exception { |
| 931 | + // @formatter:off |
| 932 | + http |
| 933 | + .oauth2Login() |
| 934 | + .clientRegistrationRepository( |
| 935 | + new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) |
| 936 | + .and() |
| 937 | + .httpBasic(); |
| 938 | + // @formatter:on |
| 939 | + super.configure(http); |
| 940 | + } |
| 941 | + |
| 942 | + } |
| 943 | + |
| 944 | + @EnableWebSecurity |
| 945 | + static class OAuth2LoginWithXHREntryPointConfig extends CommonWebSecurityConfigurerAdapter { |
| 946 | + |
| 947 | + @Override |
| 948 | + protected void configure(HttpSecurity http) throws Exception { |
| 949 | + // @formatter:off |
| 950 | + http |
| 951 | + .oauth2Login() |
| 952 | + .clientRegistrationRepository( |
| 953 | + new InMemoryClientRegistrationRepository(GOOGLE_CLIENT_REGISTRATION)) |
| 954 | + .and() |
| 955 | + .exceptionHandling() |
| 956 | + .defaultAuthenticationEntryPointFor( |
| 957 | + new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED), |
| 958 | + new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest")); |
| 959 | + // @formatter:on |
| 960 | + super.configure(http); |
| 961 | + } |
| 962 | + |
| 963 | + } |
| 964 | + |
899 | 965 | private abstract static class CommonWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
|
900 | 966 |
|
901 | 967 | @Override
|
|
0 commit comments