4
4
import cn .springcloud .gray .server .module .user .UserModule ;
5
5
import cn .springcloud .gray .server .resources .domain .ApiRes ;
6
6
import cn .springcloud .gray .utils .WebUtils ;
7
- import com .fasterxml .jackson .core .JsonProcessingException ;
7
+ import com .fasterxml .jackson .core .type . TypeReference ;
8
8
import com .fasterxml .jackson .databind .ObjectMapper ;
9
+ import org .apache .commons .collections .CollectionUtils ;
9
10
import org .apache .commons .lang3 .ArrayUtils ;
10
11
import org .apache .commons .lang3 .StringUtils ;
11
12
import org .aspectj .lang .JoinPoint ;
24
25
import org .springframework .web .context .request .ServletRequestAttributes ;
25
26
26
27
import javax .servlet .http .HttpServletRequest ;
27
- import java .util .Date ;
28
+ import java .io .IOException ;
29
+ import java .util .*;
28
30
29
31
@ Aspect
30
32
public class ResultfulOpRecordAspect {
@@ -41,7 +43,10 @@ public ResultfulOpRecordAspect(ObjectMapper objectMapper, UserModule userModule,
41
43
this .operateAuditModule = operateAuditModule ;
42
44
}
43
45
44
- @ Pointcut ("@annotation(org.springframework.web.bind.annotation.RequestMapping))" )
46
+ @ Pointcut ("@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
47
+ "|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
48
+ "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" +
49
+ "|| @annotation(org.springframework.web.bind.annotation.PutMapping))" )
45
50
public void pointcut () {
46
51
}
47
52
@@ -72,8 +77,9 @@ public void doAfter(JoinPoint joinPoint, Object result) {
72
77
// }
73
78
// }
74
79
try {
75
- operateRecord .setHeadlerArgs (objectMapper .writeValueAsString (joinPoint .getArgs ()));
76
- } catch (JsonProcessingException e ) {
80
+ String HeadlerArgs = desensitizationArgs (request , joinPoint .getArgs ());
81
+ operateRecord .setHeadlerArgs (objectMapper .writeValueAsString (HeadlerArgs ));
82
+ } catch (Exception e ) {
77
83
log .warn (e .getMessage (), e );
78
84
}
79
85
if (result instanceof ApiRes ) {
@@ -86,21 +92,55 @@ public void doAfter(JoinPoint joinPoint, Object result) {
86
92
operateAuditModule .recordOperate (operateRecord );
87
93
}
88
94
95
+ private Set <String > desensitizationUris = new HashSet <>(
96
+ Arrays .asList ("/gray/user/" , "/gray/user/login" , "/gray/user/resetPassword" , "/gray/user/updatePassword" ));
97
+
98
+
99
+ private String [] desensitizationFields = new String []{"password" ,"oldPassword" ,"newPassword" };
100
+
101
+ private String desensitizationArgs (HttpServletRequest request , Object [] args ) throws IOException {
102
+ if (desensitizationUris .contains (request .getRequestURI ())) {
103
+ List <Map <String , Object >> list = objectMapper .readValue (objectMapper .writeValueAsString (args ), new TypeReference <List <Map <String , Object >>>(){});
104
+ if (CollectionUtils .isNotEmpty (list )){
105
+ Map <String , Object > map = list .get (0 );
106
+ Object v ;
107
+ for (String field : desensitizationFields ){
108
+ v = map .get (field );
109
+ if (!Objects .isNull (v )){
110
+ map .put (field , convertDesensitization (v .toString ()));
111
+ }
112
+ }
113
+
114
+ }
115
+ return objectMapper .writeValueAsString (list );
116
+ }
117
+ return objectMapper .writeValueAsString (args );
118
+ }
119
+
120
+
121
+ private String convertDesensitization (String str ){
122
+ StringBuilder sb = new StringBuilder ();
123
+ for (int i =0 , l =str .length (); i <l ;i ++){
124
+ sb .append ('*' );
125
+ }
126
+ return sb .toString ();
127
+ }
128
+
89
129
90
130
private boolean isSholdRecord (RequestMapping requestMapping ) {
91
- if (requestMapping == null ){
131
+ if (requestMapping == null ) {
92
132
return false ;
93
133
}
94
134
return isSholdRecord (requestMapping .method ()) && sholdFromRequest ();
95
135
}
96
136
97
- private RequestMapping getRequestMapping (JoinPoint joinPoint ){
137
+ private RequestMapping getRequestMapping (JoinPoint joinPoint ) {
98
138
if (joinPoint .getSignature () instanceof MethodSignature ) {
99
139
MethodSignature signature = (MethodSignature ) joinPoint .getSignature ();
100
140
RequestMapping requestMapping = AnnotationUtils .findAnnotation (signature .getMethod (), RequestMapping .class );
101
- if (requestMapping == null ){
141
+ if (requestMapping == null ) {
102
142
return AnnotationUtils .findAnnotation (joinPoint .getTarget ().getClass (), RequestMapping .class );
103
- }else {
143
+ } else {
104
144
return requestMapping ;
105
145
}
106
146
}
0 commit comments