當需要在aop中獲取請求的參數,並做攔截時,可以參考下面的方法:
package com.zxh.configure; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.*; @Aspect @Component @Slf4j public class ControllerAspect { private static ObjectMapper objectMapper = new ObjectMapper(); @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)") public void controllerGetMethodPointcut() { } @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)") public void controllerPostMethodPointcut() { } @Before("controllerGetMethodPointcut()") public void doBeforeGetMethod(JoinPoint joinPoint) throws Exception { Map<String, Object> requestParams = getRequestParams(joinPoint); } @Before("controllerPostMethodPointcut()") public void doBeforePostMethod(JoinPoint joinPoint) throws Exception { Map<String, Object> requestParams = this.getRequestParams(joinPoint); this.preHandle(requestParams); } private void preHandle(Map<String, Object> requestParams) { String uri = (String) requestParams.get("uri"); String method = (String) requestParams.get("method"); String params = ((List<String>) requestParams.get("params")).get(0); JSONObject headers = (JSONObject) requestParams.get("headers"); if (!HttpMethod.POST.toString().equals(method)) { throw new RuntimeException("方法不被允許"); } //寫具體的業務,用於攔截。不符合條件時直接拋出異常,全局捕獲異常進行處理 } //獲取請求的相關信息 private Map<String, Object> getRequestParams(JoinPoint joinPoint) throws UnsupportedEncodingException { Map<String, Object> requestParams = new HashMap<>(); //獲取請求信息 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); requestParams.put("uri", request.getRequestURI()); // 獲取請求頭信息,需要注意的是,請求頭中的key都會轉成小寫 Enumeration<String> enumeration = request.getHeaderNames(); JSONObject headers = new JSONObject(); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement(); String value = request.getHeader(name); headers.put(name, value); } requestParams.put("headers", headers); //獲取請求的方法 String method = request.getMethod(); requestParams.put("method", method); List<String> params = new ArrayList<>(); if (HttpMethod.GET.toString().equals(method)) {// get請求 String queryString = request.getQueryString(); if (StringUtils.isNotBlank(queryString)) { params.add(0, URLDecoder.decode(queryString, "UTF-8")); } } else {//其他請求 Object[] paramsArray = joinPoint.getArgs(); if (paramsArray != null && paramsArray.length > 0) { for (int i = 0; i < paramsArray.length; i++) { if (paramsArray[i] instanceof Serializable || paramsArray[i] instanceof RequestFacade) { params.add(paramsArray[i].toString()); } else { try { //使用json系列化 反射等等方法 反系列化會影響請求性能建議重寫toString方法實現系列化接口 String param = objectMapper.writeValueAsString(paramsArray[i]); if (StringUtils.isNotBlank(param)) params.add(param); } catch (JsonProcessingException e) { e.printStackTrace(); log.error("json序列化異常", e); } } } } } log.info(">>>>>>uri: {},method: {}", request.getRequestURI(), method); log.info(">>>>>>headers: {}", headers); log.info(">>>>>>params: {}", params); requestParams.put("params", params); return requestParams; } }
使用時需要導入aop的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
分別請求下面的路徑,進行測試
get請求:
日志打印結果
post請求
日志打印結果