Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions examples/mp-rest-jwt-principal/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
= MicroProfile JWT Principal
:index-group: MicroProfile
:jbake-type: page
:jbake-status: published

This is an example on how to use MicroProfile JWT in TomEE by accessing
Principal from the JsonWebToken.

== Run the application:

[source, bash]
----
mvn clean install tomee:run
----

This example is a CRUD application for orders in store.

== Requirments and configuration

For usage of MicroProfile JWT we have to change the following to our
project:

[arabic]
. Add the dependency to our `pom.xml` file:
+
....
<dependency>
<groupId>org.eclipse.microprofile.jwt</groupId>
<artifactId>microprofile-jwt-auth-api</artifactId>
<version>${mp-jwt.version}</version>
<scope>provided</scope>
</dependency>
....
. Annotate our `Application.class` with `@LoginConfig(authMethod = "MP-JWT")`

. Provide public and private key for authentication. And specify the location of the public key and the issuer in our
`microprofile-config.properties` file.
+
[source,properties]
----
mp.jwt.verify.publickey.location=/publicKey.pem
mp.jwt.verify.issuer=https://example.com
----

. Define `@RolesAllowed()` on the endpoints we want to protect.

== Obtaining the JWT Principal

We obtain the `Principal` in the MicroProfile class `org.eclipse.microprofile.jwt.JsonWebToken`. From there
we can acquire username and groups of the user that is accessing the endpoint.

[source,java]
----
@Inject
private JsonWebToken jwtPrincipal;
----

== About the application architecture

The application enables us to manipulate orders with specific users. We have two users `Alice Wonder`
and `John Doe`. They can read, create, edit and delete specific entries. And for each creation
we save the user who created the order. In case a user edits the entry we record that by accessing
the `Principal` who has sent the request to our backend.

`alice-wonder-jwt.json`

[source,json]
----
{
"iss": "https://example.com",
"upn": "alice",
"sub": "alice.wonder@example.com",
"name": "Alice Wonder",
"iat": 1516239022,
"groups": [
"buyer"
]
}
----

`john-doe-jwt.json`
[source,json]
----
{
"iss": "https://example.com",
"upn": "john",
"sub": "john.doe@example.com",
"name": "John Doe",
"iat": 1516239022,
"groups": [
"merchant"
]
}
----

== Access the endpoints with JWT token

We access endpoints from our test class by creating a `JWT` with the help of
our `TokenUtils.generateJWTString(String jsonResource)` which signs our user
data in json format with the help of our `src/test/resources/privateKey.pem` key.

We can also generate new `privateKey.pem` and `publicKey.pem` with the
`GenerateKeyUtils.generateKeyPair(String keyAlgorithm, int keySize)` method.
184 changes: 184 additions & 0 deletions examples/mp-rest-jwt-principal/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.superbiz</groupId>
<artifactId>mp-rest-jwt-principal</artifactId>
<version>8.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>OpenEJB :: Examples :: MP REST JWT PRINCIPAL</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tomee.version>8.0.0-SNAPSHOT</tomee.version>
<junit.version>4.23</junit.version>
<arquillian-bom.version>1.4.1.Final</arquillian-bom.version>
<mp-jwt.version>1.1.1</mp-jwt.version>
<mp-config.version>1.0</mp-config.version>
<mp-rest-client.version>1.1</mp-rest-client.version>
</properties>

<build>
<defaultGoal>install</defaultGoal>
<finalName>phonestore</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomee.maven</groupId>
<artifactId>tomee-maven-plugin</artifactId>
<version>${tomee.version}</version>
<configuration>
<tomeeClassifier>microprofile</tomeeClassifier>
<args>-Xmx512m -XX:PermSize=256m</args>
<config>${project.basedir}/src/main/tomee/</config>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<!-- Now pull in our server-based unit testing framework -->
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>${arquillian-bom.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>


<dependencies>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.jwt</groupId>
<artifactId>microprofile-jwt-auth-api</artifactId>
<version>${mp-jwt.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
<version>${mp-rest-client.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
<version>${mp-config.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>arquillian-tomee-remote</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>arquillian-tomee-remote</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>apache-tomee</artifactId>
<version>${tomee.version}</version>
<type>zip</type>
<classifier>microprofile</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>mp-jwt</artifactId>
<version>${tomee.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>

<!--
This section allows you to configure where to publish libraries for sharing.
It is not required and may be deleted. For more information see:
http://maven.apache.org/plugins/maven-deploy-plugin/
-->
<distributionManagement>
<repository>
<id>localhost</id>
<url>file://${basedir}/target/repo/</url>
</repository>
<snapshotRepository>
<id>localhost</id>
<url>file://${basedir}/target/snapshot-repo/</url>
</snapshotRepository>
</distributionManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.superbiz.store.entity;

import java.math.BigDecimal;
import java.util.List;

public class Order {

private Integer id;
private String createdUser;
private String updatedUser;
private BigDecimal orderPrice;
private List<Product> products;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getCreatedUser() {
return createdUser;
}

public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
}

public String getUpdatedUser() {
return updatedUser;
}

public void setUpdatedUser(String updatedUser) {
this.updatedUser = updatedUser;
}

public BigDecimal getOrderPrice() {
return orderPrice;
}

public void setOrderPrice(BigDecimal orderPrice) {
this.orderPrice = orderPrice;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}
}
Loading