javaservletsoauth-2.0oltucallbackurl

Apache Oltu OAuth2.0 Callback Handling


I am creating a Maven project using Apache Oltu, Java servlets, and JSPs that requires OAuth2 Authentication in order to access an API. So far I am able to be redirected to the website and authorize access, however, when the website redirects back to my callback URL I get a pop up alert containing my callback URL appended with the authorization code paramter that says 'Page load failed with error: Could not connect to server.'

Is this due to me mapping out my url incorrectly? Or am I missing something else? I'm trying to redirect the user to apiConnector.jsp after the authorization code has been exchanged for an access token.

My servlet to handle the callback:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;

import OAuth2.main.OAuthUtils;
import OAuth2.model.OAuth2Details;

/**
 * Servlet implementation class AuthCallbackController
 */
@WebServlet("/AuthCallbackController")
public class AuthCallbackController extends HttpServlet {
    private OAuth2Details authDetails;
    private static final long serialVersionUID      = 1L;
    private static final String LIST_DATA           = "/apiConnector.jsp";
    public static final String CODE_URL_PARAM_NAME  = "code";
    public static final String ERROR_URL_PARAM_NAME = "error";
    public static final String URL_MAPPING          = "/OAuth2Callback";
    public static final String REDIRECT_URL         = "/";


public AuthCallbackController() {
    super();
    authDetails = OAuthUtils.createOAuth2Details();
}

/**
 * Handles the callback response from Higi Authorization Server
 */
protected void doGet (HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    String forward = LIST_DATA;     
    try {

        OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(servletRequest);
        String code = oar.getCode();


        OAuthClientRequest request = OAuthClientRequest
                .tokenLocation(authDetails.getTokenEndpoint())
                .setGrantType(GrantType.AUTHORIZATION_CODE)
                .setClientId(authDetails.getClientId())
                .setClientSecret(authDetails.getClientSecret())
                .setRedirectURI(authDetails.getRedirectURI())
                .setCode(code)
                .buildQueryMessage();           

        OAuthClient oAuthClient = new OAuthClient (new URLConnectionClient());
        GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);

        String accessToken  = oAuthResponse.getAccessToken();
        String refreshToken = oAuthResponse.getRefreshToken();
        Long expiresIn      = oAuthResponse.getExpiresIn();


        OAuthResponse r = OAuthASResponse
                .tokenResponse(HttpServletResponse.SC_OK)
                .setAccessToken(accessToken)
                .setExpiresIn("3600")
                .setRefreshToken(refreshToken)
                .buildJSONMessage();

        servletResponse.setStatus(r.getResponseStatus());
        PrintWriter pw = servletResponse.getWriter();
        pw.print(r.getBody());
        pw.flush();
        pw.close();

        // Forward attributes to page
        RequestDispatcher view = servletRequest.getRequestDispatcher(forward);
        servletRequest.setAttribute("response",r.getBody());
        view.forward(servletRequest, servletResponse);

    } catch (OAuthProblemException | OAuthSystemException ex) {
        ex.printStackTrace();
    }

}


}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <display-name>HigiSalesforceConnector</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>APIConnectorCallbackHandlerServlet</servlet-name>
    <servlet-class>OAuth2.controller.AuthCallbackController</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>APIConnectorCallbackHandlerServlet</servlet-name>
    <url-pattern>/OAuth2Callback/*</url-pattern>
  </servlet-mapping>
</web-app>

Solution

  • After doing a lot more research, I was able to solve my problem-- ended up having to create a new server on Eclipse that used my updated Tomcat configurations for SSL.