1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
47
47
import org .springframework .security .saml2 .provider .service .web .authentication .logout .HttpSessionLogoutRequestRepository ;
48
48
import org .springframework .security .saml2 .provider .service .web .authentication .logout .OpenSaml3LogoutRequestResolver ;
49
49
import org .springframework .security .saml2 .provider .service .web .authentication .logout .OpenSaml3LogoutResponseResolver ;
50
- import org .springframework .security .saml2 .provider .service .web .authentication .logout .OpenSaml4LogoutRequestResolver ;
51
- import org .springframework .security .saml2 .provider .service .web .authentication .logout .OpenSaml4LogoutResponseResolver ;
52
50
import org .springframework .security .saml2 .provider .service .web .authentication .logout .Saml2LogoutRequestFilter ;
53
51
import org .springframework .security .saml2 .provider .service .web .authentication .logout .Saml2LogoutRequestRepository ;
54
52
import org .springframework .security .saml2 .provider .service .web .authentication .logout .Saml2LogoutRequestResolver ;
67
65
import org .springframework .security .web .util .matcher .AndRequestMatcher ;
68
66
import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
69
67
import org .springframework .security .web .util .matcher .RequestMatcher ;
68
+ import org .springframework .util .ClassUtils ;
69
+ import org .springframework .util .StringUtils ;
70
70
71
71
/**
72
72
* Adds SAML 2.0 logout support.
113
113
public final class Saml2LogoutConfigurer <H extends HttpSecurityBuilder <H >>
114
114
extends AbstractHttpConfigurer <Saml2LogoutConfigurer <H >, H > {
115
115
116
+ private static final String OPEN_SAML_4_VERSION = "4" ;
117
+
116
118
private ApplicationContext context ;
117
119
118
120
private RelyingPartyRegistrationRepository relyingPartyRegistrationRepository ;
@@ -304,6 +306,19 @@ private Saml2LogoutResponseResolver createSaml2LogoutResponseResolver(
304
306
return this .logoutResponseConfigurer .logoutResponseResolver (relyingPartyRegistrationResolver );
305
307
}
306
308
309
+ private String version () {
310
+ String version = Version .getVersion ();
311
+ if (StringUtils .hasText (version )) {
312
+ return version ;
313
+ }
314
+ boolean openSaml4ClassPresent = ClassUtils
315
+ .isPresent ("org.opensaml.core.xml.persist.impl.PassthroughSourceStrategy" , null );
316
+ if (openSaml4ClassPresent ) {
317
+ return OPEN_SAML_4_VERSION ;
318
+ }
319
+ throw new IllegalStateException ("cannot determine OpenSAML version" );
320
+ }
321
+
307
322
private <C > C getBeanOrNull (Class <C > clazz ) {
308
323
if (this .context == null ) {
309
324
return null ;
@@ -314,15 +329,6 @@ private <C> C getBeanOrNull(Class<C> clazz) {
314
329
return this .context .getBean (clazz );
315
330
}
316
331
317
- private String version () {
318
- String version = Version .getVersion ();
319
- if (version != null ) {
320
- return version ;
321
- }
322
- return Version .class .getModule ().getDescriptor ().version ().map (Object ::toString )
323
- .orElseThrow (() -> new IllegalStateException ("cannot determine OpenSAML version" ));
324
- }
325
-
326
332
/**
327
333
* A configurer for SAML 2.0 LogoutRequest components
328
334
*/
@@ -403,7 +409,7 @@ private Saml2LogoutRequestResolver logoutRequestResolver(
403
409
return this .logoutRequestResolver ;
404
410
}
405
411
if (version ().startsWith ("4" )) {
406
- return new OpenSaml4LogoutRequestResolver (relyingPartyRegistrationResolver );
412
+ return OpenSaml4LogoutSupportFactory . getLogoutRequestResolver (relyingPartyRegistrationResolver );
407
413
}
408
414
return new OpenSaml3LogoutRequestResolver (relyingPartyRegistrationResolver );
409
415
}
@@ -471,13 +477,13 @@ private Saml2LogoutResponseValidator logoutResponseValidator() {
471
477
472
478
private Saml2LogoutResponseResolver logoutResponseResolver (
473
479
RelyingPartyRegistrationResolver relyingPartyRegistrationResolver ) {
474
- if (this .logoutResponseResolver == null ) {
475
- if (version ().startsWith ("4" )) {
476
- return new OpenSaml4LogoutResponseResolver (relyingPartyRegistrationResolver );
477
- }
478
- return new OpenSaml3LogoutResponseResolver (relyingPartyRegistrationResolver );
480
+ if (this .logoutResponseResolver != null ) {
481
+ return this .logoutResponseResolver ;
479
482
}
480
- return this .logoutResponseResolver ;
483
+ if (version ().startsWith ("4" )) {
484
+ return OpenSaml4LogoutSupportFactory .getLogoutResponseResolver (relyingPartyRegistrationResolver );
485
+ }
486
+ return new OpenSaml3LogoutResponseResolver (relyingPartyRegistrationResolver );
481
487
}
482
488
483
489
}
@@ -520,4 +526,38 @@ public void logout(HttpServletRequest request, HttpServletResponse response, Aut
520
526
521
527
}
522
528
529
+ private static class OpenSaml4LogoutSupportFactory {
530
+
531
+ private static Saml2LogoutResponseResolver getLogoutResponseResolver (
532
+ RelyingPartyRegistrationResolver relyingPartyRegistrationResolver ) {
533
+ try {
534
+ Class <?> logoutResponseResolver = ClassUtils .forName (
535
+ "org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutResponseResolver" ,
536
+ OpenSaml4LogoutSupportFactory .class .getClassLoader ());
537
+ return (Saml2LogoutResponseResolver ) logoutResponseResolver
538
+ .getDeclaredConstructor (RelyingPartyRegistrationResolver .class )
539
+ .newInstance (relyingPartyRegistrationResolver );
540
+ }
541
+ catch (ReflectiveOperationException ex ) {
542
+ throw new IllegalStateException ("Could not instantiate OpenSaml4LogoutResponseResolver" , ex );
543
+ }
544
+ }
545
+
546
+ private static Saml2LogoutRequestResolver getLogoutRequestResolver (
547
+ RelyingPartyRegistrationResolver relyingPartyRegistrationResolver ) {
548
+ try {
549
+ Class <?> logoutRequestResolver = ClassUtils .forName (
550
+ "org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutRequestResolver" ,
551
+ OpenSaml4LogoutSupportFactory .class .getClassLoader ());
552
+ return (Saml2LogoutRequestResolver ) logoutRequestResolver
553
+ .getDeclaredConstructor (RelyingPartyRegistrationResolver .class )
554
+ .newInstance (relyingPartyRegistrationResolver );
555
+ }
556
+ catch (ReflectiveOperationException ex ) {
557
+ throw new IllegalStateException ("Could not instantiate OpenSaml4LogoutRequestResolver" , ex );
558
+ }
559
+ }
560
+
561
+ }
562
+
523
563
}
0 commit comments