Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 1993bec

Browse files
committed
Updated the Token Management section of the tutorial.
1 parent 6a3bc15 commit 1993bec

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

docs/source/tutorial.rst

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,11 @@ will be able to get to the ``/restricted`` page.
741741
Token Management
742742
----------------
743743

744-
The code for this section can be found in `tutorials/spring-boot/05-token-management <https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/05-token-management>`_.
744+
#if( $springboot )
745+
The code for this section can be found in `tutorials/spring-boot/05-token-management`_.
746+
#elseif( $spring )
747+
The code for this section can be found in `tutorials/spring/05-token-management`_.
748+
#end
745749

746750
The Java SDK supports `oauth2 <http://oauth.net/2/>`_ workflows for obtaining and interacting with access tokens and
747751
refresh tokens. The Token Management feature is included "out of the box" and is used via the `/oauth/token` endpoint.
@@ -751,23 +755,33 @@ Spring Security (with and without WebMVC).
751755

752756
This part of the tutorial exercises the Token Magement features using Spring Security Spring Boot WebMVC.
753757

754-
There's a simple `@RestController` called `UserDetailsController` that returns information about the authenticated account.
758+
There's a simple ``@RestController`` called ``UserDetailsController`` that returns information about the authenticated account.
755759

756760
.. code-block:: java
761+
:linenos:
757762
758763
@RestController
759764
public class UserDetailsController {
765+
766+
private AccountResolver accountResolver;
767+
768+
@Autowired
769+
public UserDetailsController(AccountResolver accountResolver) {
770+
Assert.notNull(accountResolver);
771+
this.accountResolver = accountResolver;
772+
}
773+
760774
@RequestMapping(value="/userdetails", produces = MediaType.APPLICATION_JSON_VALUE)
761775
public AccountInfo info(HttpServletRequest req) {
762776
// must be logged in to get here per Spring Security config
763-
Account account = AccountResolver.INSTANCE.getAccount(req);
777+
Account account = accountResolver.getAccount(req);
764778
765779
return new AccountInfo(account.getEmail(), account.getFullName(), account.getHref());
766780
}
767781
}
768782
769-
In order to hit the `/userdetails` endpoint, we'll first, we'll get an `access_token` and a `refresh_token` by hitting the
770-
`/oauth/token` endpoint:
783+
In order to hit the ``/userdetails`` endpoint, we'll first, we'll get an ``access_token`` and a ``refresh_token`` by hitting the
784+
``/oauth/token`` endpoint:
771785

772786
.. code-block:: bash
773787
@@ -785,22 +799,22 @@ You will get back a response that looks something like this:
785799
.. code-block:: javascript
786800
787801
{
788-
"access_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI2M1laa1FBNjRTdEdUQjFhVEhlNGdPIiwiaWF0IjoxNDU0NDM4MTQ3LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU0NDQxNzQ3LCJydGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIn0.-3NNpi7-DTvl2VNCfHHFNwWVikmeCyNPy6KEu--XYjk",
789-
"refresh_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIiwiaWF0IjoxNDU0NDM4MTQ3LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU5NjIyMTQ3fQ.yK5twgj3-v51z4pszKXWTX9VtCbs1KxQU4vH1eXvgGo",
802+
"access_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ...",
803+
"refresh_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ...",
790804
"token_type":"Bearer",
791805
"expires_in":3600
792806
}
793807
794808
795-
The response includes the tokens as well as information on their type (`Bearer` in this case) and when it expires.
809+
The response includes the tokens as well as information on their type (``Bearer`` in this case) and when it expires.
796810

797-
We can now use the `access_token` to hit the `/userdetails` endpoint:
811+
We can now use the ``access_token`` to hit the ``/userdetails`` endpoint:
798812

799813

800814
.. code-block:: bash
801815
802816
curl \
803-
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI2M1laa1FBNjRTdEdUQjFhVEhlNGdPIiwiaWF0IjoxNDU0NDM4MTQ3LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU0NDQxNzQ3LCJydGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIn0.-3NNpi7-DTvl2VNCfHHFNwWVikmeCyNPy6KEu--XYjk" \
817+
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ..." \
804818
http://localhost:${port}/userdetails
805819
806820
You will get a response like this:
@@ -815,52 +829,53 @@ You will get a response like this:
815829
816830
Refresh tokens are used to obtain a new access token. This is useful when you want to allow your users to have a longer
817831
lived session - such as in a mobile application - but you still want to maintain control over how the session is
818-
managed. Your application could automatically use the `refresh_token` to obtain a new `access_token` when the
819-
`access_token` expires. With this approach, you could revoke the user's `access_token` and they would be kicked out of
820-
the system sooner because the `access_token` is short lived.
832+
managed. Your application could automatically use the ``refresh_token`` to obtain a new ``access_token`` when the
833+
``access_token`` expires. With this approach, you could revoke the user's ``refresh_token`` and they would be kicked out of
834+
the system sooner because the ``access_token`` is short lived. In this scenario, the next time the ``access_token`` expired,
835+
the ``refresh_token`` would be rejected when trying to get a new ``access_token``.
821836

822-
Let's use the `refresh_token` above to get a new `access_token`:
837+
Let's use the ``refresh_token`` above to get a new ``access_token``:
823838

824839
.. code-block:: bash
825840
826841
curl -v -X POST \
827842
-H "Origin: http://localhost:${port}" \
828843
-H "Content-Type: application/x-www-form-urlencoded" \
829-
-d "grant_type=refresh_token&refresh_token=eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIiwiaWF0IjoxNDU0NDM4MTQ3LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU5NjIyMTQ3fQ.yK5twgj3-v51z4pszKXWTX9VtCbs1KxQU4vH1eXvgGo" \
844+
-d "grant_type=refresh_token&refresh_token=eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ..." \
830845
http://localhost:${port}/oauth/token
831846
832-
Notice that in this case the `grant_type` is `refresh_token` and that we are using the `refresh_token` that we obtained
847+
Notice that in this case the ``grant_type`` is ``refresh_token`` and that we are using the ``refresh_token`` that we obtained
833848
previously.
834849

835850
You will get a response like this:
836851

837852
.. code-block:: javascript
838853
839854
{
840-
"access_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI1eDlxbWlES2U0RmlFMU02alhLSDBMIiwiaWF0IjoxNDU0NDQ0MTU1LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU0NDQ3NzU1LCJydGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIn0.J2NR7MV3OoolYImfUNiu8SCDvaQdresHTnPHgL7mO1Q",
841-
"refresh_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIiwiaWF0IjoxNDU0NDM4MTQ3LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU5NjIyMTQ3fQ.yK5twgj3-v51z4pszKXWTX9VtCbs1KxQU4vH1eXvgGo",
855+
"access_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ...",
856+
"refresh_token":"eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ...",
842857
"token_type":"Bearer",
843858
"expires_in":3600
844859
}
845860
846-
While the `refresh_token` is the same, we get a new `access_token`.
861+
While the ``refresh_token`` is the same, we get a new ``access_token``.
847862

848-
By default, when you logout, both the `access_token` and the `refresh_token` will be revoked. Let's see this in action:
863+
By default, when you logout, both the ``access_token`` and the ``refresh_token`` will be revoked. Let's see this in action:
849864

850865
.. code-block:: bash
851866
852-
curl -v \
853-
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI1eDlxbWlES2U0RmlFMU02alhLSDBMIiwiaWF0IjoxNDU0NDQ0MTU1LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU0NDQ3NzU1LCJydGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIn0.J2NR7MV3OoolYImfUNiu8SCDvaQdresHTnPHgL7mO1Q" \
867+
curl -v -X POST \
868+
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ..." \
854869
http://localhost:${port}/logout
855870
856-
Now, if you attempt to use the `access_token` again, you will not be granted access as it's been invalidated. You will
871+
Now, if you attempt to use the ``access_token`` again, you will not be granted access as it's been invalidated. You will
857872
need to login again.
858873

859874

860875
.. code-block:: bash
861876
862877
curl \
863-
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ.eyJqdGkiOiI1eDlxbWlES2U0RmlFMU02alhLSDBMIiwiaWF0IjoxNDU0NDQ0MTU1LCJpc3MiOiJodHRwczovL2FwaS5zdG9ybXBhdGguY29tL3YxL2FwcGxpY2F0aW9ucy82dkZUNEFSZldDbXVIVlY4Vmt0alRvIiwic3ViIjoiaHR0cHM6Ly9hcGkuc3Rvcm1wYXRoLmNvbS92MS9hY2NvdW50cy80V1NjTWJBbm8zVjk1aWlTc3dralBYIiwiZXhwIjoxNDU0NDQ3NzU1LCJydGkiOiI2M1laa01xMTlzYUhxTHZqSDFtbzRLIn0.J2NR7MV3OoolYImfUNiu8SCDvaQdresHTnPHgL7mO1Q" \
878+
-H "Authorization: Bearer eyJraWQiOiJSOTJTQkhKQzFVNERBSU1HUTNNSE9HVk1YIiwiYWxnIjoiSFMyNTYifQ..." \
864879
http://localhost:${port}/userdetails
865880
866881
Here's the response:
@@ -873,7 +888,7 @@ Here's the response:
873888
}
874889
875890
As you can see from the examples above, Stormpath provides powerful oauth2 Token Management out-of-the-box using the
876-
`/oauth/token` endpoint. There is no additional coding required on your part to make use of the Token Management
891+
``/oauth/token`` endpoint. There is no additional coding required on your part to make use of the Token Management
877892
feature.
878893

879894
.. _wrapping-up:
@@ -901,4 +916,6 @@ for more information on all that the Stormpath Java SDK has to offer.
901916
.. _tutorials/spring-boot/03-spring-security-refined: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/03-spring-security-refined
902917
.. _tutorials/spring/03-spring-security-refined: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/03-spring-security-refined
903918
.. _tutorials/spring-boot/04-a-finer-grain-of-control: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/04-a-finer-grain-of-control
904-
.. _tutorials/spring/04-a-finer-grain-of-control: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/04-a-finer-grain-of-control
919+
.. _tutorials/spring/04-a-finer-grain-of-control: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/04-a-finer-grain-of-control
920+
.. _tutorials/spring-boot/05-token-management: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring-boot/05-token-management
921+
.. _tutorials/spring/05-token-management: https://github.com/stormpath/stormpath-sdk-java/tree/master/tutorials/spring/05-token-management

0 commit comments

Comments
 (0)