0

I'm using the following config in web.xml to redirect to error pages

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

when 401 error occurs it is redirecting to '/' but the URI is preserved.

let's say if I'm trying to access 'localhost:8080/account' resource which needs auth, then the redirect is happening but URL is still 'localhost:8080/account'.

I was wondering how to redirect to '/' and have the url to be 'localhost:8080/'

2 Answers2

0

You want to tell the client that localhost:8080/account does not exist, so this resource should report a 404 status. Why should it do a redirect to the root directory? The root directory exists, so redirecting to the root and returning a 404 would be just wrong.

The error-page directive just tells tomcat where to pull a custom HTML style for your 404 pages from. The client should NOT be redirected to other resources, because - as already stated - that would alter the semantics alltogether.

Mick
  • 954
  • 7
  • 17
  • sure, but in case of 401 I would like to redirect to '/' which points to my angular app. The default route on my angular app is login. – Shashank Raju Feb 13 '18 at 20:06
  • The problem is that when the URI is preserved, the index.html file is being served, but the index.html is making wrong requests for the '.js' files. The requests are going as "localhost/account/script.js" instead of localhost/script.js I was thinking maybe redirect to a jsp page on 401 and have redirect logic there – Shashank Raju Feb 13 '18 at 20:22
0

The <error-page> directives set-up in-process forwards, not client-redirects (which would be 3xx responses).

If you want to convert a 401 into a 302, then set your <error-page> to point to a JSP that changes the response code to 302 with a Location response header.

Note that changing a 401 response to a 302 response (which will probably end up with a 200 when the redirect is followed) kind of defeats the purpose of having the 401 response in the first place.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77