Skip to content

Commit 19ba450

Browse files
committed
Created jjwt tutorial
1 parent 57ed00a commit 19ba450

8 files changed

Lines changed: 199 additions & 0 deletions

File tree

‎jjwt/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
target
3+
*.iml

‎jjwt/pom.xml‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.jsonwebtoken</groupId>
7+
<artifactId>jjwtfun</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>jjwtfun</name>
12+
<description>Exercising the JJWT</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-devtools</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-test</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>io.jsonwebtoken</groupId>
44+
<artifactId>jjwt</artifactId>
45+
<version>0.6.0</version>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
59+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.jsonwebtoken.jjwtfun;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.jsonwebtoken.jjwtfun.controller;
2+
3+
import io.jsonwebtoken.Jwts;
4+
import io.jsonwebtoken.SignatureAlgorithm;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.io.UnsupportedEncodingException;
12+
import java.util.AbstractMap.SimpleEntry;
13+
import java.util.Collections;
14+
import java.util.Map;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
@RestController
19+
public class DynamicJWTController {
20+
@Value("#{ @environment['jjwtfun.secret'] ?: 'secret' }")
21+
String secret;
22+
23+
Map<String, String> map = Collections.unmodifiableMap(Stream.of(
24+
new SimpleEntry<>("iss", ""),
25+
new SimpleEntry<>("a", ""),
26+
new SimpleEntry<>("b", ""),
27+
new SimpleEntry<>("c", ""),
28+
new SimpleEntry<>("d", ""),
29+
new SimpleEntry<>("e", ""),
30+
new SimpleEntry<>("f", ""),
31+
new SimpleEntry<>("g", ""),
32+
new SimpleEntry<>("h", ""))
33+
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())));
34+
35+
@RequestMapping(value = "/dynamic-builder", method = RequestMethod.POST)
36+
public String dynamicBuilder(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
37+
return Jwts.builder()
38+
.setClaims(claims)
39+
.signWith(
40+
SignatureAlgorithm.HS256,
41+
secret.getBytes("UTF-8")
42+
)
43+
.compact();
44+
}
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.jsonwebtoken.jjwtfun.controller;
2+
3+
import io.jsonwebtoken.Claims;
4+
import io.jsonwebtoken.Jws;
5+
import io.jsonwebtoken.Jwts;
6+
import io.jsonwebtoken.MalformedJwtException;
7+
import io.jsonwebtoken.SignatureAlgorithm;
8+
import io.jsonwebtoken.SignatureException;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import java.io.UnsupportedEncodingException;
17+
import java.time.Instant;
18+
import java.time.temporal.ChronoUnit;
19+
import java.util.Date;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
@RestController
24+
public class FixedJWTController {
25+
26+
@RequestMapping("/fixed-builder")
27+
public String fixedBuilder() throws UnsupportedEncodingException {
28+
29+
String jws = Jwts.builder()
30+
.setSubject("msilverman")
31+
.setExpiration(Date.from(Instant.now().plus(1, ChronoUnit.DAYS)))
32+
.claim("name", "Micah Silverman")
33+
.claim("scope", "admins")
34+
.signWith(
35+
SignatureAlgorithm.HS256,
36+
"secret".getBytes("UTF-8")
37+
)
38+
.compact();
39+
40+
return jws;
41+
}
42+
43+
@RequestMapping("/fixed-parser")
44+
public Jws<Claims> fixedParser(@RequestParam String jws) throws UnsupportedEncodingException {
45+
Jws<Claims> claims = Jwts.parser()
46+
.setSigningKey("secret".getBytes("UTF-8"))
47+
.parseClaimsJws(jws);
48+
49+
return claims;
50+
}
51+
52+
@ResponseStatus(HttpStatus.BAD_REQUEST)
53+
@ExceptionHandler({SignatureException.class, MalformedJwtException.class})
54+
public Map<String, String> exception(Exception e) {
55+
Map<String, String> response = new HashMap<>();
56+
response.put("status", "ERROR");
57+
response.put("message", e.getMessage());
58+
response.put("exception-type", e.getClass().getName());
59+
return response;
60+
}
61+
}

‎jjwt/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.jsonwebtoken.jjwtfun;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.SpringApplicationConfiguration;
6+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7+
import org.springframework.test.context.web.WebAppConfiguration;
8+
9+
@RunWith(SpringJUnit4ClassRunner.class)
10+
@SpringApplicationConfiguration(classes = DemoApplication.class)
11+
@WebAppConfiguration
12+
public class DemoApplicationTests {
13+
14+
@Test
15+
public void contextLoads() {
16+
}
17+
18+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<module>httpclient</module>
2323
<module>jackson</module>
2424
<module>javaxval</module>
25+
<module>jjwt</module>
2526
<module>jooq-spring</module>
2627
<module>json-path</module>
2728
<module>mockito</module>

0 commit comments

Comments
 (0)