An open API service indexing awesome lists of open source software.

https://github.com/oktadev/auth0-java-oauth-examples


https://github.com/oktadev/auth0-java-oauth-examples

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

= Java OAuth Examples

See link:demo.adoc[this demo script] to learn how these apps were created.

== Prerequisites:

- https://sdkman.io/[Java 17]+
- https://github.com/auth0/auth0-cli#installation[Auth0 CLI]
- https://httpie.org/doc#installation[HTTPie]
- https://auth0.com/signup[Auth0 Account]

== Get the Code

Clone this repo to begin.

[source,shell]
----
git clone https://github.com/oktadev/auth0-java-oauth-examples.git
cd auth0-java-oauth-examples
----

== Configure Spring Boot

Create an OIDC application using the Auth0 CLI.

[source,shell]
----
auth0 apps create \
--name "Spring Boot" \
--description "Spring Boot Example" \
--type regular \
--callbacks http://localhost:8080/login/oauth2/code/okta \
--logout-urls http://localhost:8080 \
--reveal-secrets
----

Update `application.properties` to use these values.

[source,properties]
----
okta.oauth2.issuer=https:///
okta.oauth2.audience=${okta.oauth2.issuer}api/v2/
okta.oauth2.client-id=
okta.oauth2.client-secret=
----

Start the app:

[source,shell]
----
gradle bootRun
----

Log in at `http://localhost:8080`.

=== API Access with OAuth 2.0

Create an access token using Auth0's CLI:

[source,shell]
----
auth0 test token -a https:///api/v2/
----

Set the access token as an environment variable:

[source,shell]
----
TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6...
----

Access your resource server using HTTPie:

[source,shell]
----
http :8080/hello "Authorization: Bearer $TOKEN"
----

== Configure Quarkus

Update `quarkus/src/main/resources/application.properties` to use your Auth0 domain.

[source,properties]
----
quarkus.oidc.auth-server-url=https://
----

Run the app:

[source,shell]
----
mvn quarkus:dev
----

Verify you can access it with an access token.

[source,shell]
----
http :8080/hello "Authorization: Bearer $TOKEN"
----

== Configure Micronaut

Update `micronaut/src/main/resources/application.yml` to use your Auth0 domain.

[source,yaml]
----
micronaut.security.token.jwt.signatures.jwks.auth0.url: https:///.well-known/jwks.json
----

Run the app:

[source,shell]
----
mvn mn:run
----

Verify you can access it with an access token.

[source,shell]
----
http :8080/hello "Authorization: Bearer $TOKEN"
----

== Learn More

For more details on Java and OAuth, please read https://developer.okta.com/blog/2022/06/16/oauth-java[OAuth for Java Developers].