Skip to content

Commit 52d050d

Browse files
committed
Factored out "suppressGetenvAccess()" method
Issue: SPR-11297
1 parent c1569d7 commit 52d050d

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
6060
* resolvable otherwise. Consider switching this flag to "true" if you experience
6161
* log warnings from {@code getenv} calls coming from Spring, e.g. on WebSphere
6262
* with strict SecurityManager settings and AccessControlExceptions warnings.
63+
* @see #suppressGetenvAccess()
6364
*/
6465
public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore";
6566

@@ -359,17 +360,9 @@ public MutablePropertySources getPropertySources() {
359360

360361
@SuppressWarnings("unchecked")
361362
public Map<String, Object> getSystemEnvironment() {
362-
try {
363-
if ("true".equalsIgnoreCase(System.getProperty(IGNORE_GETENV_PROPERTY_NAME))) {
364-
return Collections.emptyMap();
365-
}
366-
}
367-
catch (Throwable ex) {
368-
if (logger.isDebugEnabled()) {
369-
logger.debug("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex);
370-
}
363+
if (suppressGetenvAccess()) {
364+
return Collections.emptyMap();
371365
}
372-
373366
try {
374367
return (Map) System.getenv();
375368
}
@@ -393,6 +386,27 @@ protected String getSystemAttribute(String variableName) {
393386
}
394387
}
395388

389+
/**
390+
* Determine whether to suppress {@link System#getenv()}/{@link System#getenv(String)}
391+
* access for the purposes of {@link #getSystemEnvironment()}.
392+
* <p>If this method returns {@code true}, an empty dummy Map will be used instead
393+
* of the regular system environment Map, never even trying to call {@code getenv}
394+
* and therefore avoiding security manager warnings (if any).
395+
* <p>The default implementation checks for the "spring.getenv.ignore" system property,
396+
* returning {@code true} if its value equals "true" in any case.
397+
*/
398+
protected boolean suppressGetenvAccess() {
399+
try {
400+
return "true".equalsIgnoreCase(System.getProperty(IGNORE_GETENV_PROPERTY_NAME));
401+
}
402+
catch (Throwable ex) {
403+
if (logger.isDebugEnabled()) {
404+
logger.debug("Could not obtain system property '" + IGNORE_GETENV_PROPERTY_NAME + "': " + ex);
405+
}
406+
return false;
407+
}
408+
}
409+
396410
@SuppressWarnings("unchecked")
397411
public Map<String, Object> getSystemProperties() {
398412
try {

0 commit comments

Comments
 (0)