Skip to content

Commit 50509bf

Browse files
committed
Added endpoints to demonstrate claim enforcement and JWT compression. Updated usage endpoint.
1 parent 38e829e commit 50509bf

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎jjwt/src/main/java/io/jsonwebtoken/jjwtfun/config/JWTCsrfTokenRepository.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public JWTCsrfTokenRepository(String secret) {
2828

2929
@Override
3030
public CsrfToken generateToken(HttpServletRequest request) {
31-
3231
String id = UUID.randomUUID().toString().replace("-", "");
3332

3433
Date now = new Date();

‎jjwt/src/main/java/io/jsonwebtoken/jjwtfun/config/WebSecurityConfig.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.jsonwebtoken.JwtException;
44
import io.jsonwebtoken.Jwts;
55
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.beans.factory.annotation.Qualifier;
76
import org.springframework.beans.factory.annotation.Value;
87
import org.springframework.context.annotation.Configuration;
98
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -37,6 +36,7 @@ protected void configure(HttpSecurity http) throws Exception {
3736
.csrfTokenRepository(jwtCsrfTokenRepository)
3837
.ignoringAntMatchers("/dynamic-builder-general")
3938
.ignoringAntMatchers("/dynamic-builder-specific")
39+
.ignoringAntMatchers("/dynamic-builder-compress")
4040
.and().authorizeRequests()
4141
.antMatchers("/**")
4242
.permitAll();

‎jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/DynamicJWTController.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package io.jsonwebtoken.jjwtfun.controller;
22

3+
import io.jsonwebtoken.Claims;
4+
import io.jsonwebtoken.Jws;
35
import io.jsonwebtoken.JwtBuilder;
46
import io.jsonwebtoken.JwtException;
57
import io.jsonwebtoken.Jwts;
68
import io.jsonwebtoken.SignatureAlgorithm;
9+
import io.jsonwebtoken.impl.compression.CompressionCodecs;
710
import io.jsonwebtoken.jjwtfun.model.JwtResponse;
811
import org.springframework.beans.factory.annotation.Value;
912
import org.springframework.web.bind.annotation.RequestBody;
1013
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestParam;
1115
import org.springframework.web.bind.annotation.RestController;
1216

1317
import java.io.UnsupportedEncodingException;
@@ -16,6 +20,7 @@
1620
import java.util.Map;
1721

1822
import static org.springframework.web.bind.annotation.RequestMethod.POST;
23+
import static org.springframework.web.bind.annotation.RequestMethod.GET;
1924

2025
@RestController
2126
public class DynamicJWTController extends BaseController {
@@ -34,6 +39,19 @@ public JwtResponse dynamicBuilderGeneric(@RequestBody Map<String, Object> claims
3439
return new JwtResponse(jws);
3540
}
3641

42+
@RequestMapping(value = "/dynamic-builder-compress", method = POST)
43+
public JwtResponse dynamicBuildercompress(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
44+
String jws = Jwts.builder()
45+
.setClaims(claims)
46+
.compressWith(CompressionCodecs.DEFLATE)
47+
.signWith(
48+
SignatureAlgorithm.HS256,
49+
secret.getBytes("UTF-8")
50+
)
51+
.compact();
52+
return new JwtResponse(jws);
53+
}
54+
3755
@RequestMapping(value = "/dynamic-builder-specific", method = POST)
3856
public JwtResponse dynamicBuilderSpecific(@RequestBody Map<String, Object> claims) throws UnsupportedEncodingException {
3957
JwtBuilder builder = Jwts.builder();

‎jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/HomeController.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ public String home(HttpServletRequest req) {
1414
return "Available commands (assumes httpie - https://github.com/jkbrzt/httpie):\n" +
1515
" http " + requestUrl + "/\n\tThis usage message\n" +
1616
" http " + requestUrl + "/static-builder\n\tbuild JWT from hardcoded claims\n" +
17-
" 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" +
18-
" 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" +
19-
" http " + requestUrl + "/parser?jwt=<jwt>\n\tParse passed in JWT\n";
17+
" http POST " + requestUrl + "/dynamic-builder-general claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using general claims map)\n" +
18+
" http POST " + requestUrl + "/dynamic-builder-specific claim-1=value-1 ... [claim-n=value-n]\n\tbuild JWT from passed in claims (using specific claims methods)\n" +
19+
" http POST " + requestUrl + "/dynamic-builder-compress claim-1=value-1 ... [claim-n=value-n]\n\tbuild DEFLATE compressed JWT from passed in claims\n" +
20+
" http " + requestUrl + "/parser?jwt=<jwt>\n\tParse passed in JWT\n" +
21+
" http " + requestUrl + "/parser-enforce?jwt=<jwt>\n\tParse passed in JWT enforcing the 'iss' registered claim and the 'hasMotorcycle' custom claim\n";
2022
}
2123

2224
private String getUrl(HttpServletRequest req) {

‎jjwt/src/main/java/io/jsonwebtoken/jjwtfun/controller/StaticJWTController.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public JwtResponse parser(@RequestParam String jwt) throws UnsupportedEncodingEx
4949

5050
return new JwtResponse(claims);
5151
}
52+
53+
@RequestMapping(value = "/parser-enforce", method = GET)
54+
public JwtResponse parserEnforce(@RequestParam String jwt) throws UnsupportedEncodingException {
55+
Jws<Claims> claims = Jwts.parser()
56+
.requireIssuer("Stormpath")
57+
.require("hasMotorcycle", true)
58+
.setSigningKey(secret.getBytes("UTF-8"))
59+
.parseClaimsJws(jwt);
60+
61+
return new JwtResponse(claims);
62+
}
5263
}

0 commit comments

Comments
 (0)