Tato otázka pravděpodobně nepatří jen jedné vývojářské skupině, ale implementace Esri-Leaflet je konečným cílem a pravděpodobně má vývojáře nejlépe obeznámené se stackem - takže tady to je:<\/P>
<\/P>
Mám samostatný ArcGIS server a chci přistupovat k zabezpečeným feature službám. Můj web běží na PHP 7.3 a má přístup k cURL. Ideálně bych chtěl získat token pomocí přihlašovacích údajů uložených v PHP, pomocí cURL, a pak tento token předat Esri-Leaflet při volání addtomap, čímž bych udržel přihlašovací údaje v bezpečí. <\/P>
Chyba, které čelím v odpovědi na požadavek, je poměrně obecná:<\/P>
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE> <\/P>Dočasně je zde url:<\/P>http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Co jsem zatím udělal? Použijme zabezpečenou esri demo službu pro ilustraci, přičemž jsem nechal zakomentované části, kde jsem buď:<\/P>Snažil se nastavit referer ručně tak, aby cURL používal stejný referer jako ten, který je předáván ze stránky (doufejme). To zahrnovalo stránku volající mapu, jak je vidět při pohledu na referer požadavků v Chrome, a také zajištění, že referer vypisovaný PHP odpovídá tomu, který Chrome posílá. Celkově nechci tento přístup používat, protože bych chtěl mít více stránek používajících tento kód PHP.<\/LI>Snažil se místo toho použít IP adresy návštěvníků. To vygenerovalo token, ale vedlo ke stejné chybě invalid token. Moje organizace může také směrovat požadavky z různých koncových bodů, takže je to možný zdroj nestability.<\/LI>Snažil se nastavit referer pomocí jiných PHP metod jako $_SERVER['HTTP_REFERER'];<\/LI>Můžete také vidět můj regex pro parsování tokenů - ten pouze odstraní zbytek odpovědi tak, že zůstane pouze řetězec tokenu. {".,}. Tedy nepředávám expiraci zpět serveru během požadavku vrstvy - v žádné dokumentaci nevidím, že by to bylo vyžadováno.<\/LI><\/UL> <\/P> <\/P> <?php
{"error":{"code":498,"message":"Invalid Token","details":[]}}<\/P><\/BLOCKQUOTE>
Dočasně je zde url:<\/P>
http:\/\/52.65.144.150\/esri-js-demo-page\/<\/A><\/P> <\/P>Co jsem zatím udělal? Použijme zabezpečenou esri demo službu pro ilustraci, přičemž jsem nechal zakomentované části, kde jsem buď:<\/P>Snažil se nastavit referer ručně tak, aby cURL používal stejný referer jako ten, který je předáván ze stránky (doufejme). To zahrnovalo stránku volající mapu, jak je vidět při pohledu na referer požadavků v Chrome, a také zajištění, že referer vypisovaný PHP odpovídá tomu, který Chrome posílá. Celkově nechci tento přístup používat, protože bych chtěl mít více stránek používajících tento kód PHP.<\/LI>Snažil se místo toho použít IP adresy návštěvníků. To vygenerovalo token, ale vedlo ke stejné chybě invalid token. Moje organizace může také směrovat požadavky z různých koncových bodů, takže je to možný zdroj nestability.<\/LI>Snažil se nastavit referer pomocí jiných PHP metod jako $_SERVER['HTTP_REFERER'];<\/LI>Můžete také vidět můj regex pro parsování tokenů - ten pouze odstraní zbytek odpovědi tak, že zůstane pouze řetězec tokenu. {".,}. Tedy nepředávám expiraci zpět serveru během požadavku vrstvy - v žádné dokumentaci nevidím, že by to bylo vyžadováno.<\/LI><\/UL> <\/P> <\/P> <?php
Co jsem zatím udělal? Použijme zabezpečenou esri demo službu pro ilustraci, přičemž jsem nechal zakomentované části, kde jsem buď:<\/P>
<?php
Fixed by treating the json object as json properly.
<SPAN class="delimiter token"><?php</SPAN> <SPAN class="comment token">//Establish our requesting url</SPAN> <SPAN class="keyword token">global</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">home_url</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="variable token">$wp</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">request</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Check to see if the request came from the proper application</SPAN> <SPAN class="comment token">//The url to the token endpoint of the server containing the secure service</SPAN> <SPAN class="variable token">$url</SPAN> <SPAN class="operator token">=</SPAN> "https<SPAN class="punctuation token">:</SPAN><SPAN class="comment token">//someplace.com/arcgis/tokens/";</SPAN> <SPAN class="comment token">//The fields included in the request</SPAN> <SPAN class="variable token">$fields</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">array</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'request'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'getToken'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'username'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'username1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'password'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'password1'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'expiration'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="number token">120</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'clientid'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'ref.'</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="variable token">$RequestingPage</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="string token">'/'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'f'</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="string token">'json'</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">foreach</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN> <SPAN class="keyword token">as</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="variable token">$fields_string</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="operator token">=</SPAN> <SPAN class="variable token">$key</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'='</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$value</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="string token">'&'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Instantiate Curl</SPAN> <SPAN class="variable token">$ch</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_init</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$url</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_SSL_VERIFYPEER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">false</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token constant">CURLOPT_POST</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="token function">count</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$fields</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_POSTFIELDS</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$fields_string</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_RETURNTRANSFER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">true</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="token function">curl_setopt</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token constant">CURLOPT_REFERER</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">$RequestingPage</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="variable token">$result</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//If Curl was not instantiated successfully</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN><SPAN class="token function">curl_exec</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'An error has occurred: '</SPAN> <SPAN class="punctuation token">.</SPAN> <SPAN class="token function">curl_error</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Otherwise return the result</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">//Treat the response as JSON, and split by attribute.</SPAN> <SPAN class="variable token">$someObject</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">json_decode</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$result</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"><</SPAN>script</SPAN> <SPAN class="attr-name token">type</SPAN><SPAN class="attr-value token"><SPAN class="punctuation token">=</SPAN><SPAN class="punctuation token">"</SPAN>text/javascript<SPAN class="punctuation token">"</SPAN></SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">'var val = "'</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="variable token">$someObject</SPAN><SPAN class="operator token">-</SPAN><SPAN class="operator token">></SPAN><SPAN class="property token">token</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="string token">'"'</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> '<SPAN class="markup token"><SPAN class="token tag"><SPAN class="token tag"><SPAN class="punctuation token"></</SPAN>script</SPAN><SPAN class="punctuation token">></SPAN></SPAN></SPAN>'<SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">echo</SPAN> <SPAN class="string token">"\r\n"</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">//Close Curl</SPAN> <SPAN class="token function">curl_close</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="variable token">$ch</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="token function">add_action</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="string token">'wp_head'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ple_hook_javascript'</SPAN> <SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This works out for me. Thanks Simon
Glad it helped!
I guess what I didn't mention in my initial series of posts was that the script returns the variable "var" which contains the token. When collecting a layer with ESRI-Leaflet you can supply this token as part of the URL. Just for anyone else who happens upon this in the future.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.