Skip to content

Commit 52ed165

Browse files
Move classpath checks to class member variable
Closes gh-11437
1 parent c15f45d commit 52ed165

File tree

13 files changed

+134
-35
lines changed

13 files changed

+134
-35
lines changed

config/src/main/java/org/springframework/security/config/annotation/authentication/configurers/ldap/LdapAuthenticationProviderConfigurer.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
public class LdapAuthenticationProviderConfigurer<B extends ProviderManagerBuilder<B>>
6262
extends SecurityConfigurerAdapter<AuthenticationManager, B> {
6363

64+
private static final String APACHEDS_CLASSNAME = "org.apache.directory.server.core.DefaultDirectoryService";
65+
66+
private static final String UNBOUNDID_CLASSNAME = "com.unboundid.ldap.listener.InMemoryDirectoryServer";
67+
68+
private static final boolean apacheDsPresent;
69+
70+
private static final boolean unboundIdPresent;
71+
6472
private String groupRoleAttribute = "cn";
6573

6674
private String groupSearchBase = "";
@@ -91,6 +99,12 @@ public class LdapAuthenticationProviderConfigurer<B extends ProviderManagerBuild
9199

92100
private GrantedAuthoritiesMapper authoritiesMapper;
93101

102+
static {
103+
ClassLoader classLoader = LdapAuthenticationProviderConfigurer.class.getClassLoader();
104+
apacheDsPresent = ClassUtils.isPresent(APACHEDS_CLASSNAME, classLoader);
105+
unboundIdPresent = ClassUtils.isPresent(UNBOUNDID_CLASSNAME, classLoader);
106+
}
107+
94108
private LdapAuthenticationProvider build() throws Exception {
95109
BaseLdapPathContextSource contextSource = getContextSource();
96110
LdapAuthenticator ldapAuthenticator = createLdapAuthenticator(contextSource);
@@ -562,13 +576,13 @@ private DefaultSpringSecurityContextSource build() throws Exception {
562576
}
563577

564578
private void startEmbeddedLdapServer() throws Exception {
565-
if (ClassUtils.isPresent(APACHEDS_CLASSNAME, getClass().getClassLoader())) {
579+
if (apacheDsPresent) {
566580
ApacheDSContainer apacheDsContainer = new ApacheDSContainer(this.root, this.ldif);
567581
apacheDsContainer.setPort(getPort());
568582
postProcess(apacheDsContainer);
569583
this.port = apacheDsContainer.getLocalPort();
570584
}
571-
else if (ClassUtils.isPresent(UNBOUNDID_CLASSNAME, getClass().getClassLoader())) {
585+
else if (unboundIdPresent) {
572586
UnboundIdContainer unboundIdContainer = new UnboundIdContainer(this.root, this.ldif);
573587
unboundIdContainer.setPort(getPort());
574588
postProcess(unboundIdContainer);

config/src/main/java/org/springframework/security/config/annotation/web/configuration/OAuth2ClientConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@
5151
@Import(OAuth2ClientConfiguration.OAuth2ClientWebMvcImportSelector.class)
5252
final class OAuth2ClientConfiguration {
5353

54+
private static final boolean webMvcPresent;
55+
56+
static {
57+
ClassLoader classLoader = OAuth2ClientConfiguration.class.getClassLoader();
58+
webMvcPresent = ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", classLoader);
59+
}
60+
5461
static class OAuth2ClientWebMvcImportSelector implements ImportSelector {
5562

5663
@Override
5764
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
58-
if (!ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet",
59-
getClass().getClassLoader())) {
65+
if (!webMvcPresent) {
6066
return new String[0];
6167
}
6268
return new String[] { "org.springframework.security.config.annotation.web.configuration."

config/src/main/java/org/springframework/security/config/annotation/web/configuration/SpringWebMvcImportSelector.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@
3030
*/
3131
class SpringWebMvcImportSelector implements ImportSelector {
3232

33+
private static final boolean webMvcPresent;
34+
35+
static {
36+
ClassLoader classLoader = SpringWebMvcImportSelector.class.getClassLoader();
37+
webMvcPresent = ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", classLoader);
38+
}
39+
3340
@Override
3441
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
35-
if (!ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", getClass().getClassLoader())) {
42+
if (!webMvcPresent) {
3643
return new String[0];
3744
}
3845
return new String[] {

config/src/main/java/org/springframework/security/config/annotation/web/configurers/CorsConfigurer.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,20 @@
4040
*/
4141
public class CorsConfigurer<H extends HttpSecurityBuilder<H>> extends AbstractHttpConfigurer<CorsConfigurer<H>, H> {
4242

43-
private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector";
44-
4543
private static final String CORS_CONFIGURATION_SOURCE_BEAN_NAME = "corsConfigurationSource";
4644

4745
private static final String CORS_FILTER_BEAN_NAME = "corsFilter";
4846

47+
private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector";
48+
49+
private static final boolean mvcPresent;
50+
4951
private CorsConfigurationSource configurationSource;
5052

53+
static {
54+
mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR, CorsConfigurer.class.getClassLoader());
55+
}
56+
5157
/**
5258
* Creates a new instance
5359
*
@@ -84,7 +90,6 @@ private CorsFilter getCorsFilter(ApplicationContext context) {
8490
CorsConfigurationSource.class);
8591
return new CorsFilter(configurationSource);
8692
}
87-
boolean mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR, context.getClassLoader());
8893
if (mvcPresent) {
8994
return MvcCorsFilter.getMvcCorsFilter(context);
9095
}

config/src/main/java/org/springframework/security/config/annotation/web/reactive/ReactiveOAuth2ClientImportSelector.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@
4747
*/
4848
final class ReactiveOAuth2ClientImportSelector implements ImportSelector {
4949

50+
private static final boolean oauth2ClientPresent;
51+
52+
static {
53+
oauth2ClientPresent = ClassUtils.isPresent(
54+
"org.springframework.security.oauth2.client.registration.ClientRegistration",
55+
ReactiveOAuth2ClientImportSelector.class.getClassLoader());
56+
}
57+
5058
@Override
5159
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
52-
if (!ClassUtils.isPresent("org.springframework.security.oauth2.client.registration.ClientRegistration",
53-
getClass().getClassLoader())) {
60+
if (!oauth2ClientPresent) {
5461
return new String[0];
5562
}
5663
return new String[] { "org.springframework.security.config.annotation.web.reactive."

config/src/main/java/org/springframework/security/config/annotation/web/reactive/WebFluxSecurityConfiguration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ class WebFluxSecurityConfiguration {
5353

5454
public static final String REACTIVE_CLIENT_REGISTRATION_REPOSITORY_CLASSNAME = "org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository";
5555

56-
private static final boolean isOAuth2Present = ClassUtils.isPresent(
57-
REACTIVE_CLIENT_REGISTRATION_REPOSITORY_CLASSNAME, WebFluxSecurityConfiguration.class.getClassLoader());
56+
private static final boolean isOAuth2Present;
5857

5958
private List<SecurityWebFilterChain> securityWebFilterChains;
6059

6160
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
6261

62+
static {
63+
isOAuth2Present = ClassUtils.isPresent(REACTIVE_CLIENT_REGISTRATION_REPOSITORY_CLASSNAME,
64+
WebFluxSecurityConfiguration.class.getClassLoader());
65+
}
66+
6367
@Autowired
6468
ApplicationContext context;
6569

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ final class AuthenticationConfigBuilder {
8080

8181
private final Log logger = LogFactory.getLog(getClass());
8282

83+
private static final boolean webMvcPresent;
84+
8385
private static final String ATT_REALM = "realm";
8486

8587
private static final String DEF_REALM = "Realm";
@@ -213,6 +215,11 @@ final class AuthenticationConfigBuilder {
213215

214216
private final List<BeanDefinition> csrfIgnoreRequestMatchers = new ManagedList<>();
215217

218+
static {
219+
ClassLoader classLoader = AuthenticationConfigBuilder.class.getClassLoader();
220+
webMvcPresent = ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", classLoader);
221+
}
222+
216223
AuthenticationConfigBuilder(Element element, boolean forceAutoConfig, ParserContext pc,
217224
SessionCreationPolicy sessionPolicy, BeanReference requestCache, BeanReference authenticationManager,
218225
BeanMetadataElement authenticationFilterSecurityContextHolderStrategyRef,
@@ -409,9 +416,7 @@ private void registerOAuth2ClientPostProcessors() {
409416
if (!this.oauth2LoginEnabled && !this.oauth2ClientEnabled) {
410417
return;
411418
}
412-
boolean webmvcPresent = ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet",
413-
getClass().getClassLoader());
414-
if (webmvcPresent) {
419+
if (webMvcPresent) {
415420
this.pc.getReaderContext()
416421
.registerWithGeneratedName(new RootBeanDefinition(OAuth2ClientWebMvcSecurityPostProcessor.class));
417422
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@
3636
*/
3737
public class CorsBeanDefinitionParser {
3838

39-
private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector";
40-
4139
private static final String ATT_SOURCE = "configuration-source-ref";
4240

4341
private static final String ATT_REF = "ref";
4442

43+
private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector";
44+
45+
private static final boolean mvcPresent;
46+
47+
static {
48+
mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR,
49+
CorsBeanDefinitionParser.class.getClassLoader());
50+
}
51+
4552
public BeanMetadataElement parse(Element element, ParserContext parserContext) {
4653
if (element == null) {
4754
return null;
@@ -64,7 +71,6 @@ public BeanMetadataElement getSource(Element element, ParserContext parserContex
6471
if (StringUtils.hasText(configurationSourceRef)) {
6572
return new RuntimeBeanReference(configurationSourceRef);
6673
}
67-
boolean mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR, getClass().getClassLoader());
6874
if (!mvcPresent) {
6975
return null;
7076
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ public class CsrfBeanDefinitionParser implements BeanDefinitionParser {
6666

6767
private static final String REQUEST_DATA_VALUE_PROCESSOR = "requestDataValueProcessor";
6868

69-
private static final String DISPATCHER_SERVLET_CLASS_NAME = "org.springframework.web.servlet.DispatcherServlet";
70-
7169
private static final String ATT_MATCHER = "request-matcher-ref";
7270

7371
private static final String ATT_REPOSITORY = "token-repository-ref";
7472

7573
private static final String ATT_REQUEST_HANDLER = "request-handler-ref";
7674

75+
private static final boolean webMvcPresent;
76+
77+
static {
78+
ClassLoader classLoader = CsrfBeanDefinitionParser.class.getClassLoader();
79+
webMvcPresent = ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", classLoader);
80+
}
81+
7782
private String csrfRepositoryRef;
7883

7984
private BeanDefinition csrfFilter;
@@ -90,8 +95,7 @@ public BeanDefinition parse(Element element, ParserContext pc) {
9095
if (disabled) {
9196
return null;
9297
}
93-
boolean webmvcPresent = ClassUtils.isPresent(DISPATCHER_SERVLET_CLASS_NAME, getClass().getClassLoader());
94-
if (webmvcPresent) {
98+
if (webMvcPresent) {
9599
if (!pc.getRegistry().containsBeanDefinition(REQUEST_DATA_VALUE_PROCESSOR)) {
96100
RootBeanDefinition beanDefinition = new RootBeanDefinition(CsrfRequestDataValueProcessor.class);
97101
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(beanDefinition,

config/src/main/java/org/springframework/security/config/ldap/EmbeddedLdapServerContextSourceFactoryBean.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class EmbeddedLdapServerContextSourceFactoryBean
4242

4343
private static final String UNBOUNDID_CLASSNAME = "com.unboundid.ldap.listener.InMemoryDirectoryServer";
4444

45+
private static final boolean unboundIdPresent;
46+
4547
private static final int DEFAULT_PORT = 33389;
4648

4749
private static final int RANDOM_PORT = 0;
@@ -60,6 +62,11 @@ public class EmbeddedLdapServerContextSourceFactoryBean
6062

6163
private EmbeddedLdapServerContainer container;
6264

65+
static {
66+
ClassLoader classLoader = EmbeddedLdapServerContextSourceFactoryBean.class.getClassLoader();
67+
unboundIdPresent = ClassUtils.isPresent(UNBOUNDID_CLASSNAME, classLoader);
68+
}
69+
6370
/**
6471
* Create an EmbeddedLdapServerContextSourceFactoryBean that will use an embedded LDAP
6572
* server to perform LDAP authentication. This requires a dependency on
@@ -120,7 +127,7 @@ public void setManagerPassword(String managerPassword) {
120127

121128
@Override
122129
public DefaultSpringSecurityContextSource getObject() throws Exception {
123-
if (!ClassUtils.isPresent(UNBOUNDID_CLASSNAME, getClass().getClassLoader())) {
130+
if (!unboundIdPresent) {
124131
throw new IllegalStateException("Embedded LDAP server is not provided");
125132
}
126133
this.container = getContainer();
@@ -156,7 +163,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
156163
}
157164

158165
private EmbeddedLdapServerContainer getContainer() {
159-
if (!ClassUtils.isPresent(UNBOUNDID_CLASSNAME, getClass().getClassLoader())) {
166+
if (!unboundIdPresent) {
160167
throw new IllegalStateException("Embedded LDAP server is not provided");
161168
}
162169
UnboundIdContainer unboundIdContainer = new UnboundIdContainer(this.root, this.ldif);

0 commit comments

Comments
 (0)