add spring security and token management
This commit is contained in:
25
src/main/java/com/iconplus/smartproc/util/CommonUtil.java
Normal file
25
src/main/java/com/iconplus/smartproc/util/CommonUtil.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.iconplus.smartproc.util;
|
||||
|
||||
import com.iconplus.smartproc.exception.TechnicalException;
|
||||
|
||||
import java.sql.Clob;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class CommonUtil {
|
||||
private CommonUtil() {
|
||||
}
|
||||
|
||||
public static String clobToString(Clob input) {
|
||||
try {
|
||||
if (input != null) {
|
||||
return input.getSubString(1, (int) input.length());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
throw new TechnicalException(Constants.ERR_TTL_40041,
|
||||
Constants.ERR_TTL_40041,
|
||||
Constants.ERR_MSG_40041);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,4 +12,21 @@ public class Constants {
|
||||
public static final String ERR_TTL_10002 = "Data tersedia";
|
||||
public static final String ERR_MSG_10002 = "Jenis Anggaran dengan id : %s sudah tersedia";
|
||||
|
||||
public static final String ERR_CODE_10003 = "10003";
|
||||
public static final String ERR_TTL_10003 = "Data tidak tersedia";
|
||||
public static final String ERR_MSG_10003 = "User dengan email : %s tidak ditemukan";
|
||||
|
||||
public static final String ERR_CODE_10004 = "10004";
|
||||
public static final String ERR_TTL_10004 = "Gagal Authentikasi User";
|
||||
public static final String ERR_MSG_10004 = "Silahkan Periksa kembali Email dan Password Anda";
|
||||
|
||||
public static final String ERR_CODE_40041 = "40041";
|
||||
public static final String ERR_TTL_40041 = "Terjadi Gangguan";
|
||||
public static final String ERR_MSG_40041 = "Masalah Koneksi System";
|
||||
|
||||
public static final String ERR_CODE_40051 = "40051";
|
||||
public static final String ERR_CODE_80007 = "80007";
|
||||
public static final String TITLE_INVALID_NEXT_STEP = "Proses tidak dapat dilanjutkan";
|
||||
|
||||
|
||||
}
|
||||
|
109
src/main/java/com/iconplus/smartproc/util/RSAUtil.java
Normal file
109
src/main/java/com/iconplus/smartproc/util/RSAUtil.java
Normal file
@ -0,0 +1,109 @@
|
||||
package com.iconplus.smartproc.util;
|
||||
|
||||
import com.iconplus.smartproc.exception.TechnicalException;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import java.security.*;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
@Log4j2
|
||||
public class RSAUtil {
|
||||
|
||||
private RSAUtil(){
|
||||
|
||||
}
|
||||
|
||||
public static PrivateKey getPrivateKey(String base64PrivateKey){
|
||||
PrivateKey privateKey = null;
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes()));
|
||||
try {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
privateKey = keyFactory.generatePrivate(keySpec);
|
||||
} catch (InvalidKeySpecException e) {
|
||||
log.error("InvalidKeySpecException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "InvalidKeySpecException : "+e.getMessage());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error("NoSuchAlgorithmException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchAlgorithmException : "+e.getMessage());
|
||||
}
|
||||
return privateKey;
|
||||
}
|
||||
|
||||
public static PublicKey getPublicKey(String base64PublicKey){
|
||||
PublicKey publicKey = null;
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
|
||||
try {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
publicKey = keyFactory.generatePublic(keySpec);
|
||||
} catch (InvalidKeySpecException e) {
|
||||
log.error("InvalidKeySpecException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "InvalidKeySpecException : "+e.getMessage());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error("NoSuchAlgorithmException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchAlgorithmException : "+e.getMessage());
|
||||
}
|
||||
return publicKey;
|
||||
}
|
||||
|
||||
private static String decryptChipper(byte[] data, PrivateKey privateKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
return new String(cipher.doFinal(data));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error("NoSuchAlgorithmException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchAlgorithmException : "+e.getMessage());
|
||||
} catch (NoSuchPaddingException e) {
|
||||
log.error("NoSuchPaddingException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchPaddingException : "+e.getMessage());
|
||||
} catch (InvalidKeyException e) {
|
||||
log.error("InvalidKeyException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "InvalidKeyException : "+e.getMessage());
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
log.error("IllegalBlockSizeException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "IllegalBlockSizeException : "+e.getMessage());
|
||||
} catch (BadPaddingException e) {
|
||||
log.error("BadPaddingException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "BadPaddingException : "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] encryptChipper(byte[] data, PublicKey publicKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPPadding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
return cipher.doFinal(data);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
log.error("NoSuchAlgorithmException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchAlgorithmException : "+e.getMessage());
|
||||
} catch (NoSuchPaddingException e) {
|
||||
log.error("NoSuchPaddingException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "NoSuchPaddingException : "+e.getMessage());
|
||||
} catch (InvalidKeyException e) {
|
||||
log.error("InvalidKeyException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "InvalidKeyException : "+e.getMessage());
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
log.error("IllegalBlockSizeException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "IllegalBlockSizeException : "+e.getMessage());
|
||||
} catch (BadPaddingException e) {
|
||||
log.error("BadPaddingException : "+e.getMessage());
|
||||
throw new TechnicalException("80000", "BadPaddingException : "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static String decrypt(String data, String base64PrivateKey) {
|
||||
return decryptChipper(Base64.getDecoder().decode(data.getBytes()), getPrivateKey(base64PrivateKey));
|
||||
}
|
||||
|
||||
public static String encrypt(String data, String base64PublicKey) {
|
||||
return Base64.getEncoder().encodeToString(encryptChipper(data.getBytes(), getPublicKey(base64PublicKey)));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user