@@ -2617,6 +2617,49 @@ defined as annotations:
2617
2617
:doc: `another way to enforce HTTP or HTTPS </security/force_https >`
2618
2618
via the ``requires_channel `` setting.
2619
2619
2620
+ Signing URIs
2621
+ ~~~~~~~~~~~~
2622
+
2623
+ A signed URI is an URI that includes a hash value that depends on the contents of
2624
+ the URI. This way, you can later check the integrity of the signed URI by
2625
+ recomputing its hash value and comparing it with the hash included in the URI.
2626
+
2627
+ Symfony provides a utility to sign URIs via the :class: `Symfony\\ Component\\ HttpKernel\\ UriSigner `
2628
+ service, which you can inject in your services or controllers::
2629
+
2630
+ // src/Service/SomeService.php
2631
+ namespace App\Service;
2632
+
2633
+ use Symfony\Component\HttpKernel\UriSigner;
2634
+
2635
+ class SomeService
2636
+ {
2637
+ public function __construct(
2638
+ private UriSigner $uriSigner,
2639
+ ) {
2640
+ }
2641
+
2642
+ public function someMethod()
2643
+ {
2644
+ // ...
2645
+
2646
+ // generate a URL youself or get it somehow...
2647
+ $url = 'https://example.com/foo/bar?sort=desc';
2648
+
2649
+ // sign the URL (it adds a query parameter called '_hash')
2650
+ $signedUrl = $this->uriSigner->sign($url);
2651
+ // $url = 'https://example.com/foo/bar?sort=desc&_hash=e4a21b9'
2652
+
2653
+ // check the URL signature
2654
+ $uriSignatureIsValid = $this->uriSigner->check($signedUrl);
2655
+ // $uriSignatureIsValid = true
2656
+
2657
+ // if you have access to the current Request object, you can use this
2658
+ // other method to pass the entire Request object instead of the URI:
2659
+ $uriSignatureIsValid = $this->uriSigner->checkRequest($request);
2660
+ }
2661
+ }
2662
+
2620
2663
Troubleshooting
2621
2664
---------------
2622
2665
0 commit comments