java.lang.System.setSecurityManager()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(387)

本文整理了Java中java.lang.System.setSecurityManager()方法的一些代码示例,展示了System.setSecurityManager()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。System.setSecurityManager()方法的具体详情如下:
包路径:java.lang.System
类名称:System
方法名:setSecurityManager

System.setSecurityManager介绍

[英]Throws SecurityException.

Security managers do not provide a secure environment for executing untrusted code and are unsupported on Android. Untrusted code cannot be safely isolated within a single VM on Android, so this method always throws a SecurityException.
[中]抛出SecurityException。
安全管理器不提供执行不受信任代码的安全环境,在Android上不受支持。不受信任的代码无法在Android上的单个VM中安全隔离,因此此方法始终引发SecurityException。

代码示例

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. protected void after()
  3. {
  4. System.setSecurityManager( originalSecurityManager );
  5. }

代码示例来源:origin: google/guava

  1. public void testExistsThrowsSecurityException() throws IOException, URISyntaxException {
  2. SecurityManager oldSecurityManager = System.getSecurityManager();
  3. try {
  4. doTestExistsThrowsSecurityException();
  5. } finally {
  6. System.setSecurityManager(oldSecurityManager);
  7. }
  8. }

代码示例来源:origin: spring-projects/spring-framework

  1. @After
  2. public void tearDown() {
  3. env.remove(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
  4. System.setSecurityManager(originalSecurityManager);
  5. }

代码示例来源:origin: org.apache.logging.log4j/log4j-api

  1. private void before() {
  2. securityManagerBefore = System.getSecurityManager();
  3. System.setSecurityManager(securityManager);
  4. }
  5. };

代码示例来源:origin: google/guava

  1. /**
  2. * Tests that the use of a {@link FinalizableReferenceQueue} does not subsequently prevent the
  3. * loader of that class from being garbage-collected.
  4. */
  5. public void testUnloadableWithoutSecurityManager() throws Exception {
  6. if (isJdk9OrHigher()) {
  7. return;
  8. }
  9. SecurityManager oldSecurityManager = System.getSecurityManager();
  10. try {
  11. System.setSecurityManager(null);
  12. doTestUnloadable();
  13. } finally {
  14. System.setSecurityManager(oldSecurityManager);
  15. }
  16. }

代码示例来源:origin: spring-projects/spring-framework

  1. public CallbacksSecurityTests() {
  2. // setup security
  3. if (System.getSecurityManager() == null) {
  4. Policy policy = Policy.getPolicy();
  5. URL policyURL = getClass()
  6. .getResource(
  7. "/org/springframework/beans/factory/support/security/policy.all");
  8. System.setProperty("java.security.policy", policyURL.toString());
  9. System.setProperty("policy.allowSystemProperty", "true");
  10. policy.refresh();
  11. System.setSecurityManager(new SecurityManager());
  12. }
  13. }

代码示例来源:origin: neo4j/neo4j

  1. @Override
  2. protected void before()
  3. {
  4. originalSecurityManager = System.getSecurityManager();
  5. TestSecurityManager testSecurityManager = new TestSecurityManager( originalSecurityManager );
  6. System.setSecurityManager( testSecurityManager );
  7. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. @After
  2. public void tearDown() {
  3. System.setSecurityManager( oldSecurityManager );
  4. sysOutContent = null;
  5. sysErrContent = null;
  6. mockRepositoriesMeta = null;
  7. mockRepositoryMeta = null;
  8. mockRepository = null;
  9. mockRepositoryDirectory = null;
  10. }

代码示例来源:origin: google/guava

  1. System.setSecurityManager(
  2. new SecurityManager() {
  3. @Override
  4. assertEquals(oldName, Thread.currentThread().getName());
  5. } finally {
  6. System.setSecurityManager(null);

代码示例来源:origin: pentaho/pentaho-kettle

  1. @Before
  2. public void setUp() throws KettleException {
  3. KettleEnvironment.init();
  4. oldSecurityManager = System.getSecurityManager();
  5. sysOutContent = new ByteArrayOutputStream();
  6. sysErrContent = new ByteArrayOutputStream();
  7. System.setSecurityManager( new MySecurityManager( oldSecurityManager ) );
  8. mockRepositoriesMeta = mock( RepositoriesMeta.class );
  9. mockRepositoryMeta = mock( RepositoryMeta.class );
  10. mockRepository = mock( Repository.class );
  11. mockRepositoryDirectory = mock( RepositoryDirectoryInterface.class );
  12. }

代码示例来源:origin: languagetool-org/languagetool

  1. @Test
  2. public void testPermissionManager() throws Exception {
  3. try {
  4. PatternRuleLoader loader = new PatternRuleLoader();
  5. // do not crash if Authenticator.setDefault() is forbidden,
  6. // see https://github.com/languagetool-org/languagetool/issues/255
  7. loader.getRules(new ByteArrayInputStream("<rules lang='xx'></rules>".getBytes("utf-8")), "fakeName");
  8. } finally {
  9. System.setSecurityManager(null);
  10. }
  11. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void systemPropertiesSecurityManager() {
  3. AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
  4. GenericBeanDefinition bd = new GenericBeanDefinition();
  5. bd.setBeanClass(TestBean.class);
  6. bd.getPropertyValues().add("country", "#{systemProperties.country}");
  7. ac.registerBeanDefinition("tb", bd);
  8. SecurityManager oldSecurityManager = System.getSecurityManager();
  9. try {
  10. System.setProperty("country", "NL");
  11. SecurityManager securityManager = new SecurityManager() {
  12. @Override
  13. public void checkPropertiesAccess() {
  14. throw new AccessControlException("Not Allowed");
  15. }
  16. @Override
  17. public void checkPermission(Permission perm) {
  18. // allow everything else
  19. }
  20. };
  21. System.setSecurityManager(securityManager);
  22. ac.refresh();
  23. TestBean tb = ac.getBean("tb", TestBean.class);
  24. assertEquals("NL", tb.getCountry());
  25. }
  26. finally {
  27. System.setSecurityManager(oldSecurityManager);
  28. System.getProperties().remove("country");
  29. }
  30. }

代码示例来源:origin: google/guava

  1. public void testUnloadableInStaticFieldIfClosed() throws Exception {
  2. if (isJdk9OrHigher()) {
  3. return;
  4. }
  5. Policy oldPolicy = Policy.getPolicy();
  6. SecurityManager oldSecurityManager = System.getSecurityManager();
  7. try {
  8. Policy.setPolicy(new PermissivePolicy());
  9. System.setSecurityManager(new SecurityManager());
  10. WeakReference<ClassLoader> loaderRef = doTestUnloadableInStaticFieldIfClosed();
  11. GcFinalization.awaitClear(loaderRef);
  12. } finally {
  13. System.setSecurityManager(oldSecurityManager);
  14. Policy.setPolicy(oldPolicy);
  15. }
  16. }

代码示例来源:origin: spring-projects/spring-framework

  1. System.setSecurityManager(securityManager);
  2. System.setSecurityManager(oldSecurityManager);
  3. getModifiableSystemEnvironment().remove(ALLOWED_PROPERTY_NAME);
  4. getModifiableSystemEnvironment().remove(DISALLOWED_PROPERTY_NAME);

代码示例来源:origin: google/guava

  1. /**
  2. * Tests that the use of a {@link FinalizableReferenceQueue} does not subsequently prevent the
  3. * loader of that class from being garbage-collected even if there is a {@link SecurityManager}.
  4. * The {@link SecurityManager} environment makes such leaks more likely because when you create a
  5. * {@link URLClassLoader} with a {@link SecurityManager}, the creating code's {@link
  6. * java.security.AccessControlContext} is captured, and that references the creating code's {@link
  7. * ClassLoader}.
  8. */
  9. public void testUnloadableWithSecurityManager() throws Exception {
  10. if (isJdk9OrHigher()) {
  11. return;
  12. }
  13. Policy oldPolicy = Policy.getPolicy();
  14. SecurityManager oldSecurityManager = System.getSecurityManager();
  15. try {
  16. Policy.setPolicy(new PermissivePolicy());
  17. System.setSecurityManager(new SecurityManager());
  18. doTestUnloadable();
  19. } finally {
  20. System.setSecurityManager(oldSecurityManager);
  21. Policy.setPolicy(oldPolicy);
  22. }
  23. }

代码示例来源:origin: google/guava

  1. System.setSecurityManager(disallowFilesSecurityManager);
  2. try {
  3. file.exists();

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void securityManagerDisallowsAccessToSystemEnvironmentButAllowsAccessToIndividualKeys() {
  3. SecurityManager securityManager = new SecurityManager() {
  4. @Override
  5. public void checkPermission(Permission perm) {
  6. // Disallowing access to System#getenv means that our
  7. // ReadOnlySystemAttributesMap will come into play.
  8. if ("getenv.*".equals(perm.getName())) {
  9. throw new AccessControlException("Accessing the system environment is disallowed");
  10. }
  11. }
  12. };
  13. System.setSecurityManager(securityManager);
  14. DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  15. AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf);
  16. reader.register(C1.class);
  17. assertThat(bf.containsBean("c1"), is(true));
  18. }

代码示例来源:origin: google/guava

  1. /**
  2. * Runs Runnable r with a security policy that permits precisely the specified permissions. If
  3. * there is no current security manager, the runnable is run twice, both with and without a
  4. * security manager. We require that any security manager permit getPolicy/setPolicy.
  5. */
  6. public void runWithPermissions(Runnable r, Permission... permissions) {
  7. SecurityManager sm = System.getSecurityManager();
  8. if (sm == null) {
  9. r.run();
  10. Policy savedPolicy = Policy.getPolicy();
  11. try {
  12. Policy.setPolicy(permissivePolicy());
  13. System.setSecurityManager(new SecurityManager());
  14. runWithPermissions(r, permissions);
  15. } finally {
  16. System.setSecurityManager(null);
  17. Policy.setPolicy(savedPolicy);
  18. }
  19. } else {
  20. Policy savedPolicy = Policy.getPolicy();
  21. AdjustablePolicy policy = new AdjustablePolicy(permissions);
  22. Policy.setPolicy(policy);
  23. try {
  24. r.run();
  25. } finally {
  26. policy.addPermission(new SecurityPermission("setPolicy"));
  27. Policy.setPolicy(savedPolicy);
  28. }
  29. }
  30. }

代码示例来源:origin: spring-projects/spring-framework

  1. @Test
  2. public void securityManagerDisallowsAccessToSystemEnvironmentAndDisallowsAccessToIndividualKey() {
  3. SecurityManager securityManager = new SecurityManager() {
  4. @Override
  5. public void checkPermission(Permission perm) {
  6. // Disallowing access to System#getenv means that our
  7. // ReadOnlySystemAttributesMap will come into play.
  8. if ("getenv.*".equals(perm.getName())) {
  9. throw new AccessControlException("Accessing the system environment is disallowed");
  10. }
  11. // Disallowing access to the spring.profiles.active property means that
  12. // the BeanDefinitionReader won't be able to determine which profiles are
  13. // active. We should see an INFO-level message in the console about this
  14. // and as a result, any components marked with a non-default profile will
  15. // be ignored.
  16. if (("getenv." + AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME).equals(perm.getName())) {
  17. throw new AccessControlException(
  18. format("Accessing system environment variable [%s] is disallowed",
  19. AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
  20. }
  21. }
  22. };
  23. System.setSecurityManager(securityManager);
  24. DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
  25. AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(bf);
  26. reader.register(C1.class);
  27. assertThat(bf.containsBean("c1"), is(false));
  28. }

代码示例来源:origin: languagetool-org/languagetool

  1. @BeforeClass
  2. public static void startup() throws Exception {
  3. Policy.setPolicy(new MyPolicy());
  4. System.setSecurityManager(new SecurityManager());
  5. }

相关文章