Skip to content

Commit 4ff03f3

Browse files
committed
chore: use SecurityManager only for JDKs up to 24
1 parent 93ac65d commit 4ff03f3

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

modules/kernel/src/org/apache/axis2/java/security/AccessController.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* accessing system resources (ie, read/write files, opening ports, and etc).
3636
* <p/>
3737
* This class provides a consistent security model across Java versions by
38-
* always using doPrivileged(), ensuring proper privilege elevation regardless
38+
* always using doPrivileged(), if it is available, ensuring proper privilege elevation regardless
3939
* of SecurityManager presence (which was deprecated in Java 17 and removed in Java 21).
4040
* <p/>
4141
* Note: This utility should be used properly, otherwise might introduce
@@ -56,14 +56,13 @@
5656
* }
5757
* </code>
5858
*/
59-
60-
6159
public class AccessController {
60+
private static final boolean SUPPORTS_SECURITY_MANAGER = Runtime.version().feature() < 24;
6261

6362
/**
6463
* Performs the specified <code>PrivilegedAction</code> with privileges
6564
* enabled. This method always uses doPrivileged for security consistency
66-
* across Java versions.
65+
* across Java versions, if it is available.
6766
* <p/>
6867
* If the action's <code>run</code> method throws an (unchecked) exception,
6968
* it will propagate through this method.
@@ -74,7 +73,11 @@ public class AccessController {
7473
* @see #doPrivileged(PrivilegedExceptionAction)
7574
*/
7675
public static <T> T doPrivileged(PrivilegedAction<T> action) {
77-
return java.security.AccessController.doPrivileged(action);
76+
if (!SUPPORTS_SECURITY_MANAGER) {
77+
return (action.run());
78+
} else {
79+
return java.security.AccessController.doPrivileged(action);
80+
}
7881
}
7982

8083

@@ -97,7 +100,11 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
97100
* @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
98101
*/
99102
public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContext context) {
100-
return java.security.AccessController.doPrivileged(action, context);
103+
if (!SUPPORTS_SECURITY_MANAGER) {
104+
return action.run();
105+
} else {
106+
return java.security.AccessController.doPrivileged(action, context);
107+
}
101108
}
102109

103110
/**
@@ -117,7 +124,17 @@ public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContex
117124
*/
118125
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
119126
throws PrivilegedActionException {
120-
return java.security.AccessController.doPrivileged(action);
127+
if (!SUPPORTS_SECURITY_MANAGER) {
128+
try {
129+
return action.run();
130+
} catch (java.lang.RuntimeException e) {
131+
throw e;
132+
} catch (Exception e) {
133+
throw new PrivilegedActionException(e);
134+
}
135+
} else {
136+
return java.security.AccessController.doPrivileged(action);
137+
}
121138
}
122139

123140

@@ -146,7 +163,18 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
146163
public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
147164
AccessControlContext context)
148165
throws PrivilegedActionException {
149-
return java.security.AccessController.doPrivileged(action, context);
166+
167+
if (!SUPPORTS_SECURITY_MANAGER) {
168+
try {
169+
return action.run();
170+
} catch (java.lang.RuntimeException e) {
171+
throw e;
172+
} catch (Exception e) {
173+
throw new PrivilegedActionException(e);
174+
}
175+
} else {
176+
return java.security.AccessController.doPrivileged(action, context);
177+
}
150178
}
151179

152180
/**
@@ -174,7 +202,9 @@ public static AccessControlContext getContext() {
174202
* is not permitted, based on the current security policy.
175203
*/
176204
public static void checkPermission(Permission perm) throws AccessControlException {
177-
java.security.AccessController.checkPermission(perm);
205+
if (SUPPORTS_SECURITY_MANAGER) {
206+
java.security.AccessController.checkPermission(perm);
207+
}
178208
}
179209

180210
/**

modules/kernel/test/org/apache/axis2/java/security/driver/Java2SecTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.util.Calendar;
3939
import java.util.TimeZone;
4040

41+
import static org.junit.Assume.assumeTrue;
42+
4143
/**
4244
* Java2SecTest demonstrates the usages of AccessController class for privileged operations.
4345
*
@@ -69,6 +71,12 @@ public Java2SecTest() {
6971
System.out.println("Current time => " + sdf.format(cal.getTime()) + "\n");
7072
}
7173

74+
@Override
75+
public void setUp() throws Exception {
76+
// Security Manager was removed after that
77+
assumeTrue(Runtime.version().feature() < 24);
78+
}
79+
7280
// Constructor
7381
public Java2SecTest(String arg) {
7482
super(arg);

0 commit comments

Comments
 (0)