Skip to content

Commit fc007aa

Browse files
committed
Check OpenSAML Version in XML Support
Closes gh-12483
1 parent eaaa813 commit fc007aa

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

config/src/main/java/org/springframework/security/config/http/Saml2LoginBeanDefinitionParserUtils.java

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.config.http;
1818

19+
import org.opensaml.core.Version;
1920
import org.w3c.dom.Element;
2021

2122
import org.springframework.beans.BeanMetadataElement;
@@ -27,6 +28,7 @@
2728
import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver;
2829
import org.springframework.security.saml2.provider.service.web.HttpSessionSaml2AuthenticationRequestRepository;
2930
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter;
31+
import org.springframework.util.ClassUtils;
3032
import org.springframework.util.StringUtils;
3133

3234
/**
@@ -35,6 +37,8 @@
3537
*/
3638
final class Saml2LoginBeanDefinitionParserUtils {
3739

40+
private static final String OPEN_SAML_4_VERSION = "4";
41+
3842
private static final String ATT_RELYING_PARTY_REGISTRATION_REPOSITORY_REF = "relying-party-registration-repository-ref";
3943

4044
private static final String ATT_AUTHENTICATION_REQUEST_REPOSITORY_REF = "authentication-request-repository-ref";
@@ -78,15 +82,27 @@ static BeanMetadataElement createDefaultAuthenticationRequestResolver(
7882
.rootBeanDefinition(DefaultRelyingPartyRegistrationResolver.class)
7983
.addConstructorArgValue(relyingPartyRegistrationRepository)
8084
.getBeanDefinition();
85+
if (version().startsWith("4")) {
86+
return BeanDefinitionBuilder.rootBeanDefinition(
87+
"org.springframework.security.saml2.provider.service.web.authentication.OpenSaml4AuthenticationRequestResolver")
88+
.addConstructorArgValue(defaultRelyingPartyRegistrationResolver)
89+
.getBeanDefinition();
90+
}
8191
return BeanDefinitionBuilder.rootBeanDefinition(
82-
"org.springframework.security.saml2.provider.service.web.authentication.OpenSaml4AuthenticationRequestResolver")
92+
"org.springframework.security.saml2.provider.service.web.authentication.OpenSamlAuthenticationRequestResolver")
8393
.addConstructorArgValue(defaultRelyingPartyRegistrationResolver)
8494
.getBeanDefinition();
8595
}
8696

8797
static BeanDefinition createAuthenticationProvider() {
88-
return BeanDefinitionBuilder.rootBeanDefinition(
89-
"org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider")
98+
if (version().startsWith("4")) {
99+
return BeanDefinitionBuilder.rootBeanDefinition(
100+
"org.springframework.security.saml2.provider.service.authentication.OpenSaml4AuthenticationProvider")
101+
.getBeanDefinition();
102+
}
103+
return BeanDefinitionBuilder
104+
.rootBeanDefinition(
105+
"org.springframework.security.saml2.provider.service.authentication.OpenSamlAuthenticationProvider")
90106
.getBeanDefinition();
91107
}
92108

@@ -108,4 +124,17 @@ static BeanDefinition createDefaultAuthenticationConverter(BeanMetadataElement r
108124
.getBeanDefinition();
109125
}
110126

127+
static String version() {
128+
String version = Version.getVersion();
129+
if (StringUtils.hasText(version)) {
130+
return version;
131+
}
132+
boolean openSaml4ClassPresent = ClassUtils
133+
.isPresent("org.opensaml.core.xml.persist.impl.PassthroughSourceStrategy", null);
134+
if (openSaml4ClassPresent) {
135+
return OPEN_SAML_4_VERSION;
136+
}
137+
throw new IllegalStateException("cannot determine OpenSAML version");
138+
}
139+
111140
}

config/src/main/java/org/springframework/security/config/http/Saml2LogoutBeanDefinitionParserUtils.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.config.http;
1818

19+
import org.opensaml.core.Version;
1920
import org.w3c.dom.Element;
2021

2122
import org.springframework.beans.BeanMetadataElement;
@@ -25,6 +26,7 @@
2526
import org.springframework.security.saml2.provider.service.authentication.logout.OpenSamlLogoutResponseValidator;
2627
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
2728
import org.springframework.security.saml2.provider.service.web.authentication.logout.HttpSessionLogoutRequestRepository;
29+
import org.springframework.util.ClassUtils;
2830
import org.springframework.util.StringUtils;
2931

3032
/**
@@ -33,6 +35,8 @@
3335
*/
3436
final class Saml2LogoutBeanDefinitionParserUtils {
3537

38+
private static final String OPEN_SAML_4_VERSION = "4";
39+
3640
private static final String ATT_RELYING_PARTY_REGISTRATION_REPOSITORY_REF = "relying-party-registration-repository-ref";
3741

3842
private static final String ATT_LOGOUT_REQUEST_VALIDATOR_REF = "logout-request-validator-ref";
@@ -62,8 +66,14 @@ static BeanMetadataElement getLogoutResponseResolver(Element element, BeanMetada
6266
if (StringUtils.hasText(logoutResponseResolver)) {
6367
return new RuntimeBeanReference(logoutResponseResolver);
6468
}
69+
if (version().startsWith("4")) {
70+
return BeanDefinitionBuilder.rootBeanDefinition(
71+
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutResponseResolver")
72+
.addConstructorArgValue(registrations)
73+
.getBeanDefinition();
74+
}
6575
return BeanDefinitionBuilder.rootBeanDefinition(
66-
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutResponseResolver")
76+
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSamlLogoutResponseResolver")
6777
.addConstructorArgValue(registrations)
6878
.getBeanDefinition();
6979
}
@@ -97,10 +107,29 @@ static BeanMetadataElement getLogoutRequestResolver(Element element, BeanMetadat
97107
if (StringUtils.hasText(logoutRequestResolver)) {
98108
return new RuntimeBeanReference(logoutRequestResolver);
99109
}
110+
if (version().startsWith("4")) {
111+
return BeanDefinitionBuilder.rootBeanDefinition(
112+
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutRequestResolver")
113+
.addConstructorArgValue(registrations)
114+
.getBeanDefinition();
115+
}
100116
return BeanDefinitionBuilder.rootBeanDefinition(
101-
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSaml4LogoutRequestResolver")
117+
"org.springframework.security.saml2.provider.service.web.authentication.logout.OpenSamlLogoutRequestResolver")
102118
.addConstructorArgValue(registrations)
103119
.getBeanDefinition();
104120
}
105121

122+
static String version() {
123+
String version = Version.getVersion();
124+
if (StringUtils.hasText(version)) {
125+
return version;
126+
}
127+
boolean openSaml4ClassPresent = ClassUtils
128+
.isPresent("org.opensaml.core.xml.persist.impl.PassthroughSourceStrategy", null);
129+
if (openSaml4ClassPresent) {
130+
return OPEN_SAML_4_VERSION;
131+
}
132+
throw new IllegalStateException("cannot determine OpenSAML version");
133+
}
134+
106135
}

0 commit comments

Comments
 (0)