简体   繁体   English

从tomcat的Filter类方法响应servlet客户端时如何设置http状态代码

[英]How to set http status code when responding to servlet client from Filter class-method in tomcat

I am writing a webservice with spring (this question is not about spring...) that implements a (hopefully) restful api. 我正在用spring(这个问题不是关于spring ...)编写一个Web服务,该Web服务实现了(希望)静态API。 From my understanding all response should be in xml or json format. 据我了解,所有响应都应采用xml或json格式。 This is not really a big deal in most cases. 在大多数情况下,这并不是什么大问题。 But in one situation this seems not possible. 但是在一种情况下,这似乎是不可能的。 I am using a facility from tomcat where a servlet is involved. 我正在使用tomcat中涉及Servlet的工具。 I have to use a filter for some reason (and this reason is authentication). 由于某种原因,我不得不使用过滤器(而这就是身份验证)。 As I am new to servlets my understanding is eventually not so well, but to me it looks like this 由于我是servlet的新手,所以最终我的理解不是很好,但是对我来说,看起来像这样

My filter-class derives from javax.servlet.filter and I am writing my code within the doFilter method: 我的过滤器类是从javax.servlet.filter派生的,并且正在doFilter方法中编写代码:

@Override
public void doFilter(ServletRequest request, ServletResponse response,
                          FilterChain chain) throws IOException, ServletException { // ... }

And at some point i realize i have to respond to the client with http status code 401 and also want to give him a xml or json information about what happened. 在某个时候,我意识到我必须以http状态代码401响应客户端,并且还想向他提供有关所发生情况的xml或json信息。 Now to me it looks as if i can either 现在对我来说,好像我可以

1) Use the ServletResponse: This allows me to get an OutputStream and write my xml/json out. 1)使用ServletResponse:这使我可以获得OutputStream并将xml / json写出来。 However I cannot set the http status code at all. 但是,我根本无法设置http状态代码。 The final response arriving at the client does contain some http headers. 到达客户端的最终响应确实包含一些http标头。

2) Cast ServletResponse to HttpServletResponse: This allows me to set status code, but I don't seem to be able to set the response body, but let response body be handled from tomcat. 2)将ServletResponse强制转换为HttpServletResponse:这允许我设置状态代码,但是我似乎无法设置响应主体,但可以从tomcat处理响应主体。

Either way seems incomplete. 两种方法似乎都不完整。 If i use ServletResponse to write to the OutputStream and then cast to HttpServletResponse and then call sendError(401) - hoping that whatever I wrote to OutputStream reaches the client - my response does not contain an http "status line". 如果我使用ServletResponse写入OutputStream,然后转换为HttpServletResponse,然后调用sendError(401)-希望我写到OutputStream的所有内容都到达客户端-我的响应不包含http“状态行”。 However http headers are present like "Server: Apache-Coyote/1.1" 但是,http标头的存在类似于“服务器:Apache-Coyote / 1.1”

any help welcome... 任何帮助欢迎...

I've implemented a filter for authentication shortly. 我不久就实现了用于身份验证的过滤器。 I've coded something similar to this: 我已经编写了类似于以下内容的代码:

public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain)
{
    HttpServletResponse response=(HttpServletResponse) resp;

    boolean authenticated=false;
    // perform authentication

    if (authenticated)
    {
         chain.doFilter(req, response);
    }
    else
    {
         // don't continue the chain
         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
         response.setHeader("WWW-Authenticate", "BASIC realm=\"Your realm\"");

         response.setContentType("what you need");
         PrintWriter writer=response.getWriter();

         // don't set content length , don't close
    }
}

This works for me: 这对我有用:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
    ServletException {

    response.resetBuffer();
    response.getOutputStream().write("Your content".getBytes());
    HttpServletResponse hsr = (HttpServletResponse) response;
    hsr.setStatus(401);
    chain.doFilter(request, response);
}

Method HttpServletResponse::sendError is working for me to return from filter in SpringBoot 2.0 application: 方法HttpServletResponse :: sendError对我来说正在从SpringBoot 2.0应用程序的过滤器中返回:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (authenticated)
    {
         chain.doFilter(req, response);
    }
    else
    {
    ((HttpServletResponse) response).sendError(HttpStatus.FORBIDDEN.value(), "Authorization shall be provided");
    }
    chain.doFilter(request, response);
}

暂无
暂无

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

相关问题 无法将参数从一个类方法调用到另一个类方法 - Unable to call the parameters from one class-method to another class-method 从另一个类方法调用对象数组的特定成员 - Calling a specific member of an object array from another class-method 从JSP调用servlet时出现“ HTTP状态405 –此URL不支持HTTP方法GET”错误 - “HTTP Status 405 – HTTP method GET is not supported by this URL” error when calling servlet from JSP 如何从 servlet 过滤器返回 HTTP 错误代码? - How to return HTTP error code from servlet filter? 如何从javax.servlet.filter doFilter方法返回JSON响应消息和HTTP错误代码? - How do I return a JSON response message and an HTTP error code from javax.servlet.filter doFilter method? 如何将状态从servlet发送到客户端? - How to send status from servlet to client? tomcat7上的servlet eclipse HTTP状态405 - HTTP方法此URL不支持POST - servlet eclipse on tomcat7 HTTP Status 405 - HTTP method POST is not supported by this URL Tomcat:对JSP的Servlet sendRedirect表示:“ HTTP状态405-此URL不支持HTTP方法GET” - Tomcat: Servlet sendRedirect to JSP says: “HTTP Status 405 - HTTP method GET is not supported by this URL” 如何使用 Apache Camel REST DSL (Servlet/Restlet) 设置 HTTP 状态代码原因 - How to set HTTP Status code reason with Apache Camel REST DSL (Servlet/Restlet) 如何从Servlet运行套接字服务器/客户端,以便更好地访问项目代码和文件,从而更好地访问Tomcat服务器? - How to run socket server/client from a servlet to give better access on project code and files to Tomcat Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM