N
N
nuclear_kote2016-06-09 13:25:23
Java
nuclear_kote, 2016-06-09 13:25:23

Is it possible to change tomcat's 401 response?

You need to add some headers to the 401 error. Is it possible to do this somehow?
Tried with filter:

<filter>
        <filter-name>Filter</filter-name>
        <filter-class>com.example.filter.Filter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

but there is no recourse to him at all.
I am using BASIC authentication.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danila, 2016-06-09
@nuclear_kote

If I understand correctly, then in web.xml add

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

After that, everyone with 401 will try to be redirected to their page with an error, however, since they are not authorized, they will not get access, for this it is worth
adding to the jsp:
response.addHeader("WWW-Authenticate", "BASIC realm=\"YOUR-REALM\"");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

where the name for realm is the same as the one in web.xml

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question