响应错误中缺少消息参数

时间:2021-07-22 15:29:12

标签: spring-boot spring-security

我创建了一个 CustomAuthenticationFailureHandler 并发送了一些自定义消息。 但是当我在本地运行它时,消息会在响应正文中返回。但是当我将它部署到 aws vm 实例时,消息字段没有出现。

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@Slf4j
public class CustomLoginFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        String message = HttpStatus.UNAUTHORIZED.getReasonPhrase();
        if(e.getCause() instanceof ApplicationException){
            message = e.getMessage();
        }
        log.info("Message: {}",message);
        response.sendError(HttpStatus.UNAUTHORIZED.value(), message);
    }
}

这是我在本地运行时的响应。

{
    "timestamp": 1626966882957,
    "status": 401,
    "error": "Unauthorized",
    "path": "/login"
}

但是当我在本地运行相同的代码时。

{
    "timestamp": 1626967590472,
    "status": 401,
    "error": "Unauthorized",
    "message": "Otp is expired",
    "path": "/login"
}

在第一个响应中,消息字段以某种方式丢失了。需要在 ui 上显示 message 属性。我在这里很困惑为什么本地 vs aws vm 上的两种类型的响应。我已经检查了这两种情况下的日志。日志打印消息字段,但它不会作为响应返回。 谢谢,提前。

0 个答案:

没有答案