我已经创建了customauthenticationfailurehandler并发送了一些自定义消息。但当我在本地运行消息时,它会在响应体中返回。但当我将其部署到AWSVM示例时,消息字段没有提交。
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上显示消息属性。我在这里感到困惑,为什么本地和aws虚拟机上会出现这两种类型的响应。我已经检查了这两个案例的日志。日志打印消息字段,但不会返回响应。提前谢谢。
暂无答案!
目前还没有任何答案,快来回答吧!