After you sign up or log in to PlusAuth, you need to create a client to get the necessary configuration keys in the dashboard. Go to Clients and create a client with the type of Server to Server Application
API is a definition in PlusAuth equals to your services which you want to secure. You need to create an API to add authorization to your app. Go to Api's and create a new API. Provide a name and audience to your api. Audience must be a URL that identifies your api, like https://example.com/api.
After you create the API, you can create permissions for it. Permissions allow you to define how resources can be accessed with a specific access token. Go to Dashboard > Api's and click on the Permissions button on the row at the data table which contains your API.
Finally, authorize your client in your api to grant permissions. Go to Dashboard > Api's , then select your api and navigate to Authorized Clients. Add your client to the Authorized Clients list and grant permissions to it.
The sample uses application.yml file to add Oauth2 issuer, jwks uri, and audience. Other configuration mechanisms like application.properties are also supported.
WebSecurityConfigurerAdapter interface provides a way to add authorization middleware to endpoints. SecurityConfig middleware, which is defined below, checks the request's header for access token. An error response returns from middleware if the token is not present in the header of the request.
You may have noticed that the Audience value defined in the Create API section is used here.
Also, jwtDecoder provides token decoder and validation functionality to security middleware. It also provides scope validation if the endpoint requires permission.
Add a Controller to your application to serve API endpoints. All routes are protected by SecurityConfig, which is defined in Configure Authorization Middleware section, so you don't need to add any annotation to secure endpoints.
You need to obtain an access token to call your API. This tutorial shows how OAuth Client Credentials Flow works for server-to-server communication where there is no user and login process. You will need your client's Client Id and Client Secret properties to acquire an access token in Client Credentials Flow. Also, you must include Audience and Scope parameters to access your API.
You can obtain an access token using the command line or another application. Create a POST request and enter the required parameters.
You may have noticed that the values defined in Configure Client and Configure APIs sections are used here. If you have used different values make sure to update this file accordingly.