Skip to content

Commit 97aa004

Browse files
author
zhanggaosheng
committed
init
0 parents  commit 97aa004

File tree

14 files changed

+915
-0
lines changed

14 files changed

+915
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/logs
2+
/target
3+
.idea

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# CHANGELOG
2+
3+
## v1.0.0
4+
5+
* 提供全新ESCloud JAVA SDK
6+
* 包含Play,Resource的API JAVA SDK

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# escloud-java-sdk

escloud-java-sdk.iml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: com.auth0:java-jwt:3.9.0" level="project" />
15+
<orderEntry type="library" scope="RUNTIME" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.10.0.pr3" level="project" />
16+
<orderEntry type="library" scope="RUNTIME" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.10.0.pr3" level="project" />
17+
<orderEntry type="library" scope="RUNTIME" name="Maven: com.fasterxml.jackson.core:jackson-core:2.10.0.pr3" level="project" />
18+
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.12" level="project" />
19+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.7.5" level="project" />
20+
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.5" level="project" />
21+
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
22+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.4.1" level="project" />
23+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
24+
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
25+
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
26+
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:2.21.0" level="project" />
27+
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy:1.8.15" level="project" />
28+
<orderEntry type="library" scope="TEST" name="Maven: net.bytebuddy:byte-buddy-agent:1.8.15" level="project" />
29+
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.6" level="project" />
30+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpasyncclient:4.0.2" level="project" />
31+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.1" level="project" />
32+
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore-nio:4.4.1" level="project" />
33+
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.1" level="project" />
34+
<orderEntry type="library" name="Maven: org.json:json:20190722" level="project" />
35+
</component>
36+
</module>

pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.escloud</groupId>
8+
<artifactId>escloud-java-sdk</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<java-jwt>3.9.0</java-jwt>
15+
<slf4j>1.7.5</slf4j>
16+
<junit>4.12</junit>
17+
<org.apache.httpcomponents.httpasyncclient>4.0.2</org.apache.httpcomponents.httpasyncclient>
18+
<org.apache.httpcomponents.httpclient>4.4.1</org.apache.httpcomponents.httpclient>
19+
<org.apache.httpcomponents>4.4.1</org.apache.httpcomponents>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.auth0</groupId>
25+
<artifactId>java-jwt</artifactId>
26+
<version>${java-jwt}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-log4j12</artifactId>
31+
<version>${slf4j}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.httpcomponents</groupId>
35+
<artifactId>httpclient</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>${junit}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.mockito</groupId>
45+
<artifactId>mockito-core</artifactId>
46+
<version>2.21.0</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<!-- httpclient api -->
51+
<dependency>
52+
<groupId>org.apache.httpcomponents</groupId>
53+
<artifactId>httpasyncclient</artifactId>
54+
<version>${org.apache.httpcomponents.httpasyncclient}</version>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.apache.httpcomponents</groupId>
59+
<artifactId>httpclient</artifactId>
60+
<version>${org.apache.httpcomponents.httpclient}</version>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.apache.httpcomponents</groupId>
65+
<artifactId>httpcore</artifactId>
66+
<version>${org.apache.httpcomponents}</version>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.apache.httpcomponents</groupId>
71+
<artifactId>httpcore-nio</artifactId>
72+
<version>${org.apache.httpcomponents}</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>com.google.code.gson</groupId>
76+
<artifactId>gson</artifactId>
77+
<version>2.8.1</version>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.google.code.gson</groupId>
82+
<artifactId>gson</artifactId>
83+
<version>2.8.1</version>
84+
<scope>compile</scope>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.json</groupId>
89+
<artifactId>json</artifactId>
90+
<version>20190722</version>
91+
</dependency>
92+
93+
</dependencies>
94+
95+
</project>

src/main/java/com/escloud/Auth.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.escloud;
2+
3+
4+
import java.io.UnsupportedEncodingException;
5+
import java.security.MessageDigest;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.util.Base64;
8+
import java.util.Date;
9+
10+
import com.auth0.jwt.algorithms.Algorithm;
11+
import com.auth0.jwt.JWT;
12+
import com.auth0.jwt.JWTCreator;
13+
14+
import java.util.Map;
15+
16+
import java.util.Random;
17+
18+
public class Auth {
19+
protected String accessKey;
20+
21+
protected String secretKey;
22+
23+
public Auth(String accessKey, String secretKey) {
24+
this.accessKey = accessKey;
25+
this.secretKey = secretKey;
26+
}
27+
28+
public String getAccessKey() {
29+
return accessKey;
30+
}
31+
32+
33+
public String makeSignature(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
34+
return Base64.getEncoder().encodeToString(encrypt(text).getBytes("UTF-8")).replace('+', '-').replace('/', '_');
35+
}
36+
37+
38+
public static String encrypt(String srcStr) {
39+
try {
40+
StringBuilder result = new StringBuilder();
41+
MessageDigest md = MessageDigest.getInstance("SHA1");
42+
byte[] bytes = md.digest(srcStr.getBytes("utf-8"));
43+
for (byte b : bytes) {
44+
String hex = Integer.toHexString(b & 0xFF);
45+
if (hex.length() == 1)
46+
result.append("0");
47+
result.append(hex);
48+
}
49+
return result.toString();
50+
} catch (Exception e) {
51+
throw new RuntimeException(e);
52+
}
53+
}
54+
55+
56+
/**
57+
* @param uri
58+
* @param lifetime
59+
* @return
60+
*/
61+
public String makeRequestAuthorization(String uri, int lifetime) {
62+
JWTCreator.Builder builder = JWT.create();
63+
64+
builder.withKeyId(this.accessKey);
65+
builder.withJWTId(getRandomString(16));
66+
builder.withExpiresAt(new Date(System.currentTimeMillis() + lifetime * 1000));
67+
68+
Algorithm algorithm = Algorithm.HMAC256(this.secretKey);
69+
return "Bearer " + builder.sign(algorithm);
70+
}
71+
72+
/**
73+
* @param payload
74+
* @return
75+
*/
76+
public String makeJwtToken(Map<String, Object> payload) {
77+
JWTCreator.Builder builder = JWT.create();
78+
79+
for (String key : payload.keySet()) {
80+
if (payload.get(key).getClass().toString() == "Boolean") {
81+
builder.withClaim(key, (Boolean) payload.get(key));
82+
} else if (payload.get(key).getClass().toString() == "String") {
83+
builder.withClaim(key, (String) payload.get(key));
84+
} else if (payload.get(key).getClass().toString() == "Date") {
85+
builder.withClaim(key, (Date) payload.get(key));
86+
}
87+
}
88+
builder.withJWTId(getRandomString(16));
89+
Algorithm algorithm = Algorithm.HMAC256(this.secretKey);
90+
return builder.sign(algorithm);
91+
}
92+
93+
94+
protected static String getRandomString(int length) {
95+
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
96+
Random random = new Random();
97+
StringBuffer sb = new StringBuffer();
98+
for (int i = 0; i < length; i++) {
99+
int number = random.nextInt(62);
100+
sb.append(str.charAt(number));
101+
}
102+
return sb.toString();
103+
}
104+
}
105+

0 commit comments

Comments
 (0)