-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-ISecurityAbility-初步实现数据加密、签名相关的能力
- Loading branch information
Showing
8 changed files
with
155 additions
and
5 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
my-core/src/main/java/net/ximatai/muyun/ability/ISecurityAbility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.ximatai.muyun.ability; | ||
|
||
import net.ximatai.muyun.core.security.AEncryptor; | ||
import net.ximatai.muyun.database.builder.Column; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ISecurityAbility { | ||
|
||
String SIGN_SUFFIX = "_sign_"; | ||
|
||
List<String> getColumnsForSigning(); | ||
|
||
List<String> getColumnsForEncryption(); | ||
|
||
AEncryptor getAEncryptor(); | ||
|
||
default String column2SignColumn(String column) { | ||
return column + SIGN_SUFFIX; | ||
} | ||
|
||
/** | ||
* 获取因为存在签名字段所以需要追加的列 | ||
* | ||
* @return 额外的签名校验列 | ||
*/ | ||
default List<Column> getSignColumns() { | ||
if (getColumnsForSigning() == null) return List.of(); | ||
|
||
return getColumnsForSigning().stream() | ||
.map(this::column2SignColumn) | ||
.map(Column::of) | ||
.map(c -> c.setType("varchar")) | ||
.toList(); | ||
} | ||
|
||
/** | ||
* 该签名签名、该加密加密 | ||
* | ||
* @param map | ||
*/ | ||
default void signAndEncrypt(Map map) { | ||
AEncryptor encryptor = getAEncryptor(); | ||
if (encryptor == null) return; | ||
|
||
getColumnsForSigning().forEach(s -> { | ||
if (map.containsKey(s)) { | ||
map.put(column2SignColumn(s), encryptor.sign(map.get(s).toString())); | ||
} | ||
}); | ||
|
||
getColumnsForEncryption().forEach(s -> { | ||
if (map.containsKey(s)) { | ||
map.put(s, encryptor.encrypt(map.get(s).toString())); | ||
} | ||
}); | ||
} | ||
|
||
default void decrypt(Map map) { | ||
AEncryptor encryptor = getAEncryptor(); | ||
if (encryptor == null) return; | ||
|
||
getColumnsForEncryption().forEach(s -> { | ||
if (map.containsKey(s)) { | ||
map.put(s, encryptor.decrypt(map.get(s).toString())); | ||
} | ||
}); | ||
} | ||
|
||
default void checkSign(Map map) { | ||
AEncryptor encryptor = getAEncryptor(); | ||
if (encryptor == null) return; | ||
|
||
getColumnsForSigning().forEach(s -> { | ||
if (map.containsKey(s)) { | ||
encryptor.checkSign(map.get(s).toString(), map.get(column2SignColumn(s)).toString()); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
my-core/src/main/java/net/ximatai/muyun/core/exception/InvalidSignatureException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.ximatai.muyun.core.exception; | ||
|
||
public class InvalidSignatureException extends RuntimeException { | ||
|
||
public InvalidSignatureException(String message) { | ||
super(message); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
my-core/src/main/java/net/ximatai/muyun/core/security/AEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.ximatai.muyun.core.security; | ||
|
||
import net.ximatai.muyun.core.exception.InvalidSignatureException; | ||
import net.ximatai.muyun.util.StringUtil; | ||
|
||
public abstract class AEncryptor { | ||
|
||
|
||
public void checkSign(String source, String sign) { | ||
if (StringUtil.isBlank(sign)) return; | ||
if (StringUtil.isBlank(source)) return; | ||
if (!sign.equals(sign(source))) { | ||
throw new InvalidSignatureException("数据「%s」已被篡改".formatted(source)); | ||
} | ||
} | ||
|
||
abstract public String sign(String source); | ||
|
||
abstract public String encrypt(String source); | ||
|
||
abstract public String decrypt(String encodeText); | ||
} |
16 changes: 16 additions & 0 deletions
16
my-core/src/main/java/net/ximatai/muyun/util/StringUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.ximatai.muyun.util; | ||
|
||
public class StringUtil { | ||
|
||
public static boolean isBlank(Object x) { | ||
return switch (x) { | ||
case null -> true; | ||
case String str -> str.isBlank() || "NULL".equalsIgnoreCase(str.trim()); | ||
default -> false; | ||
}; | ||
} | ||
|
||
public static boolean isNotBlank(Object x) { | ||
return !isBlank(x); | ||
} | ||
} |