BAEL-1529 Intro to the Java SecurityManager (#7697)
This commit is contained in:
parent
c4a1b44b39
commit
73743acb30
34
core-java-modules/core-java-security-manager/pom.xml
Normal file
34
core-java-modules/core-java-security-manager/pom.xml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<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>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>core-java-security-manager</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.security.manager;
|
||||||
|
|
||||||
|
import java.security.BasicPermission;
|
||||||
|
|
||||||
|
public class CustomPermission extends BasicPermission {
|
||||||
|
public CustomPermission(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CustomPermission(String name, String actions) {
|
||||||
|
super(name, actions);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.baeldung.security.manager;
|
||||||
|
|
||||||
|
public class Service {
|
||||||
|
|
||||||
|
public static final String OPERATION = "my-operation";
|
||||||
|
|
||||||
|
public void operation() {
|
||||||
|
SecurityManager securityManager = System.getSecurityManager();
|
||||||
|
if (securityManager != null) {
|
||||||
|
securityManager.checkPermission(new CustomPermission(OPERATION));
|
||||||
|
}
|
||||||
|
System.out.println("Operation is executed");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Service().operation();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@
|
|||||||
<module>core-java-optional</module>
|
<module>core-java-optional</module>
|
||||||
<module>core-java-lang-operators</module>
|
<module>core-java-lang-operators</module>
|
||||||
<module>core-java-networking-2</module>
|
<module>core-java-networking-2</module>
|
||||||
|
<module>core-java-security-manager</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user