Skip to content

Commit 9eec530

Browse files
committed
初始化Http Converter Plugin
添加ValueHideFilter
1 parent fe782bc commit 9eec530

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright [2019] [恒宇少年 - 于起宇]
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<parent>
23+
<artifactId>api-boot-plugins</artifactId>
24+
<groupId>org.minbox.framework</groupId>
25+
<version>2.0.4.RC1</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<properties>
29+
<main.basedir>${basedir}/../../..</main.basedir>
30+
</properties>
31+
<artifactId>api-boot-plugin-http-converter</artifactId>
32+
<description>
33+
ApiBoot Http Converter 插件
34+
提供插件的扩展、集成方式
35+
</description>
36+
<dependencies>
37+
<!--ApiBoot Plugin-->
38+
<dependency>
39+
<groupId>org.minbox.framework</groupId>
40+
<artifactId>api-boot-plugin</artifactId>
41+
</dependency>
42+
<!--fastJson-->
43+
<dependency>
44+
<groupId>com.alibaba</groupId>
45+
<artifactId>fastjson</artifactId>
46+
<optional>true</optional>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.http.converter.filter;
19+
20+
import com.alibaba.fastjson.serializer.ValueFilter;
21+
import org.minbox.framework.api.boot.plugin.http.converter.filter.annotation.ApiBootValueHide;
22+
23+
import java.lang.reflect.Field;
24+
25+
/**
26+
* Value Hide Filter
27+
*
28+
* @author:恒宇少年 - 于起宇
29+
* <p>
30+
* DateTime:2019-04-15 17:44
31+
* Blog:http://blog.yuqiyu.com
32+
* WebSite:http://www.jianshu.com/u/092df3f77bca
33+
* Gitee:https://gitee.com/hengboy
34+
* GitHub:https://github.com/hengboy
35+
*/
36+
public class ValueHideFilter implements ValueFilter {
37+
/**
38+
* Execute hide value
39+
*
40+
* @param object object instance
41+
* @param name field name
42+
* @param value filed value
43+
* @return hidden values
44+
*/
45+
@Override
46+
public Object process(Object object, String name, Object value) {
47+
if (object != null) {
48+
try {
49+
// get declared field
50+
Field field = object.getClass().getDeclaredField(name);
51+
if (field.isAnnotationPresent(ApiBootValueHide.class)) {
52+
// field declared ApiBootValueHide Annotation
53+
ApiBootValueHide valueHide = field.getDeclaredAnnotation(ApiBootValueHide.class);
54+
// don't execute hide
55+
if (valueHide.length() <= 0) {
56+
return object;
57+
}
58+
// field value
59+
String fieldValue = String.valueOf(value);
60+
// convert to char array
61+
char[] chars = fieldValue.toCharArray();
62+
63+
// hide with start position
64+
if (valueHide.start() > 0) {
65+
return startHide(valueHide, chars);
66+
}
67+
// hide with position model
68+
else {
69+
return positionHide(valueHide, chars);
70+
}
71+
72+
}
73+
74+
} catch (Exception e) {
75+
return value;
76+
}
77+
}
78+
return value;
79+
}
80+
81+
/**
82+
* hide with start position
83+
*
84+
* @param valueHide ApiBootValueHide annotation
85+
* @param chars field value char array
86+
* @return hidden values
87+
*/
88+
String startHide(ApiBootValueHide valueHide, char[] chars) {
89+
for (int i = 0; i < chars.length; i++) {
90+
if (valueHide.start() < i && i < valueHide.start() + valueHide.length()) {
91+
chars[i] = valueHide.placeholder().charAt(0);
92+
}
93+
}
94+
return new String(chars);
95+
}
96+
97+
/**
98+
* hide with position model
99+
*
100+
* @param valueHide ApiBootValueHide annotation
101+
* @param chars field value char array
102+
* @return hidden values
103+
*/
104+
String positionHide(ApiBootValueHide valueHide, char[] chars) {
105+
for (int i = 0; i < chars.length; i++) {
106+
switch (valueHide.position()) {
107+
// Back to back
108+
case START:
109+
if (i < valueHide.length()) {
110+
chars[i] = valueHide.placeholder().charAt(0);
111+
}
112+
break;
113+
// middle
114+
case MIDDLE:
115+
// start hide position
116+
int startPosition = chars.length / 2;
117+
if (i >= startPosition - 1 && i < startPosition - 1 + valueHide.length()) {
118+
chars[i] = valueHide.placeholder().charAt(0);
119+
}
120+
break;
121+
// From behind
122+
case END:
123+
if (i >= chars.length - valueHide.length()) {
124+
chars[i] = valueHide.placeholder().charAt(0);
125+
}
126+
break;
127+
}
128+
}
129+
return new String(chars);
130+
}
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.http.converter.filter.annotation;
19+
20+
import org.minbox.framework.api.boot.plugin.http.converter.filter.enums.ValueHidePositionEnum;
21+
22+
import java.lang.annotation.ElementType;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.RetentionPolicy;
25+
import java.lang.annotation.Target;
26+
27+
/**
28+
* ApiBoot Value Hide Value Filter
29+
*
30+
* @author:恒宇少年 - 于起宇
31+
* <p>
32+
* DateTime:2019-04-15 17:45
33+
* Blog:http://blog.yuqiyu.com
34+
* WebSite:http://www.jianshu.com/u/092df3f77bca
35+
* Gitee:https://gitee.com/hengboy
36+
* GitHub:https://github.com/hengboy
37+
*/
38+
@Target({ElementType.FIELD})
39+
@Retention(RetentionPolicy.RUNTIME)
40+
public @interface ApiBootValueHide {
41+
/**
42+
* hide length
43+
*
44+
* @return
45+
*/
46+
int length() default 0;
47+
48+
/**
49+
* start hide position
50+
*
51+
* @return
52+
*/
53+
int start() default 0;
54+
55+
/**
56+
* 隐藏位置枚举
57+
* 1、从前往后开始隐藏(排除excludeLength的值个字符)
58+
* 2、从后往前开始隐藏(排除excludeLength的值个字符)
59+
*
60+
* @return
61+
*/
62+
ValueHidePositionEnum position();
63+
64+
/**
65+
* placeholder
66+
*
67+
* @return
68+
*/
69+
String placeholder() default "*";
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.plugin.http.converter.filter.enums;
19+
20+
/**
21+
* 隐藏位置枚举
22+
* @author:于起宇 <br/>
23+
* ===============================
24+
* Created with IDEA.
25+
* Date:2018/1/20
26+
* Time:14:27
27+
* 简书:http://www.jianshu.com/u/092df3f77bca
28+
* ================================
29+
*/
30+
public enum ValueHidePositionEnum {
31+
/**
32+
* 从前往后
33+
*/
34+
START,
35+
/**
36+
* 从后往前
37+
*/
38+
END,
39+
/**
40+
* 中间
41+
*/
42+
MIDDLE
43+
}

0 commit comments

Comments
 (0)