Skip to content

Commit 0437544

Browse files
authored
JCR-5121: Java 23: getSubject is supported only if a security manager… (#222)
* JCR-5121: Java 23: getSubject is supported only if a security manager is allowed - borrowed fix from OAK-11199 * JCR-5121: Java 23: getSubject is supported only if a security manager is allowed - borrowed fix from OAK-11199 * JCR-5121: Java 23: getSubject is supported only if a security manager is allowed - borrowed fix from OAK-11199 - remove unused 'callAs'
1 parent 9f5e1c7 commit 0437544

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.io.OutputStream;
2323
import java.io.StringReader;
2424
import java.nio.charset.StandardCharsets;
25-
import java.security.AccessControlContext;
26-
import java.security.AccessController;
2725
import java.util.ArrayList;
2826
import java.util.Arrays;
2927
import java.util.HashMap;
@@ -80,6 +78,7 @@
8078
import org.apache.jackrabbit.core.gc.GarbageCollector;
8179
import org.apache.jackrabbit.core.id.NodeId;
8280
import org.apache.jackrabbit.core.id.NodeIdFactory;
81+
import org.apache.jackrabbit.core.jdkcompat.Java23Subject;
8382
import org.apache.jackrabbit.core.lock.LockManager;
8483
import org.apache.jackrabbit.core.lock.LockManagerImpl;
8584
import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
@@ -1025,8 +1024,7 @@ private Session extendAuthentication(String workspaceName)
10251024

10261025
Subject subject = null;
10271026
try {
1028-
AccessControlContext acc = AccessController.getContext();
1029-
subject = Subject.getSubject(acc);
1027+
subject = Java23Subject.getSubject();
10301028
} catch (SecurityException e) {
10311029
log.warn("Can't check for preauthentication. Reason: {}", e.getMessage());
10321030
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.jackrabbit.core.jdkcompat;
18+
19+
import javax.security.auth.Subject;
20+
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Method;
22+
import java.security.AccessControlContext;
23+
import java.security.AccessController;
24+
import java.util.concurrent.Callable;
25+
26+
/**
27+
* This class contains methods replacing the deprecated
28+
* {@link Subject#getSubject(AccessControlContext)}
29+
* and associated methods, which changed their behavior
30+
* with Java 23 (@see https://inside.java/2024/07/08/quality-heads-up).
31+
* <p>
32+
* Subset borrowed from org.apache.jackrabbit.oak.commons.jdkcompat
33+
* (see JCR-5121 and OAK-11199).
34+
*/
35+
public class Java23Subject {
36+
37+
static Method current;
38+
39+
static {
40+
try {
41+
current = Subject.class.getMethod("current");
42+
} catch (NoSuchMethodException ignored) {}
43+
}
44+
45+
public static Subject getSubject() {
46+
Subject result;
47+
if (current != null) {
48+
try {
49+
result = (Subject) current.invoke(null);
50+
} catch (InvocationTargetException | IllegalAccessException e) {
51+
throw new SecurityException(e);
52+
}
53+
} else {
54+
result = Subject.getSubject(AccessController.getContext());
55+
}
56+
return result;
57+
}
58+
}

0 commit comments

Comments
 (0)