Package ch.tocco.nice2.web.core.api.auth
Class AuthenticatedServlet
java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
ch.tocco.nice2.web.core.api.auth.SecureServlet
ch.tocco.nice2.web.core.api.auth.AuthenticatedServlet
- All Implemented Interfaces:
jakarta.servlet.Servlet,jakarta.servlet.ServletConfig,Serializable
Base class for servlets that want to have each method run by an authenticated user. If a request to this servlet is made by anonymous user, the client is requested to authenticate using HTTP Basic authentication.
- See Also:
-
Field Summary
Fields inherited from class ch.tocco.nice2.web.core.api.auth.SecureServlet
runEnvironmentServiceFields inherited from class jakarta.servlet.http.HttpServlet
LEGACY_DO_HEAD -
Constructor Summary
ConstructorsConstructorDescriptionAuthenticatedServlet(RunEnvironmentService runEnvironmentService, SecureSettings settings) -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanauthenticate(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) Checks whether there is a valid (non-anonymous) principal on the thread.protected PrincipalGets the current non-anonymous principal from the thread.protected abstract StringgetRealm()Returns a string that names the authentication realm used for http basic authentication.protected voidservice(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) voidsetUnauthorizedPath(String unauthorizedPath) Sets the path that is redirected to, if authentication fails.Methods inherited from class ch.tocco.nice2.web.core.api.auth.SecureServlet
checkSecuredConnectionMethods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, init, serviceMethods inherited from class jakarta.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Constructor Details
-
AuthenticatedServlet
-
-
Method Details
-
setUnauthorizedPath
Sets the path that is redirected to, if authentication fails.- Parameters:
unauthorizedPath- the path to redirect on authentication error
-
getRealm
Returns a string that names the authentication realm used for http basic authentication. -
service
protected void service(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws jakarta.servlet.ServletException, IOException - Overrides:
servicein classSecureServlet- Throws:
jakarta.servlet.ServletExceptionIOException
-
getCurrentPrincipal
Gets the current non-anonymous principal from the thread.- Returns:
- current non-anonymous principal
- Throws:
IllegalStateException- if no principal if found
-
authenticate
protected boolean authenticate(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) throws IOException, jakarta.servlet.ServletException Checks whether there is a valid (non-anonymous) principal on the thread. If not, it redirects the request to use http basic authentication.- Parameters:
req- the requestresp- the response- Returns:
trueif succesfully authenticated,falseotherwise- Throws:
IOExceptionjakarta.servlet.ServletException
-