我正在使用Spring启动并使用过滤器过滤请求。 过滤器用于检查用户验证,该验证从请求主体读取一些数据(我已经使用了HttpServletRequestWrapper实现)。从requestWrapper我得到的数据符合预期,我的服务在过滤器中工作得很好。但是,当成功时,过滤器将请求传递给主控制器,我得到一个流关闭异常,如下所示:
[B, I] = sort(orig_matrix.');
Result = I.';
这是Filter类:
o.s.w.s.m.s.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Stream closed; nested exception is java.io.IOException: Stream closed
这里是HttpServletRequestWrapper实现类:
@WebFilter(urlPatterns = {"/getDocs" })
public class AuthenticationFilter implements Filter{
private static Logger logger = Logger.getLogger(AuthenticationFilter.class);
@Autowired
private UserVerificationService userVerificationService;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
logger.info("checking token in filter");
HttpServletRequest request = (HttpServletRequest) arg0;
DocVerificationRequestWrapper myRequestWrapper = new DocVerificationRequestWrapper((HttpServletRequest) request);
String body = myRequestWrapper.getBody();
Token token = null;
try {
JSONObject jsonObj = new JSONObject(body);
JSONObject tokenObj = (JSONObject) jsonObj.get("token");
Gson gson = new Gson();
token = gson.fromJson(tokenObj.toString(), Token.class);
if(null != token) {
if(userVerificationService==null){
ServletContext servletContext = request.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
userVerificationService = webApplicationContext.getBean(UserVerificationService.class);
}
String verStatus = userVerificationService.verifyUser(token);
logger.info("verStatus = "+verStatus);
if(verStatus != null && verStatus.equalsIgnoreCase("success")) {
chain.doFilter(request, response);
}else
logger.error("Invalid token");
}else {
logger.error("token missing.");
}
} catch (JSONException e) {
logger.error("exception in authetication filter " + e);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
有任何建议,如何解决此错误?
答案 0 :(得分:1)
我想这是因为您不是将实际的包装器传递给链,而是传递原始请求。试试这个:
if(verStatus != null && verStatus.equalsIgnoreCase("success")) {
chain.doFilter(myRequestWrapper, response);
}
显然,当ObjectMapper
尝试读取请求正文时 - 请求输入流已经关闭,因为它已经被包装器读取了。