Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15393

This commit is contained in:
amit2103 2019-09-04 22:02:11 +05:30
commit fd4e4171fe
8 changed files with 108 additions and 7 deletions

View 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>

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}
}

View File

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

View File

@ -3,7 +3,6 @@ package com.baeldung.blockchain;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -17,10 +16,10 @@ public class Block {
private long timeStamp;
private int nonce;
public Block(String data, String previousHash) {
public Block(String data, String previousHash, long timeStamp) {
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.timeStamp = timeStamp;
this.hash = calculateBlockHash();
}

View File

@ -3,6 +3,7 @@ package com.baeldung.blockchain;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.AfterClass;
@ -17,10 +18,10 @@ public class BlockchainUnitTest {
@BeforeClass
public static void setUp() {
Block genesisBlock = new Block("The is the Genesis Block.", "0");
Block genesisBlock = new Block("The is the Genesis Block.", "0", new Date().getTime());
genesisBlock.mineBlock(prefix);
blockchain.add(genesisBlock);
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash(), new Date().getTime());
firstBlock.mineBlock(prefix);
blockchain.add(firstBlock);
}
@ -28,7 +29,7 @@ public class BlockchainUnitTest {
@Test
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
.getHash());
.getHash(), new Date().getTime());
newBlock.mineBlock(prefix);
assertTrue(newBlock.getHash()
.substring(0, prefix)

View File

@ -579,7 +579,7 @@
<module>spring-boot-nashorn</module>
<module>java-blockchain</module>
<!-- <module>Twitter4J</module> --> <!-- Builds locally, but fails in Jenkins, Failed to parse POMs -->
</modules>
</profile>