プロジェクト
ログの脱感作は、共通のセキュリティ要件です。ツールベースのアプローチの一般的な方法は、コードにあまりにも侵襲。アップライトは特に面倒となっています。
このプロジェクトは、アノテーションベースの方法、および開発を容易にするために構築された脱感作の一般的な方法を提供します。
プロパティ
- ノートは、脱感作に基づくログインします。
- あなたがポリシーの実装をカスタマイズすることができ、ポリシーが有効の状態です。
- 共通するのは、脱感作プログラムを構築しました。
- Javaのディープコピー、および任意のインターフェイスを実装することなく、元のオブジェクト。
- これは、ユーザ定義の注釈をサポートしています。
カスタム注釈
インポート達人
<dependency>
<groupId>com.github.houbb</groupId> <artifactId>sensitive</artifactId> <version>0.0.4</version> </dependency>
カスタム注釈
新機能をv0.0.4。カスタム条件の注釈と注釈戦略を可能にします。
ケース
カスタム注釈
- 戦略脱感作
/**
* 自定义密码脱敏策略
* @author binbin.hou
* date 2019/1/17
* @since 0.0.4
*/
@Inherited
@Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @SensitiveStrategy(CustomPasswordStrategy.class) public @interface SensitiveCustomPasswordStrategy { }
- 力への脱感作エントリ
/**
* 自定义密码脱敏策略生效条件
* @author binbin.hou
* date 2019/1/17
* @since 0.0.4
*/
@Inherited
@Documented @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @SensitiveCondition(ConditionFooPassword.class) public @interface SensitiveCustomPasswordCondition{ }
- TIPS
@SensitiveStrategy
単独の戦略を使用した場合、デフォルトでは有効になっています。
そこ@SensitiveCondition場合
の注意事項を、条件が満たされた場合にのみ、脱感作戦略を実行されます。
@SensitiveCondition
のみ@Sensitiveがあるため、有効にカスタム注釈やメモを建てシステムに
発効するための独自の戦略を持っています。
- ポリシーの優先順位
@Sensitive
力への優先項目、内蔵のノート、システム、そして最終的にユーザ定義の注釈。
対応する実装
2元の注釈@SensitiveStrategy
、@SensitiveCondition
対応する実装を指定します。
- CustomPasswordStrategy.java
public class CustomPasswordStrategy implements IStrategy { @Override public Object des(Object original, IContext context) { return "**********************"; } }
- ConditionFooPassword.java
/**
* 让这些 123456 的密码不进行脱敏
* @author binbin.hou
* date 2019/1/2
* @since 0.0.1
*/
public class ConditionFooPassword implements ICondition { @Override public boolean valid(IContext context) { try { Field field = context.getCurrentField(); final Object currentObj = context.getCurrentObject(); final String name = (String) field.get(currentObj); return !name.equals("123456"); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } }
テスト・オブジェクトの定義
使用のカスタム注釈オブジェクトを定義します。
public class CustomPasswordModel {
@SensitiveCustomPasswordCondition @SensitiveCustomPasswordStrategy private String password; @SensitiveCustomPasswordCondition @SensitiveStrategyPassword private String fooPassword; //其他方法 }
テスト
/**
* 自定义注解测试
*/
@Test
public void customAnnotationTest() { final String originalStr = "CustomPasswordModel{password='hello', fooPassword='123456'}"; final String sensitiveStr = "CustomPasswordModel{password='**********************', fooPassword='123456'}"; CustomPasswordModel model = buildCustomPasswordModel(); Assert.assertEquals(originalStr, model.toString()); CustomPasswordModel sensitive = SensitiveUtil.desCopy(model); Assert.assertEquals(sensitiveStr, sensitive.toString()); Assert.assertEquals(originalStr, model.toString()); }
次のようにオブジェクトを構築する方法:
/**
* 构建自定义密码对象
* @return 对象
*/
private CustomPasswordModel buildCustomPasswordModel(){ CustomPasswordModel model = new CustomPasswordModel(); model.setPassword("hello"); model.setFooPassword("123456"); return model; }