Skip to content

Commit 7bcbd95

Browse files
committed
Finished Hands-On JWT
1 parent 8e79ed0 commit 7bcbd95

File tree

7 files changed

+53
-6
lines changed

7 files changed

+53
-6
lines changed

working/frontend/pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
2-
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
45
<modelVersion>4.0.0</modelVersion>
56
<groupId>org.acme</groupId>
67
<artifactId>frontend</artifactId>
@@ -30,6 +31,10 @@
3031
</dependencies>
3132
</dependencyManagement>
3233
<dependencies>
34+
<dependency>
35+
<groupId>io.quarkus</groupId>
36+
<artifactId>quarkus-smallrye-jwt</artifactId>
37+
</dependency>
3338
<dependency>
3439
<groupId>io.quarkus</groupId>
3540
<artifactId>quarkus-resteasy</artifactId>

working/frontend/src/main/java/org/acme/FrontendResource.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.acme;
22

3+
import java.security.Principal;
34
import java.util.Arrays;
45
import java.util.List;
6+
import java.util.stream.Collectors;
57

8+
import javax.annotation.security.RolesAllowed;
69
import javax.inject.Inject;
710
import javax.ws.rs.GET;
811
import javax.ws.rs.Path;
@@ -11,6 +14,7 @@
1114

1215
import org.eclipse.microprofile.faulttolerance.CircuitBreaker;
1316
import org.eclipse.microprofile.faulttolerance.Fallback;
17+
import org.eclipse.microprofile.jwt.JsonWebToken;
1418
import org.eclipse.microprofile.metrics.MetricUnits;
1519
import org.eclipse.microprofile.metrics.annotation.Counted;
1620
import org.eclipse.microprofile.metrics.annotation.Gauge;
@@ -29,12 +33,34 @@ public class FrontendResource {
2933
@RestClient
3034
StudentRestClient student;
3135

36+
@Inject
37+
Principal principal;
38+
39+
@Inject
40+
JsonWebToken token;
41+
42+
@RolesAllowed("user")
43+
@GET
44+
@Path("/tokeninfo")
45+
@Produces(MediaType.TEXT_PLAIN)
46+
public String tokeninfo() {
47+
String string = "Principal: " + principal.getName();
48+
string += ",\n";
49+
string += token.getClaimNames()
50+
.stream()
51+
.map(claim -> "\n " + claim + ": " + token.getClaim(claim))
52+
.collect(Collectors.toList())
53+
.toString();
54+
return string;
55+
}
56+
3257
@GET
3358
@Produces(MediaType.TEXT_PLAIN)
3459
public String hello() {
3560
return student.hello();
3661
}
3762

63+
@RolesAllowed("admin")
3864
@SimplyTimed(absolute = true, name = "listStudentsTime", displayName = "FrontendResource.listStudents()")
3965
// @Fallback(value = ListStudentsFallbackHandler.class)
4066
@Fallback(fallbackMethod = "listStudentsFallback")

working/frontend/src/main/java/org/acme/StudentRestClient.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import javax.ws.rs.Produces;
88
import javax.ws.rs.core.MediaType;
99

10+
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
1011
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
1112

13+
@RegisterClientHeaders
1214
@RegisterRestClient(configKey = "StudentService")
1315
@Path("/student")
1416
public interface StudentRestClient {

working/frontend/src/main/resources/META-INF/microprofile-config.properties

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ StudentService/mp-rest/uri=http://localhost:8082
1010

1111
# org.acme.FrontendResource/listStudents/Timeout/value=3000
1212

13-
quarkus.resteasy.metrics.enabled=true
13+
quarkus.resteasy.metrics.enabled=true
14+
15+
org.eclipse.microprofile.rest.client.propagateHeaders=Authorization
16+
mp.jwt.verify.issuer=airhacks
17+
mp.jwt.verify.publickey=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtBR6TwVxolT5E2emnQEwqJztmeWRThU4ZA3V9+4vjOXoNmSKWrLfqLaKuMric9opYQi86yO1o0qChkAnlRY7ZytcaFqcehYOSAhcghYNn4Wzi70D2lJHj/YflFKdssySyNzqMIBMxNWZWx8kIVDRrVamsmF2Fo4Dg72ce8KiMSlqkWrHiSbfWpa2aQru9dEhErJPf05fGzQWwtvOvtLCp/tLXq7GmTE2XJJdiCk3CdE3OP/FQRWyeRtHk6Uq4hjzXTX6Wnrb7xDZCjQubfWYq9yoINet1eMFWFUXRsAJQbMJKIstcCvwmO35iPjFrftWTADOh3pzIARVqWwupDN7fwIDAQAB

working/student/pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
2-
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
45
<modelVersion>4.0.0</modelVersion>
56
<groupId>org.acme</groupId>
67
<artifactId>student</artifactId>
@@ -30,6 +31,10 @@
3031
</dependencies>
3132
</dependencyManagement>
3233
<dependencies>
34+
<dependency>
35+
<groupId>io.quarkus</groupId>
36+
<artifactId>quarkus-smallrye-jwt</artifactId>
37+
</dependency>
3338
<dependency>
3439
<groupId>io.quarkus</groupId>
3540
<artifactId>quarkus-resteasy</artifactId>

working/student/src/main/java/org/acme/StudentResource.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.concurrent.TimeUnit;
66

7+
import javax.annotation.security.RolesAllowed;
78
import javax.inject.Inject;
89
import javax.ws.rs.GET;
910
import javax.ws.rs.Path;
@@ -28,6 +29,7 @@ public String hello() {
2829
return "Howdy";
2930
}
3031

32+
@RolesAllowed("admin")
3133
@GET
3234
@Path("/list")
3335
@Produces(MediaType.APPLICATION_JSON)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# delay=2500
22
quarkus.http.port=8082
3-
students=Duke,John,Jane,Arun,Christina
3+
students=Duke,John,Jane,Arun,Christina
4+
5+
mp.jwt.verify.issuer=airhacks
6+
mp.jwt.verify.publickey=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtBR6TwVxolT5E2emnQEwqJztmeWRThU4ZA3V9+4vjOXoNmSKWrLfqLaKuMric9opYQi86yO1o0qChkAnlRY7ZytcaFqcehYOSAhcghYNn4Wzi70D2lJHj/YflFKdssySyNzqMIBMxNWZWx8kIVDRrVamsmF2Fo4Dg72ce8KiMSlqkWrHiSbfWpa2aQru9dEhErJPf05fGzQWwtvOvtLCp/tLXq7GmTE2XJJdiCk3CdE3OP/FQRWyeRtHk6Uq4hjzXTX6Wnrb7xDZCjQubfWYq9yoINet1eMFWFUXRsAJQbMJKIstcCvwmO35iPjFrftWTADOh3pzIARVqWwupDN7fwIDAQAB

0 commit comments

Comments
 (0)