Skip to content

Commit 494462c

Browse files
authored
Token authentication (#347)
1 parent 1ac7561 commit 494462c

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<h1>Token authentication</h1>
2+
3+
<p>Access a map service that is secured with an ArcGIS token-based authentication.</p>
4+
5+
<p><img src="TokenAuthentication.png"/></p>
6+
7+
<h2>Use case</h2>
8+
9+
<p>Applications often require accessing data from private map services on remote servers. A token authentication system can be used to allow app users who hold a valid username and password to access the remote service. </p>
10+
11+
<h2>How to use the sample</h2>
12+
13+
<p>When starting the sample, the user is challenged for an ArcGIS Online login to view the protected map service. Enter a username and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). Upon successful authentication, the protected map service will display in the map.</p>
14+
15+
<h2>How it works</h2>
16+
17+
<ol>
18+
<li>Create an <code>AuthenticationChallengeHandler</code> using the <code>DefaultAuthenticationChallengeHandler</code> to handle the challenges sent by the protected map service.</li>
19+
<li>Set the <code>AuthenticationChallengeHandler</code> used by the <code>AuthenticationManager</code>.</li>
20+
<li>Create a <code>Portal</code> to ArcGIS Online.</li>
21+
<li>Create a <code>PortalItem</code> for the protected web map using the <code>Portal</code> and Item ID of the protected map service as parameters</li>
22+
<li>Create an <code>ArcGISMap</code> from the portal item, and display it in a <code>MapView</code></li>
23+
<li>Set the map to display in the map view.</li>
24+
</ol>
25+
26+
<h2>Relevant API</h2>
27+
28+
<ul>
29+
<li>AuthenticationChallengeHandler</li>
30+
<li>AuthenticationManager</li>
31+
<li>DefaultAuthenticationChallengeHandler</li>
32+
<li>Map</li>
33+
<li>MapView</li>
34+
<li>Portal</li>
35+
<li>PortalItem</li>
36+
</ul>
37+
38+
<h2>Tags</h2>
39+
40+
<p>authentication, map service, security, token</p>
409 KB
Loading
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2019 Esri
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.esri.samples.portal.token_authentication;
18+
19+
import javafx.application.Application;
20+
import javafx.scene.Scene;
21+
import javafx.scene.layout.StackPane;
22+
import javafx.stage.Stage;
23+
24+
import com.esri.arcgisruntime.mapping.ArcGISMap;
25+
import com.esri.arcgisruntime.mapping.view.MapView;
26+
import com.esri.arcgisruntime.portal.Portal;
27+
import com.esri.arcgisruntime.portal.PortalItem;
28+
import com.esri.arcgisruntime.security.AuthenticationChallengeHandler;
29+
import com.esri.arcgisruntime.security.AuthenticationManager;
30+
import com.esri.arcgisruntime.security.DefaultAuthenticationChallengeHandler;
31+
32+
public class TokenAuthenticationSample extends Application {
33+
34+
private MapView mapView;
35+
36+
@Override
37+
public void start(Stage stage) {
38+
39+
try {
40+
// create stack pane and application scene
41+
StackPane stackPane = new StackPane();
42+
Scene scene = new Scene(stackPane);
43+
44+
// set title, size, and add scene to stage
45+
stage.setTitle("Token Authentication Sample");
46+
stage.setWidth(800);
47+
stage.setHeight(700);
48+
stage.setScene(scene);
49+
stage.show();
50+
51+
// set up an authentication handler to take credentials for access to the protected map service
52+
AuthenticationChallengeHandler authenticationChallengeHandler = new DefaultAuthenticationChallengeHandler();
53+
AuthenticationManager.setAuthenticationChallengeHandler(authenticationChallengeHandler);
54+
55+
// create a portal to ArcGIS Online
56+
Portal portal = new Portal("https://www.arcgis.com/");
57+
58+
// create a portal item using the portal and the item id of a protected map service
59+
PortalItem portalItem = new PortalItem(portal, "e5039444ef3c48b8a8fdc9227f9be7c1");
60+
61+
// create a map with the portal item
62+
ArcGISMap map = new ArcGISMap(portalItem);
63+
64+
// set the map to be displayed in the map view
65+
mapView = new MapView();
66+
mapView.setMap(map);
67+
68+
// add the map view to stack pane
69+
stackPane.getChildren().add(mapView);
70+
71+
} catch (Exception e) {
72+
// on any error, display the stack trace.
73+
e.printStackTrace();
74+
}
75+
}
76+
77+
/**
78+
* Stops and releases all resources used in application.
79+
*/
80+
@Override
81+
public void stop() {
82+
83+
if (mapView != null) {
84+
mapView.dispose();
85+
}
86+
}
87+
88+
/**
89+
* Opens and runs application.
90+
*
91+
* @param args arguments passed to this application
92+
*/
93+
public static void main(String[] args) {
94+
95+
Application.launch(args);
96+
}
97+
}

0 commit comments

Comments
 (0)