springboot@ControllerAdvice + RequestBodyAdvice 복호화 도구 구덩이 밟기 기록

2173 단어 springboot
배경:
최소 몇 십 편의springboot 복호화와 관련된 강좌를 보았는데, 대체로 @Controller Advice 설명 + RequestBody Advice 인터페이스를 실현했습니다.코드가 복잡하지 않기 때문에 자신의 복호화 차단류를 직접 참고하고 처리했다.그러나 차단에 들어갈 수 없었고 손으로 코드를 두드리는 것이 차단류를 잘못 두드린 줄 알고 다시 인터넷에서 코피 코드를 시도했지만 결과적으로 코드가 정상적으로 작동하지 못했다.그러나github 위에서pull 다른 사람의 코드는 달리기 시작하면 정상적으로 차단할 수 있다.그래서 클래스를 다시 차단합니다. 차단할 수 없습니다.
이유:
Controller층의 수신 파라미터가 @RequestBody를 추가하지 않아서 차단에 들어가지 않았습니다!!!이에 기록합니다
Controller층의 수신 파라미터가 @RequestBody를 추가하지 않아서 차단에 들어가지 않았습니다!!!이에 기록합니다
Controller층의 수신 파라미터가 @RequestBody를 추가하지 않아서 차단에 들어가지 않았습니다!!!이에 기록합니다
 
코드 붙여넣기

차단 처리 복호화 실현


1. @ControllerAdvice 메모 삽입
2. RequestBodyAdvice 인터페이스 구현
@ControllerAdvice(basePackages = "com.xiaoc.verify.controller")
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
        //            ,     ,       
        return methodParameter.getMethodAnnotation(DecodeRequest.class) != null ;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) throws IOException {
        System.out.println("***       ***");
        return httpInputMessage;
    }

    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
        return o;
    }

    @Nullable
    @Override
    public Object handleEmptyBody(@Nullable Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class extends HttpMessageConverter>> aClass) {
        return o;
    }
}

Controller 레이어


관건은 매개 변수를 받는 @RequestBody 메모입니다.
@ResponseBody
@RequestMapping(value = "/test",method = RequestMethod.POST)
public ReturnVo test(@RequestBody String param) throws Exception{
    System.out.println(param);
    return ReturnVo.success();
}

좋은 웹페이지 즐겨찾기