【问题标题】:Spring Boot - External Tomcat Server Ajax Authentication Failure MessageSpring Boot - 外部 Tomcat 服务器 Ajax 身份验证失败消息
【发布时间】:2015-12-14 15:54:34
【问题描述】:

我正在使用Spring Boot.war 文件部署到external Tomcat Server

我正在使用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"
}

但是,当使用External Tomcat Server 时,我得到一个HTML 响应,这会带来通常的Tomcat 身份验证失败页面。有什么办法可以绕过外部服务器?

【问题讨论】:

    标签: ajax spring tomcat authentication spring-boot


    【解决方案1】:

    解决方案只是不使用 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();
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 2012-08-15
      • 2015-05-13
      • 2022-08-16
      • 2017-04-09
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多