This repository shows how to create a GrapQL API with MicroProfile and use JWT for authentication. Please read How to GraphQL with MicroProfile to see how this example was created.
Prerequisites:
- Java 11: OpenJDK website.
- Maven: Maven can be installed according to the instructions on their website. You could also use SDKMAN or Homebrew.
- Okta Developer Account: Go to our developer site and sign up for a free developer account.
- HTTPie: Install it according to the docs on their site.
Okta has Authentication and User Management APIs that reduce development time with instant-on, scalable user infrastructure. Okta's intuitive API and expert support make it easy for developers to authenticate, manage, and secure users and roles in any application.
To install this example, run the following commands:
git clone https://github.com/oktadeveloper/okta-microprofile-graphql-example.git
cd okta-microprofile-graphql-example
Log in to your Okta Developer account (or sign up if you don’t have an account).
- From the Applications page, choose Add Application.
- On the Create New Application page, select Web.
- Give your app a memorable name, add
https://oidcdebugger.com/debug
as a Login redirect URI, check the box next to Implicit (Hybrid), then click Done.
Modify pom.xml
to use your Okta domain:
<properties>
...
<liberty.var.oktaDomain>{yourOktaDomain}</liberty.var.oktaDomain>
</properties>
NOTE: The value of {yourOktaDomain}
should be something like dev-123456.okta.com
. Make sure you don't include -admin
in the value!
Build your app and start the server:
mvn liberty:run
To generate a JWT access token, go to https://oidcdebugger.com and fill out the values for your OIDC application.
In a shell, store the token in a variable:
TOKEN=eyJraWQiOiJBX05XeGVXcVdrNG5pUjBFWlJnbWg5X3JJ...
Then, use it to hit the API and authenticate with OAuth 2.0:
http POST :9080/graphql query='{ surfReport(location:"Texas") {windKnots,swellHeight,swellPeriodSeconds} }' "Authorization: Bearer $TOKEN"
This example uses the following open source libraries:
Please post any questions as comments on the blog post, or on the Okta Developer Forums.
Apache 2.0, see LICENSE.