public class PrivateConstructorPermissionKlass {
private PrivateConstructorPermissionKlass() {
AccessControlContext acc = AccessController.getContext();
acc.checkPermission(new RuntimePermission("privateConstructorPermission"));
}
}
public class PrivateConstructorPermissionKlassTest {
@Test
public void test() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<PrivateConstructorPermissionKlass> constructor = PrivateConstructorPermissionKlass.class.getDeclaredConstructor(new Class<?>[0]);
constructor.setAccessible(true);
PrivateConstructorPermissionKlass instance3 = constructor.newInstance(new Object[0]);
Assert.assertNotNull(instance3);
}
}
默认情况下,运行该程序将会出现错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.java.PrivateConstructorPermissionKlassTest.test(PrivateConstructorPermissionKlassTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission privateConstructorPermission)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at com.java.PrivateConstructorPermissionKlass.<init>(PrivateConstructorPermissionKlass.java:10)
... 28 more
在上面的错误信息, 应该是没有java.lang.RuntimePermission privateConstructorPermission访问权限,打开jre\lib\security\java.policy, 在最后添加:
permission java.lang.RuntimePermission "privateConstructorPermission";
运行后结果如下:
ok
相关推荐
此外,Java还提供了安全管理API,例如java.security包下的类和接口,如Policy、Permission和Guard。这些API允许开发者实现更精细的控制,比如定义特定的权限对象来限制代码的行为。 在网络通信方面,Java的SSL/TLS...
2. `Permission`类:表示一个安全权限,如`FilePermission`用于控制文件访问,`SocketPermission`用于网络连接。 3. `AccessController`类:负责执行上下文敏感的安全检查,根据当前运行的代码的权限来决定是否允许...
它提供了丰富的特性和高可用性,如RAC(Real Application Clusters,实时应用集群)、Data Guard、Materialized Views等,确保了数据的安全性和可靠性。在SSI框架中,Oracle通常作为后端的数据存储,通过JDBC接口与...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 然后,在应用代码中,可以创建一个LocationManager对象,并请求GPS提供器: ```java LocationManager locationManager = ...
<uses-permission android:name="android.permission.CAMERA" /> ``` 1. 使用Camera API(适用于Android 2.3及以上版本): ```java Camera camera = Camera.open(); Parameters parameters = camera.getParameters...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 这将允许应用访问设备的地理位置,因为加速度传感器的数据可能会与GPS数据一起使用。 2. 在Activity或Service中初始化...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 在iOS的Info.plist文件中,添加以下键值对: ```xml <key>NSLocationWhenInUseUsageDescription 需要您的位置信息以提供服务。 ``...
1. **权限问题**:在AndroidManifest.xml中,确保添加了相机访问权限`<uses-permission android:name="android.permission.CAMERA" />`。 2. **编译错误**:检查依赖库版本是否与项目兼容,确保所有库都已经更新到...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 然后,在代码中初始化`FusedLocationProviderClient`并请求位置更新: ```java FusedLocationProviderClient ...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` 然后,创建一个`SensorEventListener`监听器,用于监听加速度传感器(Accelerometer)的变化。在`onSensorChanged`方法中,通过...