public abstract class AbstractAuthorizationCodeCallbackServlet
extends jakarta.servlet.http.HttpServlet
This is designed to simplify the flow in which an end-user authorizes your web application to
access their protected data. The main servlet class extends AbstractAuthorizationCodeServlet which if the end-user credentials are not found, will redirect
the end-user to an authorization page. If the end-user grants authorization, they will be
redirected to this servlet that extends AbstractAuthorizationCodeCallbackServlet and the
onSuccess(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, com.google.api.client.auth.oauth2.Credential) will be called. Similarly, if the end-user grants authorization, they will be
redirected to this servlet and onError(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) will be called.
Sample usage:
public class ServletCallbackSample extends AbstractAuthorizationCodeCallbackServlet {
@Override
protected void onSuccess(HttpServletRequest req, HttpServletResponse resp, Credential credential)
throws ServletException, IOException {
resp.sendRedirect("/");
}
@Override
protected void onError(
HttpServletRequest req, HttpServletResponse resp, AuthorizationCodeResponseUrl errorResponse)
throws ServletException, IOException {
// handle error
}
@Override
protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
GenericUrl url = new GenericUrl(req.getRequestURL().toString());
url.setRawPath("/oauth2callback");
return url.build();
}
@Override
protected AuthorizationCodeFlow initializeFlow() throws IOException {
return new AuthorizationCodeFlow.Builder(BearerToken.authorizationHeaderAccessMethod(),
new NetHttpTransport(),
new GsonFactory(),
new GenericUrl("https://server.example.com/token"),
new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"),
"s6BhdRkqt3",
"https://server.example.com/authorize").setCredentialStore(
new JdoCredentialStore(JDOHelper.getPersistenceManagerFactory("transactions-optional")))
.build();
}
@Override
protected String getUserId(HttpServletRequest req) throws ServletException, IOException {
// return user ID
}
}
| Constructor and Description |
|---|
AbstractAuthorizationCodeCallbackServlet() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
doGet(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp) |
protected abstract String |
getRedirectUri(jakarta.servlet.http.HttpServletRequest req)
Returns the redirect URI for the given HTTP servlet request.
|
protected abstract String |
getUserId(jakarta.servlet.http.HttpServletRequest req)
Returns the user ID for the given HTTP servlet request.
|
protected abstract AuthorizationCodeFlow |
initializeFlow()
Loads the authorization code flow to be used across all HTTP servlet requests (only called
during the first HTTP servlet request with an authorization code).
|
protected void |
onError(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp,
AuthorizationCodeResponseUrl errorResponse)
Handles an error to the authorization, such as when an end user denies authorization.
|
protected void |
onSuccess(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp,
Credential credential)
Handles a successfully granted authorization.
|
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, servicepublic AbstractAuthorizationCodeCallbackServlet()
protected final void doGet(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp)
throws jakarta.servlet.ServletException,
IOException
doGet in class jakarta.servlet.http.HttpServletjakarta.servlet.ServletExceptionIOExceptionprotected abstract AuthorizationCodeFlow initializeFlow() throws jakarta.servlet.ServletException, IOException
jakarta.servlet.ServletExceptionIOExceptionprotected abstract String getRedirectUri(jakarta.servlet.http.HttpServletRequest req) throws jakarta.servlet.ServletException, IOException
jakarta.servlet.ServletExceptionIOExceptionprotected abstract String getUserId(jakarta.servlet.http.HttpServletRequest req) throws jakarta.servlet.ServletException, IOException
jakarta.servlet.ServletExceptionIOExceptionprotected void onSuccess(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp,
Credential credential)
throws jakarta.servlet.ServletException,
IOException
Default implementation is to do nothing, but subclasses should override and implement. Sample implementation:
resp.sendRedirect("/granted");
req - HTTP servlet requestresp - HTTP servlet responsecredential - credentialjakarta.servlet.ServletException - HTTP servlet exceptionIOException - some I/O exceptionprotected void onError(jakarta.servlet.http.HttpServletRequest req,
jakarta.servlet.http.HttpServletResponse resp,
AuthorizationCodeResponseUrl errorResponse)
throws jakarta.servlet.ServletException,
IOException
Default implementation is to do nothing, but subclasses should override and implement. Sample implementation:
resp.sendRedirect("/denied");
req - HTTP servlet requestresp - HTTP servlet responseerrorResponse - error response (AuthorizationCodeResponseUrl.getError() is not
null)jakarta.servlet.ServletException - HTTP servlet exceptionIOException - some I/O exceptionCopyright © 2011–2025 Google. All rights reserved.