本文整理汇总了Java中java.security.Permission类的典型用法代码示例。如果您正苦于以下问题:Java Permission类的具体用法?Java Permission怎么用?Java Permission使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Permission类属于java.security包,在下文中一共展示了Permission类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findClass
import java.security.Permission; //导入依赖的package包/类
static Class<?> findClass(Module module, String cn, Permission perm) {
try {
Class<?> c = Class.forName(module, cn);
if (c == null) {
throw new RuntimeException(cn + " not found in " + module);
}
if (c.getModule() != module) {
throw new RuntimeException(c.getModule() + " != " + module);
}
return c;
} catch (AccessControlException e) {
if (e.getPermission().equals(perm))
return null;
throw e;
}
}
示例2: main
import java.security.Permission; //导入依赖的package包/类
public static void main(String[] args) throws JAXBException {
System.out.println("\nWithout security manager\n");
test(FactoryBase.class);
test(Factory1.class);
test(Factory2.class);
System.out.println("\nWith security manager\n");
Policy.setPolicy(new Policy() {
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
return true; // allow all
}
});
System.setSecurityManager(new SecurityManager());
test(FactoryBase.class);
test(Factory1.class);
test(Factory2.class);
}
示例3: checkSunClass
import java.security.Permission; //导入依赖的package包/类
/**
* Fix for 4179055: Need to assist resolving sun stubs; resolve
* class locally if it is a "permitted" sun class
*/
private Class<?> checkSunClass(String className, AccessControlException e)
throws AccessControlException
{
// ensure that we are giving out a stub for the correct reason
Permission perm = e.getPermission();
String name = null;
if (perm != null) {
name = perm.getName();
}
Class<?> resolvedClass = permittedSunClasses.get(className);
// if class not permitted, throw the SecurityException
if ((name == null) ||
(resolvedClass == null) ||
((!name.equals("accessClassInPackage.sun.rmi.server")) &&
(!name.equals("accessClassInPackage.sun.rmi.registry"))))
{
throw e;
}
return resolvedClass;
}
示例4: getPermissions
import java.security.Permission; //导入依赖的package包/类
/**
* Get the Permissions for a CodeSource. If this instance
* of WebappClassLoaderBase is for a web application context,
* add read FilePermission or JndiPermissions for the base
* directory (if unpacked),
* the context URL, and jar file resources.
*
* @param codeSource where the code was loaded from
* @return PermissionCollection for CodeSource
*/
@Override
protected PermissionCollection getPermissions(CodeSource codeSource) {
String codeUrl = codeSource.getLocation().toString();
PermissionCollection pc;
if ((pc = loaderPC.get(codeUrl)) == null) {
pc = super.getPermissions(codeSource);
if (pc != null) {
Iterator<Permission> perms = permissionList.iterator();
while (perms.hasNext()) {
Permission p = perms.next();
pc.add(p);
}
loaderPC.put(codeUrl,pc);
}
}
return (pc);
}
示例5: test
import java.security.Permission; //导入依赖的package包/类
public static void test(Permission perm, boolean expectException) {
boolean getException = (Boolean) AccessController.doPrivileged((PrivilegedAction) () -> {
try {
AccessController.checkPermission(perm);
return (Boolean) false;
} catch (AccessControlException ex) {
return (Boolean) true;
}
});
if (expectException ^ getException) {
String message = "Check Permission :" + perm + "\n ExpectException = "
+ expectException + "\n getException = " + getException;
throw new RuntimeException(message);
}
}
示例6: checkMBeanTrustPermission
import java.security.Permission; //导入依赖的package包/类
private static void checkMBeanTrustPermission(final Class<?> theClass)
throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
Permission perm = new MBeanTrustPermission("register");
PrivilegedAction<ProtectionDomain> act =
new PrivilegedAction<ProtectionDomain>() {
public ProtectionDomain run() {
return theClass.getProtectionDomain();
}
};
ProtectionDomain pd = AccessController.doPrivileged(act);
AccessControlContext acc =
new AccessControlContext(new ProtectionDomain[] { pd });
sm.checkPermission(perm, acc);
}
}
示例7: checkRemoveCallerContext
import java.security.Permission; //导入依赖的package包/类
/**
* Check if the connector server creator can assume the identity of each
* principal in the authenticated subject, i.e. check if the connector
* server creator codebase contains a subject delegation permission for
* each principal present in the authenticated subject.
*
* @return {@code true} if the connector server creator can delegate to all
* the authenticated principals in the subject. Otherwise, {@code false}.
*/
public static synchronized boolean
checkRemoveCallerContext(Subject subject) {
try {
for (Principal p : getSubjectPrincipals(subject)) {
final String pname =
p.getClass().getName() + "." + p.getName();
final Permission sdp =
new SubjectDelegationPermission(pname);
AccessController.checkPermission(sdp);
}
} catch (SecurityException e) {
return false;
}
return true;
}
示例8: checkPermission
import java.security.Permission; //导入依赖的package包/类
private void checkPermission(Permission perm, boolean expectException) {
boolean getException = (Boolean) AccessController
.doPrivileged((PrivilegedAction) () -> {
try {
AccessController.checkPermission(perm);
return (Boolean) false;
} catch (AccessControlException ex) {
return (Boolean) true;
}
});
if (expectException ^ getException) {
String message = "Check Permission :" + perm + "\n ExpectException = "
+ expectException + "\n getException = " + getException;
throw new RuntimeException(message);
}
}
示例9: checkPermission
import java.security.Permission; //导入依赖的package包/类
/**
* {@inheritDoc}
*
* @see java.lang.SecurityManager#checkPermission(java.security.Permission)
*/
@Override
public void checkPermission ( Permission perm ) {
if ( perm instanceof RuntimePermission ) {
return;
}
Set<URL> cbs = new HashSet<>();
for ( Class<?> cl : getClassContext() ) {
if ( cl.getProtectionDomain() != null && cl.getProtectionDomain().getCodeSource() != null
&& cl.getProtectionDomain().getCodeSource().getLocation() != null
&& !"file".equals(cl.getProtectionDomain().getCodeSource().getLocation().getProtocol()) ) {
cbs.add(cl.getProtectionDomain().getCodeSource().getLocation());
}
}
this.remoteCodebases.addAll(cbs);
}
示例10: implies
import java.security.Permission; //导入依赖的package包/类
/**
* Check and see if this collection of permissions implies the permissions
* expressed in "permission".
*
* @param permission the Permission object to compare
*
* @return true if "permission" is a proper subset of a permission in
* the collection, false if not.
*/
public boolean implies(Permission permission)
{
if (! (permission instanceof SocketPermission))
return false;
SocketPermission np = (SocketPermission) permission;
int desired = np.getMask();
int effective = 0;
int needed = desired;
synchronized (this) {
int len = perms.size();
//System.out.println("implies "+np);
for (int i = 0; i < len; i++) {
SocketPermission x = perms.get(i);
//System.out.println(" trying "+x);
if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
effective |= x.getMask();
if ((effective & desired) == desired)
return true;
needed = (desired ^ effective);
}
}
}
return false;
}
示例11: getPermission
import java.security.Permission; //导入依赖的package包/类
@Override public Permission getPermission() throws IOException {
URL url = getURL();
String hostname = url.getHost();
int hostPort = url.getPort() != -1
? url.getPort()
: HttpUrl.defaultPort(url.getProtocol());
if (usingProxy()) {
InetSocketAddress proxyAddress = (InetSocketAddress) client.proxy().address();
hostname = proxyAddress.getHostName();
hostPort = proxyAddress.getPort();
}
return new SocketPermission(hostname + ":" + hostPort, "connect, resolve");
}
示例12: getClassLoader
import java.security.Permission; //导入依赖的package包/类
public final ClassLoader getClassLoader(ObjectName name) {
ClassLoader instance = loadersWithNames.get(name);
if (instance != null) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
Permission perm =
new MBeanPermission(instance.getClass().getName(),
null,
name,
"getClassLoader");
sm.checkPermission(perm);
}
}
return instance;
}
示例13: elements
import java.security.Permission; //导入依赖的package包/类
public java.util.Enumeration<Permission> elements() {
if (perms == null)
init();
synchronized (perms) {
return perms.elements();
}
}
示例14: getConnectPermission
import java.security.Permission; //导入依赖的package包/类
public static Permission getConnectPermission(URL url) throws IOException {
String urlStringLowerCase = url.toString().toLowerCase();
if (urlStringLowerCase.startsWith("http:") || urlStringLowerCase.startsWith("https:")) {
return getURLConnectPermission(url);
} else if (urlStringLowerCase.startsWith("jar:http:") || urlStringLowerCase.startsWith("jar:https:")) {
String urlString = url.toString();
int bangPos = urlString.indexOf("!/");
urlString = urlString.substring(4, bangPos > -1 ? bangPos : urlString.length());
URL u = new URL(urlString);
return getURLConnectPermission(u);
// If protocol is HTTP or HTTPS than use URLPermission object
} else {
return url.openConnection().getPermission();
}
}
示例15: checkPermission
import java.security.Permission; //导入依赖的package包/类
private static void checkPermission(String action)
throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
Permission perm = new MBeanServerPermission(action);
sm.checkPermission(perm);
}
}