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