Skip to content

Commit 0adde95

Browse files
Apply suggestions from code review
Co-authored-by: Deepu K Sasidharan <d4udts@gmail.com>
1 parent a581456 commit 0adde95

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

_source/_posts/2024-01-24-spring-boot-authorization.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@ Learn how to use Spring Boot, Java, and Auth0 to secure a feature-complete API,
2929

3030
In computer systems, authentication and authorization are part of a discipline called Identity and Access Management (IAM). [OAuth 2.0](https://www.rfc-editor.org/rfc/rfc6749.html) is a standard designed to authorize a website or application to access resources hosted by third-party services on behalf of a user. For web and mobile applications, an identity protocol was born in 2014, [OpenID Connect 1.0](https://openid.net/specs/openid-connect-core-1_0.html) (OIDC), a simple identity layer on top of OAuth 2.0, now widely adopted as part of the IAM strategy of many identity providers and identity clients on the internet.
3131

32-
For authorizing access to a protected resource, OAuth 2.0 uses Access Tokens, a piece of data, a string representing an authorization issued to the client, scopes and durations of the access, validated at the resource server. Briefly, the standard defines the roles of resource owner, resource server, client, and authorization server, and a protocol flow for the client requesting access, through an authorization server, to resources controlled by the resource owner and hosted by the resource server.
32+
For authorizing access to a protected resource, OAuth 2.0 uses Access Tokens. An access token is a piece of data representing an authorization issued to the client. It may also include scopes and durations of the access. The token is validated at the resource server. Briefly, the standard defines the roles of resource owner, resource server, client, and authorization server, and a protocol flow for the client requesting access, through an authorization server, to resources controlled by the resource owner and hosted by the resource server.
3333

3434
OpenID Connect provides authentication built on top of OAuth 2.0, and information about the authentication performed is returned in an ID Token with JWT format (JSON Web Token). In abstract, the protocol defines a client role or Relying Party that sends a request to the OpenID Provider, which in turn authenticates the end-user and obtains authorization, returning an ID Token and Access Token to the Relying Party.
3535

3636
## Authorization in a Spring Boot API
3737

3838
After the year 2020, in Buenos Aires, many bars and restaurants implemented a digital menu with a QR code that translates to a public document, for having an updated prices list. Prices change so often due to inflation, and updating a physical menu seems tedious and costly. Still, restaurant tables are managed with a software application, which includes a menu management module.
3939

40-
For learning purposes, let's assume you have built a Spring Boot menu API that must be secured, so only authorized users can perform requests to its endpoints. Now you are going to implement authorization for the API with OAuth2 2.0 and Auth0. Start by doing a checkout of the API repository, which already implements basic request handling:
40+
For learning purposes, let's assume you have built a Spring Boot menu API that must be secured, so only authorized users can perform requests to its endpoints. Now you are going to implement authorization for the API with OAuth 2.0 and Auth0. Start by doing a checkout of the API repository, which already implements basic request handling:
4141

4242
```shell
4343
git clone https://github.com/indiepopart/spring-menu-api.git
4444
```
4545

4646
The repository contains two project folders, `start` and `demo`. The bare bones menu API is a Gradle project in the `start` folder, open it with your favorite IDE. If you would rather skip the step-by-step security configuration and just run the final `demo` project, follow the instructions in the [README](https://github.com/indiepopart/spring-menu-api).
4747

48-
Sign up at [Auth0](https://auth0.com/signup) and install the [Auth0 CLI](https://github.com/auth0/auth0-cli). Then in the command line run:
48+
Sign up at [Auth0](https://a0.to/blog_signup) and install the [Auth0 CLI](https://github.com/auth0/auth0-cli). Then in the command line run:
4949

5050
```shell
5151
auth0 login
5252
```
5353

5454
The command output will display a device confirmation code and open a browser session to activate the device.
5555

56-
You don't need to create a client application for your API if not using opaque tokens. But you must register the API within your tenant, you can do it using Auth0 CLI:
56+
You need to create a client application for your API if you are using opaque tokens. Since this tutorial does not use opaque tokens, you only need to register the API within your tenant. You can do it using Auth0 CLI:
5757

5858
```shell
5959
auth0 apis create \
@@ -63,7 +63,9 @@ auth0 apis create \
6363
--offline-access=false
6464
```
6565

66-
The scopes `create:items`, `update:items`, `delete:items` will be required ahead in the tutorial. Add the `okta-spring-boot-starter` dependency:
66+
The scopes `create:items`, `update:items`, `delete:items` will be required ahead in the tutorial.
67+
68+
Next, add the `okta-spring-boot-starter` dependency:
6769

6870
```groovy
6971
// build.gradle
@@ -73,7 +75,7 @@ dependencies {
7375
...
7476
}
7577
```
76-
As the `menu-api` must be configured as an OAuth2 resource server, add the following properties:
78+
As the `menu-api` must be configured as an OAuth2 resource server, add the following properties to the `application.properties` file:
7779

7880
```properties
7981
# application.properties
@@ -94,7 +96,7 @@ Run the API with:
9496
Test the API authorization with curl:
9597

9698
```shell
97-
curl localhost:8080/api/menu/items
99+
curl -i localhost:8080/api/menu/items
98100
```
99101
You will get HTTP response code `401` because the request requires bearer authentication. Using Auth0 CLI, get an access token:
100102

@@ -110,9 +112,9 @@ ACCESS_TOKEN=<auth0-access-token>
110112
```
111113

112114
```shell
113-
curl -v --header "Authorization: Bearer $ACCESS_TOKEN" localhost:8080/api/menu/items
115+
curl -i --header "Authorization: Bearer $ACCESS_TOKEN" localhost:8080/api/menu/items
114116
```
115-
The request will not be authorized yet, because _This aud claim is not equal to the configured audience_. If the audience is not specified in the`auth0 test token` command, the default value is `https://dev-avup2laz.us.auth0.com/api/v2`, which is the Auth0Provider management API audience.
117+
The request will not be authorized yet, because _This aud claim is not equal to the configured audience_. If the audience is not specified in the`auth0 test token` command, the default value is `https://dev-avup2laz.us.auth0.com/api/v2`, which is the Auth0 Provider management API audience.
116118

117119
> NOTE: The Okta Spring Boot Starter autoconfigures the issuer and audience validation from the resource server properties for JWT authorization.
118120
@@ -209,7 +211,7 @@ public class ItemController {
209211
...
210212
```
211213

212-
Restart the server API. Before the sign in, you can create some test users with the Auth0 CLI. This step is optional, you can sign up later from the [Auth0 Universal Login](https://auth0.com/docs/authenticate/login/auth0-universal-login) form, or choose Google social login.
214+
Restart the API server. Before the sign in, you can create some test users with the Auth0 CLI. This step is optional, you can sign up later from the [Auth0 Universal Login](https://auth0.com/docs/authenticate/login/auth0-universal-login) form, or choose Google social login.
213215

214216
```shell
215217
auth0 users create
@@ -251,7 +253,7 @@ In the WHATABYTE client settings, re-enable the authentication features, and als
251253

252254
Now if you sign in with the user you created, the UI will not display the links to perform write operations, as the role has not yet been assigned.
253255

254-
First, the role must be defined in the Auth0 tenant as well. You can use the following Auth0 CLI command:
256+
First, the `menu-admin` role must be defined in the Auth0 tenant as well. You can use the following Auth0 CLI command:
255257

256258
```shell
257259
auth0 roles create
@@ -276,7 +278,7 @@ Follow the steps, you will see the output below:
276278

277279
### Mapping the roles to token claims
278280

279-
The role `menu-admin` and its permissions must be mapped to a claim in the accessToken, to make them available in the API for authorization. With [Auth0 Actions](https://auth0.com/docs/customize/actions) you can customize the Login flow to map the user roles to a custom claim.
281+
The role `menu-admin` and its permissions must be mapped to a claim in the access token, to make them available in the API for authorization. With [Auth0 Actions](https://auth0.com/docs/customize/actions) you can customize the Login flow to map the user roles to a custom claim.
280282

281283
First [configure your preferred editor](https://github.com/auth0/auth0-cli#customization) to use with the Auth0 CLI:
282284

0 commit comments

Comments
 (0)