简体   繁体   English

为什么sendError返回空的响应主体

[英]Why sendError returns empty response body

I am trying to use sendError method from HttpServletResponse, but always get back an empty body response. 我正在尝试使用HttpServletResponse中的sendError方法,但始终会返回一个空的正文响应。

@Override
protected void doFilterInternal(@NonNull HttpServletRequest httpServletRequest, 
                                @NonNull HttpServletResponse httpServletResponse,
                                @NonNull FilterChain filterChain)
                                    throws ServletException, IOException {

        String token = jwtTokenProvider.resolveToken(httpServletRequest);
        try {
            if (token != null && jwtTokenProvider.validateToken(token)) {
                Authentication auth = jwtTokenProvider.getAuthentication(token);
                SecurityContextHolder.getContext().setAuthentication(auth);
            }
        } catch (CustomException ex) {
            SecurityContextHolder.clearContext();
            httpServletResponse.sendError(ex.getHttpStatus().value(), ex.getMessage());
            return;
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
}

Since it didn't work, I tried constructing my own response and return it to client (this is an API call). 由于它不起作用,我尝试构建自己的响应并将其返回给客户端(这是一个API调用)。

String error = "Expired or invalid JWT token";
ApiError errorResponse = new ApiError(new Date(),500, HttpStatus.INTERNAL_SERVER_ERROR, error, ex.getLocalizedMessage(), httpServletRequest.getRequestURL().toString());
httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
httpServletResponse.getWriter().write(convertObjectToJson(errorResponse));
private String convertObjectToJson(Object object) throws JsonProcessingException {
        if (object == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(object);
}

This works, but I had to construct it using string and then convert it to JSON. 这可行,但是我必须使用字符串构造它,然后将其转换为JSON。 I want to just use the sendError method. 我只想使用sendError方法。 Can someone help me figure out why the response body is always empty? 有人可以帮我弄清楚为什么响应正文始终为空吗? The status code of the empty body response (when using sendError) is 403. 空主体响应的状态码(使用sendError时)为403。

I started having the same problem when I upgraded to spring boot 2.1 from 1.5.8. 从1.5.8升级到Spring Boot 2.1时,我开始遇到同样的问题。

Before the upgrade, the filter HeaderWriterFilter was not in the filter chain, it seems to be added by spring automatically. 在升级之前,过滤器HeaderWriterFilter不在过滤器链中,它似乎是由spring自动添加的。 This filter is also calling your filter when sendError is called. 调用sendError时,此筛选器还会调用您的筛选器。

You did not mention if you are implementing Filter or extending a specific filter, but if you can extend OncePerRequestFilter it should fix the issue. 您没有提到是要实现Filter还是要扩展特定的过滤器,但是如果可以扩展OncePerRequestFilter,它应该可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM