在Spring到达ASPECT之前,输入流已关闭

时间:2018-10-31 06:01:01

标签: java spring spring-boot aop spring-aop

我有一个要求,在处理其余端点方法之前需要调用Aspect,我创建了注释并注释了其余端点,

我能够处理请求,但是在读取InpuStream时,它已经关闭了 inseide方面。附加的代码段是我正在使用Spring Boot 1.5

  

GitHub https://github.com/primeap/user-auth-poc.git

一旦我在Aspect中获得了输入流,我将在使用它之前对其进行缓存,但是我的问题是在当前代码中它抛出了

  

io异常流已关闭

在线

  

Map jsonMap =   mapper.readValue(request.getInputStream(),Map.class);

应用

@SpringBootApplication
@ComponentScan(basePackages = { "org.ap" })
public class UserAuthPocApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserAuthPocApplication.class, args);
    }
}

注释

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface HasPrivilegeXX {

}

尊重

@Aspect
@Component
public class MyPrivilegeAspect {
    @Before("@annotation(HasPrivilegeXX)")
    public boolean logBeforeAllMethods(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
                .getRequest();
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> jsonMap = mapper.readValue(request.getInputStream(), Map.class);
            System.out.println("In aspect " + jsonMap.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}

其他端点

@RestController
@RequestMapping("/api")
public class Api {
    @PostMapping("/hi")
    @HasPrivilegeXX()
    public String hiPost(@RequestBody User user) {
        System.out.println(user.toString());
        return "Hi...";
    }
}

用户

public class User {
    private String name;
    private String company;
    private Integer id;
}

0 个答案:

没有答案