《云原生机密计算最佳实践白皮书》——05编程框架——Apache Teaclave Java TEE SDK: 面向Java生态的机密计算编程框架——Apache_Teaclave_ Java_TEE_SDK 最佳实践(5)
4.5 管理服务实现(host)
在host子模块实现对enclave的管理与服务加载.
cd helloworld/host/src/main/java/com/sample/ mkdir -p helloworld/host
创建Main.java, 实现机密计算服务管理:
package com.sample.helloworld.host; import org.apache.teaclave.javasdk.host.Enclave; import org.apache.teaclave.javasdk.host.EnclaveFactory; import org.apache.teaclave.javasdk.host.EnclaveType; import com.sample.helloworld.common.Service; import java.util.Iterator; public class Main { public static void main(String[] args) throws Exception { EnclaveType[] enclaveTypes = { EnclaveType.MOCK_IN_JVM, EnclaveType.MOCK_IN_SVM, EnclaveType.TEE_SDK}; for (EnclaveType enclaveType : enclaveTypes) { Enclave enclave = EnclaveFactory.create(enclaveType); Iterator<Service> services = enclave.load(Service.class); System.out.println(services.next().sayHelloWorld()); enclave.destroy(); } } }
4.6 编译和运行
回到工程根目录并编译工程:
mvn -Pnative clean package
编译成功后, 运行该Demo:
$JAVA_HOME/bin/java -cp host/target/host-1.0-SNAPSHOT-jar-with dependencies.jar:enclave/target/enclave-1.0-SNAPSHOT-jar-with-dependencies.jar com. sample.helloworld.host.Main
[INFO] Building jar: /opt/temp/helloworld/host/target/host-1.0-SNAP SHOT-jar-with-dependencies.jar [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for helloworld 1.0-SNAPSHOT: [INFO] [INFO] helloworld ......................................... SUCCESS [ 0.111 s] [INFO] common ............................................. SUCCESS [ 14.584 s] [INFO] enclave ............................................ SUCCESS [01:29 min] [INFO] host ............................................... SUCCESS [ 51.027 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:35 min [INFO] Finished at: 2023-02-16T12:01:09Z [INFO] ------------------------------------------------------------------------ # $JAVA_HOME/bin/java -cp host/target/host-1.0-SNAPSHOT-jar-with-dependencies.jar:en clave/target/enclave-1.0-SNAPSHOT-jar-with-dependencies.jar com.sample.helloworld.host. Main Hello World Hello World Hello World