Added HomeController with usage. Updated static and dynamic jwt builders.
This commit is contained in:
parent
19ba450c08
commit
bf9c5b3c91
|
@ -4,9 +4,9 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
public class JJWTFunApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
SpringApplication.run(JJWTFunApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -1,39 +1,27 @@
|
|||
package io.jsonwebtoken.jjwtfun.controller;
|
||||
|
||||
import io.jsonwebtoken.JwtBuilder;
|
||||
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.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST;
|
||||
|
||||
@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 {
|
||||
@RequestMapping(value = "/dynamic-builder-general", method = POST)
|
||||
public String dynamicBuilderGeneric(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
.signWith(
|
||||
|
@ -42,4 +30,41 @@ public class DynamicJWTController {
|
|||
)
|
||||
.compact();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/dynamic-builder-specific", method = POST)
|
||||
public String dynamicBuilderSpecific(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
|
||||
JwtBuilder builder = Jwts.builder();
|
||||
|
||||
claims.forEach((key, value) -> {
|
||||
switch (key) {
|
||||
case "iss":
|
||||
builder.setIssuer((String)value);
|
||||
break;
|
||||
case "sub":
|
||||
builder.setSubject((String)value);
|
||||
break;
|
||||
case "aud":
|
||||
builder.setAudience((String)value);
|
||||
break;
|
||||
case "exp":
|
||||
builder.setExpiration(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value))));
|
||||
break;
|
||||
case "nbf":
|
||||
builder.setNotBefore(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value))));
|
||||
break;
|
||||
case "iat":
|
||||
builder.setIssuedAt(Date.from(Instant.ofEpochSecond(Long.parseLong((String)value))));
|
||||
break;
|
||||
case "jti":
|
||||
builder.setId((String)value);
|
||||
break;
|
||||
default:
|
||||
builder.claim(key, value);
|
||||
}
|
||||
});
|
||||
|
||||
builder.signWith(SignatureAlgorithm.HS256, secret.getBytes("UTF-8"));
|
||||
|
||||
return builder.compact();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package io.jsonwebtoken.jjwtfun.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@RestController
|
||||
public class HomeController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String home(HttpServletRequest req) {
|
||||
String requestUrl = getUrl(req);
|
||||
return "Available commands (assumes httpie - https://github.com/jkbrzt/httpie):\n" +
|
||||
" http " + requestUrl + "/\n\tThis usage\n" +
|
||||
" http " + requestUrl + "/static-builder\n\tbuild JWT from hardcoded claims\n" +
|
||||
" http " + requestUrl + "/dynamic-builder-general claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using general claims map)\n" +
|
||||
" http " + requestUrl + "/dynamic-builder-specific claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using specific claims methods)\n" +
|
||||
" http " + requestUrl + "/parser?jwt=<jwt>\n\tParse passed in JWT\n";
|
||||
}
|
||||
|
||||
private String getUrl(HttpServletRequest req) {
|
||||
return req.getScheme() + "://" +
|
||||
req.getServerName() +
|
||||
((req.getServerPort() == 80 || req.getServerPort() == 443) ? "" : ":" + req.getServerPort());
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ 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.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
@ -20,10 +21,13 @@ import java.util.Date;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class FixedJWTController {
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST;
|
||||
|
||||
@RequestMapping("/fixed-builder")
|
||||
@RestController
|
||||
public class StaticJWTController {
|
||||
|
||||
@RequestMapping(value = "/static-builder", method = POST)
|
||||
public String fixedBuilder() throws UnsupportedEncodingException {
|
||||
|
||||
String jws = Jwts.builder()
|
||||
|
@ -40,7 +44,7 @@ public class FixedJWTController {
|
|||
return jws;
|
||||
}
|
||||
|
||||
@RequestMapping("/fixed-parser")
|
||||
@RequestMapping(value = "/parser", method = GET)
|
||||
public Jws<Claims> fixedParser(@RequestParam String jws) throws UnsupportedEncodingException {
|
||||
Jws<Claims> claims = Jwts.parser()
|
||||
.setSigningKey("secret".getBytes("UTF-8"))
|
|
@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = DemoApplication.class)
|
||||
@SpringApplicationConfiguration(classes = JJWTFunApplication.class)
|
||||
@WebAppConfiguration
|
||||
public class DemoApplicationTests {
|
||||
|
||||
|
|
Loading…
Reference in New Issue