本文整理汇总了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());
}