本文整理匯總了Java中javax.security.auth.Subject.doAsPrivileged方法的典型用法代碼示例。如果您正苦於以下問題:Java Subject.doAsPrivileged方法的具體用法?Java Subject.doAsPrivileged怎麽用?Java Subject.doAsPrivileged使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.security.auth.Subject
的用法示例。
在下文中一共展示了Subject.doAsPrivileged方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import javax.security.auth.Subject; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
for (Permission perm : perms) {
AccessController.checkPermission(perm);
}
Permission princPerm = new java.util.PropertyPermission("user.home",
"read");
Set<Principal> princSet = new HashSet<>(Arrays.asList(princs));
Subject subject = new Subject(true, princSet, Collections.emptySet(),
Collections.emptySet());
PrivilegedAction<Void> pa = () -> {
AccessController.checkPermission(princPerm);
return null;
};
Subject.doAsPrivileged(subject, pa, null);
}
示例2: main
import javax.security.auth.Subject; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Subject s = new Subject();
s.getPrincipals().add
(new javax.security.auth.x500.X500Principal("CN=test"));
s.getPrivateCredentials().add(new String("test"));
try {
Subject.doAsPrivileged(s, new PrivilegedAction() {
public Object run() {
java.util.Iterator i = Subject.getSubject
(AccessController.getContext
()).getPrivateCredentials().iterator();
return i.next();
}
}, null);
System.out.println("Test succeeded");
} catch (Exception e) {
System.out.println("Test failed");
e.printStackTrace();
throw e;
}
}
示例3: main
import javax.security.auth.Subject; //導入方法依賴的package包/類
public static void main(String[] args) {
System.setSecurityManager(new SecurityManager());
try {
Subject.doAsPrivileged(get("CN=joe"), new PrivilegedAction() {
public Object run() {
return Subject.doAs(null, new PrivilegedAction() {
public Object run() {
return System.getProperty("foobar");
}
});
}
}, null);
throw new RuntimeException
("Access control exception should have occcured");
} catch (java.security.AccessControlException e) {
// Expected exception occured
}
}
示例4: main
import javax.security.auth.Subject; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
// try setting the local hostname
InetAddress localHost = InetAddress.getLocalHost();
if (localHost.isLoopbackAddress()) {
System.err.println("Local host name is resolved into a loopback address. Quit now!");
return;
}
System.setProperty("host.name", localHost.
getHostName());
String policyFileName = System.getProperty("test.src", ".") +
"/" + "policy.file";
System.setProperty("java.security.policy", policyFileName);
System.setSecurityManager(new SecurityManager());
InetAddress localHost1 = null;
InetAddress localHost2 = null;
localHost1 = InetAddress.getLocalHost();
Subject mySubject = new Subject();
MyPrincipal userPrincipal = new MyPrincipal("test");
mySubject.getPrincipals().add(userPrincipal);
localHost2 = (InetAddress)Subject.doAsPrivileged(mySubject,
new MyAction(), null);
if (localHost1.equals(localHost2)) {
System.out.println("localHost1 = " + localHost1);
throw new RuntimeException("InetAddress.getLocalHost() test " +
" fails. localHost2 should be " +
" the real address instead of " +
" the loopback address."+localHost2);
}
}
示例5: run
import javax.security.auth.Subject; //導入方法依賴的package包/類
@Override public Void run() {
Set<Principal> principals = new HashSet<>();
Set<Object> publicCredentials = new HashSet<>();
Set<Object> privateCredentials = new HashSet<>();
principals.add(principal);
Subject subject = new Subject(true,
principals,
publicCredentials,
privateCredentials);
Subject.doAsPrivileged(subject, action, null);
return null;
}
示例6: main
import javax.security.auth.Subject; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
String foo = Subject.doAs(new Subject(),
new A1<String>("foo"));
Integer one = Subject.doAs(new Subject(),
new A2<Integer>(new Integer("1")));
Boolean troo = Subject.doAsPrivileged(new Subject(),
new A1<Boolean>(new Boolean("true")),
AccessController.getContext());
Generic gen = Subject.doAsPrivileged(new Subject(),
new A2<Generic>(new Generic()),
AccessController.getContext());
}