From 19ba450c086de761e31e6991a3c6c80d2b77d418 Mon Sep 17 00:00:00 2001 From: Micah Silverman Date: Fri, 24 Jun 2016 12:14:11 -0400 Subject: [PATCH] Created jjwt tutorial --- jjwt/.gitignore | 3 + jjwt/pom.xml | 59 ++++++++++++++++++ .../jsonwebtoken/jjwtfun/DemoApplication.java | 12 ++++ .../controller/DynamicJWTController.java | 45 ++++++++++++++ .../controller/FixedJWTController.java | 61 +++++++++++++++++++ .../src/main/resources/application.properties | 0 .../jjwtfun/DemoApplicationTests.java | 18 ++++++ pom.xml | 1 + 8 files changed, 199 insertions(+) create mode 100644 jjwt/.gitignore create mode 100644 jjwt/pom.xml create mode 100644 jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java create mode 100644 jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java create mode 100644 jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java create mode 100644 jjwt/src/main/resources/application.properties create mode 100644 jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java diff --git a/jjwt/.gitignore b/jjwt/.gitignore new file mode 100644 index 0000000000..f83e8cf07c --- /dev/null +++ b/jjwt/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml diff --git a/jjwt/pom.xml b/jjwt/pom.xml new file mode 100644 index 0000000000..0764194803 --- /dev/null +++ b/jjwt/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + io.jsonwebtoken + jjwtfun + 0.0.1-SNAPSHOT + jar + + jjwtfun + Exercising the JJWT + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-devtools + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-test + test + + + + io.jsonwebtoken + jjwt + 0.6.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java new file mode 100644 index 0000000000..2d09c182b6 --- /dev/null +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/DemoApplication.java @@ -0,0 +1,12 @@ +package io.jsonwebtoken.jjwtfun; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java new file mode 100644 index 0000000000..e1d98bb199 --- /dev/null +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java @@ -0,0 +1,45 @@ +package io.jsonwebtoken.jjwtfun.controller; + +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.io.UnsupportedEncodingException; +import java.util.AbstractMap.SimpleEntry; +import java.util.Collections; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@RestController +public class DynamicJWTController { + @Value("#{ @environment['jjwtfun.secret'] ?: 'secret' }") + String secret; + + Map map = Collections.unmodifiableMap(Stream.of( + new SimpleEntry<>("iss", ""), + new SimpleEntry<>("a", ""), + new SimpleEntry<>("b", ""), + new SimpleEntry<>("c", ""), + new SimpleEntry<>("d", ""), + new SimpleEntry<>("e", ""), + new SimpleEntry<>("f", ""), + new SimpleEntry<>("g", ""), + new SimpleEntry<>("h", "")) + .collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()))); + + @RequestMapping(value = "/dynamic-builder", method = RequestMethod.POST) + public String dynamicBuilder(@RequestBody Map claims) throws UnsupportedEncodingException { + return Jwts.builder() + .setClaims(claims) + .signWith( + SignatureAlgorithm.HS256, + secret.getBytes("UTF-8") + ) + .compact(); + } +} diff --git a/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java new file mode 100644 index 0000000000..7a648063ac --- /dev/null +++ b/jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/FixedJWTController.java @@ -0,0 +1,61 @@ +package io.jsonwebtoken.jjwtfun.controller; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jws; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.MalformedJwtException; +import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.SignatureException; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import java.io.UnsupportedEncodingException; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +@RestController +public class FixedJWTController { + + @RequestMapping("/fixed-builder") + public String fixedBuilder() throws UnsupportedEncodingException { + + String jws = Jwts.builder() + .setSubject("msilverman") + .setExpiration(Date.from(Instant.now().plus(1, ChronoUnit.DAYS))) + .claim("name", "Micah Silverman") + .claim("scope", "admins") + .signWith( + SignatureAlgorithm.HS256, + "secret".getBytes("UTF-8") + ) + .compact(); + + return jws; + } + + @RequestMapping("/fixed-parser") + public Jws fixedParser(@RequestParam String jws) throws UnsupportedEncodingException { + Jws claims = Jwts.parser() + .setSigningKey("secret".getBytes("UTF-8")) + .parseClaimsJws(jws); + + return claims; + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler({SignatureException.class, MalformedJwtException.class}) + public Map exception(Exception e) { + Map response = new HashMap<>(); + response.put("status", "ERROR"); + response.put("message", e.getMessage()); + response.put("exception-type", e.getClass().getName()); + return response; + } +} diff --git a/jjwt/src/main/resources/application.properties b/jjwt/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java b/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java new file mode 100644 index 0000000000..7e5b9b78f1 --- /dev/null +++ b/jjwt/src/test/java/io/jsonwebtoken/jjwtfun/DemoApplicationTests.java @@ -0,0 +1,18 @@ +package io.jsonwebtoken.jjwtfun; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = DemoApplication.class) +@WebAppConfiguration +public class DemoApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/pom.xml b/pom.xml index 75281ce80d..9e1d7e44c2 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ httpclient jackson javaxval + jjwt jooq-spring json-path mockito