Merge remote-tracking branch 'upstream/master' into BAEL-20573

This commit is contained in:
Krzysztof Woyke 2020-01-03 09:21:03 +01:00
commit da78ae7cbe
10 changed files with 44 additions and 58 deletions

View File

@ -1,3 +0,0 @@
### Relevant Articles:
- [Intro to the Java SecurityManager](https://www.baeldung.com/java-security-manager)

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-security-manager</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-security-manager</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
</project>

View File

@ -1,35 +0,0 @@
package com.baeldung.security.manager;
import org.junit.Test;
import java.net.URL;
import java.security.AccessControlException;
import java.util.concurrent.Callable;
public class SecurityManagerUnitTest {
@Test(expected = AccessControlException.class)
public void whenSecurityManagerIsActive_thenNetworkIsNotAccessibleByDefault() throws Exception {
doTest(() -> {
new URL("http://www.google.com").openConnection().connect();
return null;
});
}
@Test(expected = AccessControlException.class)
public void whenUnauthorizedClassTriesToAccessProtectedOperation_thenAnExceptionIsThrown() throws Exception {
doTest(() -> {
new Service().operation();
return null;
});
}
private void doTest(Callable<Void> action) throws Exception {
System.setSecurityManager(new SecurityManager());
try {
action.call();
} finally {
System.setSecurityManager(null);
}
}
}

View File

@ -15,4 +15,5 @@ This module contains articles about core Java Security
- [The Java SecureRandom Class](https://www.baeldung.com/java-secure-random)
- [An Introduction to Java SASL](https://www.baeldung.com/java-sasl)
- [A Guide to Java GSS API](https://www.baeldung.com/java-gss)
- [Intro to the Java SecurityManager](https://www.baeldung.com/java-security-manager)

View File

@ -1,4 +1,4 @@
package com.baeldung.security.manager;
package com.baeldung.securitymanager;
import java.security.BasicPermission;

View File

@ -1,4 +1,4 @@
package org.baeldung.java.md5;
package com.baeldung.java.md5;
import static org.assertj.core.api.Assertions.assertThat;

View File

@ -0,0 +1,35 @@
package com.baeldung.securitymanager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import java.security.AccessControlException;
public class SecurityManagerUnitTest {
private static final String TESTING_SECURITY_POLICY = "file:src/test/resources/testing.policy";
@Before
public void setUp() {
System.setProperty("java.security.policy", TESTING_SECURITY_POLICY);
System.setSecurityManager(new SecurityManager());
}
@After
public void tearDown() {
System.setSecurityManager(null);
}
@Test(expected = AccessControlException.class)
public void whenSecurityManagerIsActive_thenNetworkIsNotAccessibleByDefault() throws IOException {
new URL("http://www.google.com").openConnection().connect();
}
@Test(expected = AccessControlException.class)
public void whenUnauthorizedClassTriesToAccessProtectedOperation_thenAnExceptionIsThrown() {
new Service().operation();
}
}

View File

@ -0,0 +1,5 @@
grant {
// This is for testing purposes only.
// It allows us to properly reset the security manager after the unit test completes.
permission java.lang.RuntimePermission "setSecurityManager";
};

View File

@ -18,7 +18,6 @@
<module>core-java-optional</module>
<module>core-java-lang-operators</module>
<module>core-java-networking-2</module>
<module>core-java-security-manager</module>
<module>core-java-date-operations-2</module>
</modules>