Overview

You will learn: how to create a new app with a login to display a layer that is not accessible to the public.

When you don't want to share content with everyone, the ArcGIS Runtime SDK for Java and ArcGIS Online provide a robust security framework. We recommend developers utilize OAuth2 instead of handling user credentials when an app requires a login.

In this lab you will register your application in advance and indicate where to redirect users after OAuth login. You will add a private layer and real-time traffic data to your map that requires authentication to access it. Later on, we verify that information when you ask them to sign in directly to arcgis.com.

Before you begin

Reuse the starter project

In a new or empty folder, make a copy of the Create a basic Java project.

If you have not done the starter project lab, be sure to review it first so you are familiar with running the app.

Steps

Update the project

  1. Open the settings.gradle file in an editor of your choice and change the rootProject.name value to access-private-layers.

Configure OAuth2 for your app

Register your app to generate a client id and set a redirect URI to access private layers:

  1. Sign into ArcGIS for Developers.

    1. Select drop-down menu Dashboard > New Application to create a new application.

      • Title: Access Private Layer App
      • Tags: OAuth
      • Description: Access Private Layer App
      • Click Register New Application
    2. At the top, click on the tab Authentication and under Redirect URLs add the following.

      • Redirect URL: urn:ietf:wg:oauth:2.0:oob
    3. Make note of your Client ID as you'll need this in the next step.

Imports

  1. Open the App.java file in src/main/java/com/arcgis/developers/labs. Then merge the following with the existing imports.
    import java.net.MalformedURLException;
    import javafx.scene.control.Alert;
    import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
    import com.esri.arcgisruntime.loadable.LoadStatus;
    import com.esri.arcgisruntime.mapping.Viewpoint;
    import com.esri.arcgisruntime.portal.Portal;
    import com.esri.arcgisruntime.security.AuthenticationManager;
    import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
    import com.esri.arcgisruntime.security.OAuthConfiguration;
    

Configure OAuth in the app

  1. Create a new method to encapsulate authentication. Set up information to create an OAuthConfiguration which will prompt the user for credentials directly from arcgis.com.

    private void setupAuthentication() {
        String portalURL = "https://www.arcgis.com";
        String clientId = "YOUR_CLIENT_ID";
        String redirectURI = "urn:ietf:wg:oauth:2.0:oob";
    }
    
  2. Create an OAuthConfiguration and assign it to the AuthenticationManager, which will invoke this authentication when private access is needed.

         String redirectURI = "urn:ietf:wg:oauth:2.0:oob";
    
         /* ** ADD ** */
         try {
           OAuthConfiguration oAuthConfiguration = new OAuthConfiguration(portalURL, clientId, redirectURI);
           AuthenticationManager.setAuthenticationChallengeHandler(new DefaultAuthenticationChallengeHandler());
           AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    
         } catch (MalformedURLException e) {
           e.printStackTrace();
         }
    
  3. Create a portal to access a private layer. Once the portal loads, call addTrafficLayer to load the traffic layer on the map.

     AuthenticationManager.addOAuthConfiguration(oAuthConfiguration);
    
     /* ** ADD ** */
     final Portal portal = new Portal(portalURL, true);
     portal.addDoneLoadingListener(() -> {
       if (portal.getLoadStatus() == LoadStatus.LOADED) {
     	  //addTrafficLayer(portal);
       } else {
         new Alert(Alert.AlertType.ERROR, "Portal: " + portal.getLoadError().getMessage()).show();
       }
     });
     portal.loadAsync();
    
  4. Update the start method to call setupAuthentication:

      setupMap();
    
      /* ** ADD ** */
      setupAuthentication();
    

Add a private layer to the map's operational layers

  1. Create a new method to setup and add the traffic layer to the map as an operational layer.

    private void addTrafficLayer(Portal portal) {
        String trafficURL = "https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer";
        ArcGISMapImageLayer layer = new ArcGISMapImageLayer(trafficURL);
        layer.addDoneLoadingListener(() -> {
            if (portal.getLoadStatus() != LoadStatus.LOADED) {
                new Alert(Alert.AlertType.ERROR, "Layer: " + layer.getLoadError().getMessage()).show();
            }
        });
        mapView.getMap().getOperationalLayers().add(layer);
    }
    
  2. Update the setupAuthentication method to comment in addTrafficLayer(portal):

      if (portal.getLoadStatus() == LoadStatus.LOADED) {
    	  addTrafficLayer(portal);
      } else {
    

Congratulations, you're done!

When your application opens it will show a dialog from ArcGIS Online asking for OAuth login. Once successfully logged in you return to the application with the traffic layer added.

Challenge

Explore basemaps

Try different basemaps available with BasemapType. Now that you are able to authenticate users, you could also define private basemaps on your ArcGIS Online account and load those in your apps. Try it!

Discover layers

Discover other layers you can use in your app. Explore ArcGIS Open Data and Los Angeles GeoHub for data you can use in your apps.