因此,基本上,您需要计算请求正文的哈希值。做到这一点的优雅方法是将装饰器应用于
InputStream。
例如,在处理程序方法内部(在这种情况下,您不能使用
@RequestBody并且需要
HttpMessageConverter手动创建):
@RequestMapping(...)public void handle(HttpServletRequest request) throws IOException { final HashingInputStreamDecorator d = new HashingInputStreamDecorator(request.getInputStream(), secretKey); HttpServletRequest wrapper = new HttpServletRequestWrapper(request) { @Override public ServletInputStream getInputStream() throws IOException { return d; } }; HttpMessageConverter conv = ...; Foo requestBody = (Foo) conv.read(Foo.class, new ServletServerHttpRequest(wrapper)); String hash = d.getHash(); ...}其中哈希值是在的重写
read方法中递增计算的
HashingInputStreamDecorator。
@RequestBody如果您创建
Filter要应用的装饰器,也可以使用。在这种情况下,装饰器可以将计算出的哈希值作为请求属性传递给处理程序方法。但是,您需要仔细映射此过滤器,以仅将其应用于对特定处理程序方法的请求。



