Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify a Java servlet response header in case the container authentication has failed?

I have a Java EE project with web services protected by web container authentication. (HTTP-basic) (we can say in this context: the web services are servlets) A would like to modify the servlets response header. Using servlet filter is not a good solution because I want to access the response object in the event of the user authentication failed. (in this case the servlet filter does not run, because the container does not invoke it)

The reason is, I want to change the HTTP status codes 401 and 403. That is because the client are distributed by Web Start and I do not want to allow the javaws to modify my client application request headers.

There is a ServletRequestListener listener, but it is not right for me, because I want to access the response object, not the request.

Thanks.

like image 319
BZoli Avatar asked Apr 18 '26 21:04

BZoli


1 Answers

Just copy entend answer

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>
like image 74
Georgy Gobozov Avatar answered Apr 20 '26 12:04

Georgy Gobozov