Created jjwt tutorial
This commit is contained in:
parent
57ed00a134
commit
19ba450c08
|
@ -0,0 +1,3 @@
|
|||
.idea
|
||||
target
|
||||
*.iml
|
|
@ -0,0 +1,59 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwtfun</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>jjwtfun</name>
|
||||
<description>Exercising the JJWT</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.5.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>0.6.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String, String> 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<String, Object> claims) throws UnsupportedEncodingException {
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
.signWith(
|
||||
SignatureAlgorithm.HS256,
|
||||
secret.getBytes("UTF-8")
|
||||
)
|
||||
.compact();
|
||||
}
|
||||
}
|
|
@ -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<Claims> fixedParser(@RequestParam String jws) throws UnsupportedEncodingException {
|
||||
Jws<Claims> claims = Jwts.parser()
|
||||
.setSigningKey("secret".getBytes("UTF-8"))
|
||||
.parseClaimsJws(jws);
|
||||
|
||||
return claims;
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler({SignatureException.class, MalformedJwtException.class})
|
||||
public Map<String, String> exception(Exception e) {
|
||||
Map<String, String> response = new HashMap<>();
|
||||
response.put("status", "ERROR");
|
||||
response.put("message", e.getMessage());
|
||||
response.put("exception-type", e.getClass().getName());
|
||||
return response;
|
||||
}
|
||||
}
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue