feat: init api (#2)

This commit is contained in:
vran
2022-01-24 22:58:47 +08:00
committed by GitHub
parent 643d182d5f
commit 61e5708196
205 changed files with 17366 additions and 59 deletions

View File

@@ -0,0 +1,8 @@
package com.databasir.common;
public interface DatabasirErrors {
String getErrCode();
String getErrMessage();
}

View File

@@ -0,0 +1,49 @@
package com.databasir.common;
import lombok.Getter;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public class DatabasirException extends RuntimeException {
@Getter
private DatabasirErrors errorCodeMessage;
@Getter
private String errCode;
@Getter
private String errMessage;
/**
* @param errorCodeMessage 错误信息
*/
public DatabasirException(DatabasirErrors errorCodeMessage) {
super(errorCodeMessage.getErrMessage());
this.errorCodeMessage = errorCodeMessage;
this.errCode = errorCodeMessage.getErrCode();
this.errMessage = errorCodeMessage.getErrMessage();
}
/**
* @param errorCodeMessage 错误信息
* @param overrideMessage 覆盖 message
*/
public DatabasirException(DatabasirErrors errorCodeMessage, String overrideMessage) {
super(overrideMessage);
this.errorCodeMessage = errorCodeMessage;
this.errCode = errorCodeMessage.getErrCode();
this.errMessage = overrideMessage;
}
/**
* @param errorCodeMessage 错误信息
* @param cause root cause
*/
public DatabasirException(DatabasirErrors errorCodeMessage, Throwable cause) {
super(errorCodeMessage.getErrMessage(), cause);
this.errorCodeMessage = errorCodeMessage;
this.errCode = errorCodeMessage.getErrCode();
this.errMessage = errorCodeMessage.getErrMessage();
}
}

View File

@@ -0,0 +1,39 @@
package com.databasir.common;
import lombok.Data;
@Data
public class JsonData<T> {
/**
* maybe null
*/
private T data;
/**
* only exists when error happened
*/
private String errCode;
/**
* only exists when error happened
*/
private String errMessage;
public static <T> JsonData<T> ok() {
return ok(null);
}
public static <T> JsonData<T> ok(T data) {
JsonData<T> jsonData = new JsonData<>();
jsonData.setData(data);
return jsonData;
}
public static <T> JsonData<T> error(String errorCode, String errMessage) {
JsonData<T> jsonData = new JsonData<>();
jsonData.setErrCode(errorCode);
jsonData.setErrMessage(errMessage);
return jsonData;
}
}

View File

@@ -0,0 +1,27 @@
package com.databasir.common;
public class SystemException extends RuntimeException{
private static final String MSG_INTERNAL_SERVER_ERROR = "服务器开小差了,请稍后再试";
public static final SystemException INTERNAL_SERVER_ERROR = new SystemException(MSG_INTERNAL_SERVER_ERROR);
/**
* @param msg the detail message
*/
public SystemException(String msg) {
super(msg);
}
/**
* @param msg the detail message
* @param cause the nested exception
*/
public SystemException(String msg, Throwable cause) {
super(msg, cause);
}
public static SystemException internalServerErrorWithCause(Throwable cause) {
return new SystemException(MSG_INTERNAL_SERVER_ERROR, cause);
}
}

View File

@@ -0,0 +1,36 @@
package com.databasir.common.codec;
import org.apache.shiro.crypto.AesCipherService;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Base64;
public class Aes {
public static String encryptToBase64Data(String data, String key) {
return new AesCipherService()
.encrypt(toBytes(data), toBytes(key))
.toBase64();
}
public static String decryptFromBase64Data(String encryptData, String key) {
byte[] originEncrypted = Base64.getDecoder().decode(toBytes(encryptData));
byte[] originKey = toBytes(key);
return toString(new AesCipherService().decrypt(originEncrypted, originKey).getBytes());
}
public static String randomBase64Key() {
Key key = new AesCipherService().generateNewKey();
return Base64.getEncoder().encodeToString(key.getEncoded());
}
private static byte[] toBytes(String data) {
return data.getBytes(StandardCharsets.UTF_8);
}
private static String toString(byte[] bytes) {
return new String(bytes, StandardCharsets.UTF_8);
}
}

View File

@@ -0,0 +1,72 @@
package com.databasir.common.codec;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class Rsa {
private static final String RSA = "RSA";
@SneakyThrows
public static String encryptToBase64DataByPublicKey(String data, String publicBase64Key) {
Cipher encryptCipher = Cipher.getInstance(RSA);
encryptCipher.init(Cipher.ENCRYPT_MODE, decodeBase64PublicKey(publicBase64Key));
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
byte[] encryptedDataBytes = encryptCipher.doFinal(dataBytes);
return Base64.getEncoder().encodeToString(encryptedDataBytes);
}
@SneakyThrows
public static String decryptFromBase64DataByPrivateKey(String data, String privateKey) {
Cipher decryptCipher = Cipher.getInstance(RSA);
decryptCipher.init(Cipher.DECRYPT_MODE, decodeBase64PrivateKey(privateKey));
byte[] dataBytes = Base64.getDecoder().decode(data);
byte[] decryptedBytes = decryptCipher.doFinal(dataBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
@SneakyThrows
private static PublicKey decodeBase64PublicKey(String base64PublicKey) {
byte[] keyBytes = Base64.getDecoder().decode(base64PublicKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes);
return keyFactory.generatePublic(publicKeySpec);
}
@SneakyThrows
private static PrivateKey decodeBase64PrivateKey(String base64PrivateKey) {
byte[] keyBytes = Base64.getDecoder().decode(base64PrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes);
return keyFactory.generatePrivate(privateKeySpec);
}
@SneakyThrows
public static RsaBase64Key generateBase64Key() {
KeyPairGenerator generator = KeyPairGenerator.getInstance(RSA);
generator.initialize(2048);
KeyPair keyPair = generator.generateKeyPair();
String privateKey = Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded());
String publicKey = Base64.getEncoder().encodeToString(keyPair.getPublic().getEncoded());
return new RsaBase64Key(privateKey, publicKey);
}
@Getter
@RequiredArgsConstructor
public static class RsaBase64Key {
private final String privateBase64Key;
private final String publicBase64Key;
}
}

View File

@@ -0,0 +1,18 @@
package com.databasir.common.codec;
import lombok.SneakyThrows;
import org.apache.shiro.codec.Hex;
import java.security.MessageDigest;
public class Sha {
@SneakyThrows
public static String sha256(String data) {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(data.getBytes());
byte[] bytes = messageDigest.digest();
return Hex.encodeToString(bytes);
}
}

View File

@@ -0,0 +1,4 @@
package com.databasir.common.exception;
public class Forbidden extends RuntimeException{
}

View File

@@ -0,0 +1,24 @@
package com.databasir.common.exception;
import com.databasir.common.DatabasirErrors;
import com.databasir.common.DatabasirException;
public class InvalidTokenException extends DatabasirException {
public InvalidTokenException(DatabasirErrors errorCodeMessage) {
super(errorCodeMessage);
}
public InvalidTokenException(DatabasirErrors errorCodeMessage, String overrideMessage) {
super(errorCodeMessage, overrideMessage);
}
public InvalidTokenException(DatabasirErrors errorCodeMessage, Throwable cause) {
super(errorCodeMessage, cause);
}
@Override
public String toString() {
return getErrCode() + ": " + getErrMessage();
}
}

View File

@@ -0,0 +1,35 @@
package com.databasir.common.codec;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class CodecTest {
@Test
public void testAes() {
String key = Aes.randomBase64Key();
Assertions.assertNotNull(key);
String data = "hello world!";
String encryptedData = Aes.encryptToBase64Data(data, key);
Assertions.assertNotNull(encryptedData);
String decryptedData = Aes.decryptFromBase64Data(encryptedData, key);
Assertions.assertEquals(data, decryptedData);
}
@Test
public void testRsa() {
Rsa.RsaBase64Key key = Rsa.generateBase64Key();
Assertions.assertNotNull(key);
Assertions.assertNotNull(key.getPrivateBase64Key());
Assertions.assertNotNull(key.getPublicBase64Key());
String data = "Hello world!";
String encrypted = Rsa.encryptToBase64DataByPublicKey(data, key.getPublicBase64Key());
Assertions.assertNotNull(encrypted);
String decrypted = Rsa.decryptFromBase64DataByPrivateKey(encrypted, key.getPrivateBase64Key());
Assertions.assertEquals(data, decrypted);
}
}