User-restricted NHS CIS2 combined authentication and authorisation - Java tutorial
Connect to a user-restricted REST API using NHS Care Identity Service 2 combined authentication and authorisation and the Java programming language.
Overview
This tutorial shows you how to connect to a user-restricted REST API using NHS Care Identity Service 2 (NHS CIS2) combined authentication and authorisation and the Java programming language.
It uses Spring to create a simple web application which authenticates the end user using our mock NHS CIS2 authorisation service, receives an access token from our authorisation server and calls the user restricted endpoint of our Hello World API.
To call a user-restricted API, the end user must be authenticated. NHS CIS2 is used to authenticate when the end user is a healthcare worker. With the combined authentication and authorisation pattern, authentication is done by NHS CIS2 but is coordinated behind our OAuth2.0 authorisation server. In exchange, you receive an access token which you need to include in the API request.
Setting up your environment
This example project was developed using Java 17 and Maven 3.8.6, so you need to have these installed.
Check out the GitHub repository
You can find the code for this Java user-restricted REST API NHS CIS2 combined authentication and authorisation tutorial in our GitHub repository.
Implementation details
This project contains:
-
an App.java file containing the application startup code
-
a controllers folder containing a MainController.java file that handles each endpoint of the application, taking input data, performing operations if necessary, and passing the output to the template views
-
an auth folder containing a WebSecurityConfig.java file. This marks certain paths as restricted, requiring the user to authenticate to access them. If an unauthenticated user tries to access these pages, it will trigger the OAuth authentication client (configured in application.yml) to run.
-
a hello folder containing a HelloWorld.java file. This makes a GET request to the specified API endpoint using the provided access token.
To follow this tutorial, download or clone this repository.
Create an application on our developer portal
Sign in to your developer account and use the My application link to create an application. This gives you access to your application's API Key and API Secret.
To do this, follow Step 1: create an application.
Notes:
-
when creating a new app, you need to select the 'Environment' and for this tutorial, select 'Sandbox'
-
when editing your application details and selecting the API that you want to use, select 'Hello World (Sandbox)'
-
you are prompted for a callback URL which is required for combined authentication and authorization. This URL is used to send users back to your application after successful (or unsuccessful) authorisation. This project is configured to run at https://localhost:8080 and Spring Security creates a callback endpoint at /login/oauth2/code/simulated-auth. Therefore the callback URL should be set to https://localhost:8080/login/oauth2/code/simulated-auth
-
make a note of your API Key and API secret
Populate the project's environment variables
You should now have your application's:
- API Key
- API Secret
To run the example tutorial, you need to set the following environment variables.
Variable name | Description |
---|---|
CLIENT_ID | Your application's API Key |
CLIENT_SECRET | Your application's API Secret |
OAUTH_ENDPOINT | Your application's Environment URL followed by /oauth2 |
REDIRECT_URI | Your application's Callback URL |
ENDPOINT | Your application's Environment URL followed by /hello-world/hello/user |
You can set your environment variables in a file named .env. This project contains a sample env file to use:
- rename env.sample to .env and modify it
- source it by running source .env
Run the code
Once you set the environment variables, you are ready to run the project.
Run the application
You should first source your environment variable file before executing your application.
Assuming you are using maven cli tool
source .env
mvn spring-boot:run
Run using Makefile
Alternatively, you can set your environment variables in a file named .env. Then use the make command: make run.
Using the application
When you run the code, you should be able to load the application at https://localhost:8080.
- Click the button 'Login with NHS CIS2' to be directed to our mock NHS CIS2 authorisation service.
- Select an option to simulate a login and click 'Sign in'.
- You are redirected back to the application and the access token you have received is displayed.
- To use the access token in a request to the Hello World API, click 'Call API'.
- The response from the API should read:
{
"message": "Hello User!"
}
Create a developer account on our developer portal
To get started with our tutorials and APIs, you need to create a developer account on our developer portal.
Last edited: 12 October 2022 2:34 pm