33
33
34
34
import org .springframework .security .oauth2 .client .web .server .ServerAuthorizationRequestRepository ;
35
35
import org .springframework .security .oauth2 .client .web .server .WebSessionOAuth2ServerAuthorizationRequestRepository ;
36
+ import org .springframework .http .HttpStatus ;
36
37
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
37
38
import org .springframework .security .oauth2 .core .OAuth2AuthorizationException ;
38
39
import reactor .core .publisher .Mono ;
113
114
import org .springframework .security .web .server .authentication .AnonymousAuthenticationWebFilter ;
114
115
import org .springframework .security .web .server .authentication .AuthenticationWebFilter ;
115
116
import org .springframework .security .web .server .authentication .HttpBasicServerAuthenticationEntryPoint ;
117
+ import org .springframework .security .web .server .authentication .HttpStatusServerEntryPoint ;
116
118
import org .springframework .security .web .server .authentication .ReactivePreAuthenticatedAuthenticationManager ;
117
119
import org .springframework .security .web .server .authentication .RedirectServerAuthenticationEntryPoint ;
118
120
import org .springframework .security .web .server .authentication .RedirectServerAuthenticationFailureHandler ;
@@ -2965,11 +2967,17 @@ private RequestCacheSpec() {}
2965
2967
* @see #httpBasic()
2966
2968
*/
2967
2969
public class HttpBasicSpec {
2970
+
2971
+ private final ServerWebExchangeMatcher xhrMatcher = (exchange ) -> Mono .just (exchange .getRequest ().getHeaders ())
2972
+ .filter ((h ) -> h .getOrEmpty ("X-Requested-With" ).contains ("XMLHttpRequest" ))
2973
+ .flatMap ((h ) -> ServerWebExchangeMatcher .MatchResult .match ())
2974
+ .switchIfEmpty (ServerWebExchangeMatcher .MatchResult .notMatch ());
2975
+
2968
2976
private ReactiveAuthenticationManager authenticationManager ;
2969
2977
2970
2978
private ServerSecurityContextRepository securityContextRepository ;
2971
2979
2972
- private ServerAuthenticationEntryPoint entryPoint = new HttpBasicServerAuthenticationEntryPoint () ;
2980
+ private ServerAuthenticationEntryPoint entryPoint ;
2973
2981
2974
2982
/**
2975
2983
* The {@link ReactiveAuthenticationManager} used to authenticate. Defaults to
@@ -3034,7 +3042,13 @@ protected void configure(ServerHttpSecurity http) {
3034
3042
MediaType .APPLICATION_OCTET_STREAM , MediaType .APPLICATION_XML ,
3035
3043
MediaType .MULTIPART_FORM_DATA , MediaType .TEXT_XML );
3036
3044
restMatcher .setIgnoredMediaTypes (Collections .singleton (MediaType .ALL ));
3037
- ServerHttpSecurity .this .defaultEntryPoints .add (new DelegateEntry (restMatcher , this .entryPoint ));
3045
+ ServerWebExchangeMatcher notHtmlMatcher = new NegatedServerWebExchangeMatcher (
3046
+ new MediaTypeServerWebExchangeMatcher (MediaType .TEXT_HTML ));
3047
+ ServerWebExchangeMatcher restNotHtmlMatcher = new AndServerWebExchangeMatcher (
3048
+ Arrays .asList (notHtmlMatcher , restMatcher ));
3049
+ ServerWebExchangeMatcher preferredMatcher = new OrServerWebExchangeMatcher (
3050
+ Arrays .asList (this .xhrMatcher , restNotHtmlMatcher ));
3051
+ ServerHttpSecurity .this .defaultEntryPoints .add (new DelegateEntry (preferredMatcher , this .entryPoint ));
3038
3052
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter (
3039
3053
this .authenticationManager );
3040
3054
authenticationFilter .setAuthenticationFailureHandler (new ServerAuthenticationEntryPointFailureHandler (this .entryPoint ));
@@ -3043,7 +3057,15 @@ protected void configure(ServerHttpSecurity http) {
3043
3057
http .addFilterAt (authenticationFilter , SecurityWebFiltersOrder .HTTP_BASIC );
3044
3058
}
3045
3059
3046
- private HttpBasicSpec () {}
3060
+ private HttpBasicSpec () {
3061
+ List <DelegateEntry > entryPoints = new ArrayList <>();
3062
+ entryPoints
3063
+ .add (new DelegateEntry (this .xhrMatcher , new HttpStatusServerEntryPoint (HttpStatus .UNAUTHORIZED )));
3064
+ DelegatingServerAuthenticationEntryPoint defaultEntryPoint = new DelegatingServerAuthenticationEntryPoint (
3065
+ entryPoints );
3066
+ defaultEntryPoint .setDefaultEntryPoint (new HttpBasicServerAuthenticationEntryPoint ());
3067
+ this .entryPoint = defaultEntryPoint ;
3068
+ }
3047
3069
}
3048
3070
3049
3071
/**
0 commit comments