我正在使用 Spring Boot
将 .war
文件部署到 外部 Tomcat 服务器
。
我正在使用 Ajax/Restful
身份验证,我有以下处理身份验证失败的类:
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, exception.getMessage());
}
当我使用嵌入式 Tomcat 服务器时,一切顺利,在身份验证失败时,我得到以下 JSON:
{
"timestamp" : "2015-12-14T15:39:07.365+0000",
"status" : 401,
"error" : "Unauthorized",
"message" : "You have provided wrong credentials",
"path" : "/api/authentication"
}
但是,当使用外部 Tomcat 服务器
时,我得到一个 HTML
响应,它会带来通常的 Tomcat 失败的身份验证页面。有什么方法可以绕过外部服务器吗?
最佳答案
解决方案只是不使用 sendError() 并提供状态代码并提供自定义异常序列化:
@Service
public class AjaxAuthenticationFailureHandler
extends SimpleUrlAuthenticationFailureHandler {
@Autowired
private ObjectMapper objectMapper;
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.getWriter().write(objectMapper.writeValueAsString(exception));
response.getWriter().flush();
}
}
关于ajax - Spring Boot - 外部 Tomcat 服务器 Ajax 身份验证失败消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34271309/