11package cn .licoy .encryptbody .advice ;
22
33import cn .licoy .encryptbody .annotation .*;
4+ import cn .licoy .encryptbody .bean .EncryptAnnotationInfoBean ;
45import cn .licoy .encryptbody .enums .EncryptBodyMethod ;
56import cn .licoy .encryptbody .enums .SHAEncryptType ;
7+ import cn .licoy .encryptbody .exception .EncryptBodyFailException ;
8+ import cn .licoy .encryptbody .exception .EncryptMethodNotFoundException ;
9+ import cn .licoy .encryptbody .exception .KeyNotConfiguredException ;
610import cn .licoy .encryptbody .util .*;
711import com .fasterxml .jackson .core .JsonProcessingException ;
812import com .fasterxml .jackson .databind .ObjectMapper ;
1721import org .springframework .web .bind .annotation .ControllerAdvice ;
1822import org .springframework .web .servlet .mvc .method .annotation .ResponseBodyAdvice ;
1923
24+ import java .lang .annotation .Annotation ;
25+
2026
2127/**
2228 * @author licoy.cn
2329 * @version 2018/9/4
30+ * ResponseBodyAdvice实现类
2431 */
2532@ Order (1 )
2633@ ControllerAdvice
@@ -40,56 +47,172 @@ public EncryptBodyAdvice(ObjectMapper objectMapper,EncryptBodyConfig config) {
4047
4148 @ Override
4249 public boolean supports (MethodParameter returnType , Class converterType ) {
50+ Annotation [] annotations = returnType .getDeclaringClass ().getAnnotations ();
51+ if (annotations !=null && annotations .length >0 ){
52+ for (Annotation annotation : annotations ) {
53+ if (annotation instanceof EncryptBody ||
54+ annotation instanceof AESEncryptBody ||
55+ annotation instanceof DESEncryptBody ||
56+ annotation instanceof RSAEncryptBody ||
57+ annotation instanceof MD5EncryptBody ||
58+ annotation instanceof SHAEncryptBody ){
59+ return true ;
60+ }
61+ }
62+ }
4363 return returnType .getMethod ().isAnnotationPresent (EncryptBody .class ) ||
44- returnType .getMethod ().isAnnotationPresent (AESEncryptBody .class ) ||
45- returnType .getMethod ().isAnnotationPresent (DESEncryptBody .class ) ||
46- returnType .getMethod ().isAnnotationPresent (RSAEncryptBody .class ) ||
47- returnType .getMethod ().isAnnotationPresent (MD5EncryptBody .class ) ||
48- returnType .getMethod ().isAnnotationPresent (SHAEncryptBody .class )
49- ;
64+ returnType .getMethod ().isAnnotationPresent (AESEncryptBody .class ) ||
65+ returnType .getMethod ().isAnnotationPresent (DESEncryptBody .class ) ||
66+ returnType .getMethod ().isAnnotationPresent (RSAEncryptBody .class ) ||
67+ returnType .getMethod ().isAnnotationPresent (MD5EncryptBody .class ) ||
68+ returnType .getMethod ().isAnnotationPresent (SHAEncryptBody .class );
5069 }
5170
5271 @ Override
5372 public Object beforeBodyWrite (Object body , MethodParameter returnType , MediaType selectedContentType ,
5473 Class selectedConverterType , ServerHttpRequest request , ServerHttpResponse response ) {
5574 if (body ==null ) return null ;
56- EncryptBodyMethod encryptBodyMethod = null ;
57- if (returnType .getMethod ().isAnnotationPresent (EncryptBody .class )){
58- EncryptBody encryptBody = returnType .getMethodAnnotation (EncryptBody .class );
59- encryptBodyMethod = encryptBody .value ();
60- }
6175 response .getHeaders ().setContentType (MediaType .TEXT_PLAIN );
62- String s = null ;
76+ String str = null ;
6377 try {
64- s = objectMapper .writeValueAsString (body );
78+ str = objectMapper .writeValueAsString (body );
6579 } catch (JsonProcessingException e ) {
6680 e .printStackTrace ();
6781 }
68- return switchEncrypt (s , encryptBodyMethod );
82+ EncryptAnnotationInfoBean classAnnotation = getClassAnnotation (returnType .getDeclaringClass ());
83+ if (classAnnotation !=null ){
84+ return switchEncrypt (str , classAnnotation );
85+ }
86+ EncryptAnnotationInfoBean methodAnnotation = getMethodAnnotation (returnType );
87+ if (methodAnnotation !=null ){
88+ return switchEncrypt (str , methodAnnotation );
89+ }
90+ throw new EncryptBodyFailException ();
6991 }
7092
71- private String switchEncrypt (String formatStringBody ,EncryptBodyMethod method ){
93+ /**
94+ * 获取方法控制器上的加密注解信息
95+ * @param methodParameter 控制器方法
96+ * @return 加密注解信息
97+ */
98+ private EncryptAnnotationInfoBean getMethodAnnotation (MethodParameter methodParameter ){
99+ if (methodParameter .getMethod ().isAnnotationPresent (EncryptBody .class )){
100+ EncryptBody encryptBody = methodParameter .getMethodAnnotation (EncryptBody .class );
101+ return EncryptAnnotationInfoBean .builder ()
102+ .encryptBodyMethod (encryptBody .value ())
103+ .key (encryptBody .otherKey ())
104+ .shaEncryptType (encryptBody .shaType ())
105+ .build ();
106+ }
107+ if (methodParameter .getMethod ().isAnnotationPresent (MD5EncryptBody .class )){
108+ return EncryptAnnotationInfoBean .builder ()
109+ .encryptBodyMethod (EncryptBodyMethod .MD5 )
110+ .build ();
111+ }
112+ if (methodParameter .getMethod ().isAnnotationPresent (SHAEncryptBody .class )){
113+ return EncryptAnnotationInfoBean .builder ()
114+ .encryptBodyMethod (EncryptBodyMethod .SHA )
115+ .shaEncryptType (methodParameter .getMethodAnnotation (SHAEncryptBody .class ).value ())
116+ .build ();
117+ }
118+ if (methodParameter .getMethod ().isAnnotationPresent (DESEncryptBody .class )){
119+ return EncryptAnnotationInfoBean .builder ()
120+ .encryptBodyMethod (EncryptBodyMethod .DES )
121+ .key (methodParameter .getMethodAnnotation (DESEncryptBody .class ).otherKey ())
122+ .build ();
123+ }
124+ if (methodParameter .getMethod ().isAnnotationPresent (AESEncryptBody .class )){
125+ return EncryptAnnotationInfoBean .builder ()
126+ .encryptBodyMethod (EncryptBodyMethod .AES )
127+ .key (methodParameter .getMethodAnnotation (AESEncryptBody .class ).otherKey ())
128+ .build ();
129+ }
130+ return null ;
131+ }
132+
133+ /**
134+ * 获取类控制器上的加密注解信息
135+ * @param clazz 控制器类
136+ * @return 加密注解信息
137+ */
138+ private EncryptAnnotationInfoBean getClassAnnotation (Class clazz ){
139+ Annotation [] annotations = clazz .getDeclaredAnnotations ();
140+ if (annotations !=null && annotations .length >0 ){
141+ for (Annotation annotation : annotations ) {
142+ if (annotation instanceof EncryptBody ){
143+ EncryptBody encryptBody = (EncryptBody ) annotation ;
144+ return EncryptAnnotationInfoBean .builder ()
145+ .encryptBodyMethod (encryptBody .value ())
146+ .key (encryptBody .otherKey ())
147+ .shaEncryptType (encryptBody .shaType ())
148+ .build ();
149+ }
150+ if (annotation instanceof MD5EncryptBody ){
151+ return EncryptAnnotationInfoBean .builder ()
152+ .encryptBodyMethod (EncryptBodyMethod .MD5 )
153+ .build ();
154+ }
155+ if (annotation instanceof SHAEncryptBody ){
156+ return EncryptAnnotationInfoBean .builder ()
157+ .encryptBodyMethod (EncryptBodyMethod .SHA )
158+ .shaEncryptType (((SHAEncryptBody ) annotation ).value ())
159+ .build ();
160+ }
161+ if (annotation instanceof DESEncryptBody ){
162+ return EncryptAnnotationInfoBean .builder ()
163+ .encryptBodyMethod (EncryptBodyMethod .DES )
164+ .key (((DESEncryptBody ) annotation ).otherKey ())
165+ .build ();
166+ }
167+ if (annotation instanceof AESEncryptBody ){
168+ return EncryptAnnotationInfoBean .builder ()
169+ .encryptBodyMethod (EncryptBodyMethod .AES )
170+ .key (((AESEncryptBody ) annotation ).otherKey ())
171+ .build ();
172+ }
173+ }
174+ }
175+ return null ;
176+ }
177+
178+
179+ /**
180+ * 选择加密方式并进行加密
181+ * @param formatStringBody 目标加密字符串
182+ * @param infoBean 加密信息
183+ * @return 加密结果
184+ */
185+ private String switchEncrypt (String formatStringBody ,EncryptAnnotationInfoBean infoBean ){
186+ EncryptBodyMethod method = infoBean .getEncryptBodyMethod ();
187+ if (method ==null ){
188+ throw new EncryptMethodNotFoundException ();
189+ }
72190 if (method == EncryptBodyMethod .MD5 ){
73191 return MD5EncryptUtil .encrypt (formatStringBody );
74192 }
75193 if (method == EncryptBodyMethod .SHA ){
76- return SHAEncryptUtil .encrypt (formatStringBody ,SHAEncryptType .SHA256 );
194+ SHAEncryptType shaEncryptType = infoBean .getShaEncryptType ();
195+ if (shaEncryptType ==null ) shaEncryptType = SHAEncryptType .SHA256 ;
196+ return SHAEncryptUtil .encrypt (formatStringBody ,shaEncryptType );
77197 }
198+ String key = infoBean .getKey ();
78199 if (method == EncryptBodyMethod .DES ){
79- if (StringUtils .isNullOrEmpty (config .getDesKey ())){
200+ if (StringUtils .isNullOrEmpty (config .getDesKey ()) && StringUtils . isNullOrEmpty ( key ) ){
80201 log .error ("未配置des-key / Des-key not configured" );
81- throw new RuntimeException ("未配置des-key / Des-key not configured" );
202+ throw new KeyNotConfiguredException ("未配置des-key / Des-key not configured" );
82203 }
83- return DESEncryptUtil .encrypt (formatStringBody ,config .getDesKey ());
204+ if (key ==null ) key =config .getDesKey ();
205+ return DESEncryptUtil .encrypt (formatStringBody ,key );
84206 }
85207 if (method == EncryptBodyMethod .AES ){
86- if (StringUtils .isNullOrEmpty (config .getAesKey ())){
208+ if (StringUtils .isNullOrEmpty (config .getAesKey ()) && StringUtils . isNullOrEmpty ( key ) ){
87209 log .error ("未配置aes-key / AES-key not configured" );
88- throw new RuntimeException ("未配置aes-key / AES-key not configured" );
210+ throw new KeyNotConfiguredException ("未配置aes-key / AES-key not configured" );
89211 }
90- return AESEncryptUtil .encrypt (formatStringBody ,config .getAesKey ());
212+ if (key ==null ) key =config .getAesKey ();
213+ return AESEncryptUtil .encrypt (formatStringBody ,key );
91214 }
92- return "encrypt data fail" ;
215+ throw new EncryptBodyFailException () ;
93216 }
94217
95218
0 commit comments