1.1 添加依賴html
經過spring boot建立好工程後,添加以下依賴,否則工程中沒法使用切面的註解,就沒法對制定的方法進行攔截java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
1.2 工程目錄結構web
其中 spring
MonitorRequest 自定義註解
RequestAspect 切面類
BookController 測試接口請求
package com.tinno.word.advice; import java.lang.annotation.*; import org.springframework.web.bind.annotation.Mapping; /** * Created by xingle on 2019/5/15 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Mapping @Documented public @interface MonitorRequest { }
1 package com.tinno.word.aspect; 2 3 import com.tinno.word.utils.CodeUtils; 4 import javax.servlet.http.HttpServletRequest; 5 import org.aspectj.lang.ProceedingJoinPoint; 6 import org.aspectj.lang.annotation.Around; 7 import org.slf4j.Logger; 8 import org.slf4j.LoggerFactory; 9 import org.springframework.web.context.request.RequestContextHolder; 10 import org.springframework.web.context.request.ServletRequestAttributes; 11 12 /** 13 * Created by xingle on 2019/5/15 14 * 此類爲一個切面類,主要做用就是對接口的請求進行攔截 15 * 攔截的方式,只須要在指定接口方法上面加上@MonitorRequest註解便可 16 */ 17 18 @Aspect 19 @Component 20 public class RequestAspect { 21 22 private Logger logger = LoggerFactory.getLogger(this.getClass()); 23 24 /** 25 * 環繞通知: 26 * 環繞通知很是強大,能夠決定目標方法是否執行,何時執行,執行時是否須要替換方法參數,執行完畢是否須要替換返回值。 27 * 環繞通知第一個參數必須是org.aspectj.lang.ProceedingJoinPoint類型 28 */ 29 @Around(value = "@annotation(com.tinno.word.advice.MonitorRequest)") 30 public Object doBefore(ProceedingJoinPoint joinPoint) { 31 32 //獲取到請求的屬性 33 ServletRequestAttributes attributes = 34 (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 35 //獲取到請求對象 36 HttpServletRequest request = attributes.getRequest(); 37 38 //獲取請求的方法,是Get仍是Post請求 39 logger.info("method=" + request.getMethod()); 40 41 Object[] args = joinPoint.getArgs(); 42 String de_input = ""; 43 try { 44 logger.debug("請求參數 解密前:" + args[0].toString()); 45 //這裏具體解密方法再也不列出 46 de_input = CodeUtils.decrypt(args[0].toString()); 47 logger.debug("請求參數 解密後:" + de_input); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 args[0] = de_input; 52 53 Object returnValue = null; 54 try { 55 //執行方法,以新的參數(若是不帶args就是用原先的參數) 56 returnValue = joinPoint.proceed(args); 57 logger.debug("returnValue 加密前:" +returnValue.toString()); 58 } catch (Throwable throwable) { 59 throwable.printStackTrace(); 60 } 61 //執行完畢也能夠替換目標方法返回值,這裏加密替換,也能夠不加密原樣返回 62 returnValue = CodeUtils.encrypt(returnValue.toString()); 63 logger.debug("returnValue 加密後:" +returnValue.toString()); 64 return returnValue; 65 66 } 67 68 69 }
1 @MonitorRequest 2 @RequestMapping(value = "/getBookVersionLs", method = {RequestMethod.GET,RequestMethod.POST},produces={"text/html;charset=UTF-8;","application/json;"}) 3 public String getBookVersionLs(@RequestBody String json){ 4 logger.info("Controller-getBookVersionLs 版本選擇書本列表,請求參數:"+json); 5 JSONObject jsonObject = JSONObject.parseObject(json); 6 String userId = jsonObject.getString("udid"); 7 String device_id = jsonObject.getString("device_id"); 8 Map<String, Object> data = JsonUtil.jsonText2JsonMap(jsonObject.getString("data")); 9 10 String rand = data.get("rand").toString(); 11 String version = data.get("version").toString(); 12 13 if (isEmpty(userId) || isEmpty(device_id)){ 14 ResultBody result = ResultBody.toFailure(ErrorInfoEnum.PARAMS_NO_COMPLETE.getStatus(),ErrorInfoEnum.PARAMS_NO_COMPLETE.getDest()); 15 return new Gson().toJson(result); 16 } 17 List<AllBookVersion> allList = bookService.getAllBookVersionLs(); 18 AllBookVersion first = allList.get(0); 19 if (isEmpty(rand) && isEmpty(version)){ 20 rand = first.getRand(); 21 version = first.getVersionLs().get(0); 22 } 23 List<BookVersion> bookList = bookService.getBookVersionLs(device_id,rand,version); 24 25 JSONObject obj = new JSONObject(); 26 obj.put("bookList",bookList); 27 obj.put("allList",allList); 28 ResultBody resultBody = ResultBody.toSussess(obj,""); 29 return new Gson().toJson(resultBody); 30 }
請求參數:json
D1D329BF7D4C02CB97C29CB03DF243D79A280D3DD1C6B0EC89B353394409DE0C950A48E2809E6539DE3A641CBA6A89957296D9E7E1DD4906505C5ADFAF89C0AA0B585B1338A57F1CAD8ABE8538E74B75C7947C0B593E3C6DB66DB2CDE07305B671E4405FFA5701C44590D3DE4973174E7D8FB983E82768A0DE086534494CAA49app
返回結果:函數
加密後的結果spring-boot