How to change response before send

折月煮酒 提交于 2020-01-06 14:26:11

问题


Is it possible to intercept sending response to client, and send modified response in final ? I want to remove "WWW-Authenticate" header from Basic Auth response or change error code from 401 to 403 in wrong authentication case. P.S. I have the same problem: http://www.java.net/forum/topic/glassfish/glassfish/suppress-www-authenticate-header-if-basic-auth-fails


回答1:


I tried use Filter with HttpServletResponseWrapper but my Filter was never called before JAAS Basic HTTP Authentication. I solved my problems with annoying popup window by next code

In web.xml:

<error-page>
    <error-code>401</error-code>
    <location>/error.jsp</location>
</error-page>

error.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <%
        int status = response.getStatus();
        if (status == 401) {
            response.setStatus(403);
        }
        %>
    </body>
</html>


来源:https://stackoverflow.com/questions/10158877/how-to-change-response-before-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!