From 73743acb30fbf4c80dc0b768149660fbc3618dd0 Mon Sep 17 00:00:00 2001 From: Denis Date: Wed, 4 Sep 2019 21:30:42 +0800 Subject: [PATCH 1/2] BAEL-1529 Intro to the Java SecurityManager (#7697) --- .../core-java-security-manager/pom.xml | 34 ++++++++++++++++++ .../security/manager/CustomPermission.java | 13 +++++++ .../baeldung/security/manager/Service.java | 18 ++++++++++ .../manager/SecurityManagerUnitTest.java | 35 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 5 files changed, 101 insertions(+) create mode 100644 core-java-modules/core-java-security-manager/pom.xml create mode 100644 core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/CustomPermission.java create mode 100644 core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/Service.java create mode 100644 core-java-modules/core-java-security-manager/src/test/java/com/baeldung/security/manager/SecurityManagerUnitTest.java diff --git a/core-java-modules/core-java-security-manager/pom.xml b/core-java-modules/core-java-security-manager/pom.xml new file mode 100644 index 0000000000..51d5e7d78f --- /dev/null +++ b/core-java-modules/core-java-security-manager/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + core-java-security-manager + 0.1.0-SNAPSHOT + jar + core-java-security-manager + + + com.baeldung.core-java-modules + core-java-modules + 1.0.0-SNAPSHOT + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + + UTF-8 + 1.8 + 1.8 + + diff --git a/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/CustomPermission.java b/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/CustomPermission.java new file mode 100644 index 0000000000..5f9c43336f --- /dev/null +++ b/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/CustomPermission.java @@ -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); + } +} diff --git a/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/Service.java b/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/Service.java new file mode 100644 index 0000000000..1b9f14e3b8 --- /dev/null +++ b/core-java-modules/core-java-security-manager/src/main/java/com/baeldung/security/manager/Service.java @@ -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(); + } +} diff --git a/core-java-modules/core-java-security-manager/src/test/java/com/baeldung/security/manager/SecurityManagerUnitTest.java b/core-java-modules/core-java-security-manager/src/test/java/com/baeldung/security/manager/SecurityManagerUnitTest.java new file mode 100644 index 0000000000..a845f233b5 --- /dev/null +++ b/core-java-modules/core-java-security-manager/src/test/java/com/baeldung/security/manager/SecurityManagerUnitTest.java @@ -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 action) throws Exception { + System.setSecurityManager(new SecurityManager()); + try { + action.call(); + } finally { + System.setSecurityManager(null); + } + } +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index ecf1f4147e..082ffbef53 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -19,6 +19,7 @@ core-java-optional core-java-lang-operators core-java-networking-2 + core-java-security-manager From 8c11eeb20e1370295041d039941e1504d9162b0f Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Wed, 4 Sep 2019 19:07:13 +0530 Subject: [PATCH 2/2] Java blockchain (#7694) * Adding source code for the article tracked under BAEL-3232. * Incorporated the review comments on the article. --- .../src/main/java/com/baeldung/blockchain/Block.java | 5 ++--- .../java/com/baeldung/blockchain/BlockchainUnitTest.java | 7 ++++--- pom.xml | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/java-blockchain/src/main/java/com/baeldung/blockchain/Block.java b/java-blockchain/src/main/java/com/baeldung/blockchain/Block.java index 264e3c4b69..6b4e971cd7 100644 --- a/java-blockchain/src/main/java/com/baeldung/blockchain/Block.java +++ b/java-blockchain/src/main/java/com/baeldung/blockchain/Block.java @@ -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(); } diff --git a/java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java b/java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java index acbf651e78..883e55c351 100644 --- a/java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java +++ b/java-blockchain/src/test/java/com/baeldung/blockchain/BlockchainUnitTest.java @@ -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) diff --git a/pom.xml b/pom.xml index 005a24effa..81338b9f81 100644 --- a/pom.xml +++ b/pom.xml @@ -579,7 +579,7 @@ spring-boot-nashorn java-blockchain - +