11package io .jsonwebtoken .jjwtfun .controller ;
22
3+ import io .jsonwebtoken .JwtBuilder ;
34import io .jsonwebtoken .Jwts ;
45import io .jsonwebtoken .SignatureAlgorithm ;
56import org .springframework .beans .factory .annotation .Value ;
67import org .springframework .web .bind .annotation .RequestBody ;
78import org .springframework .web .bind .annotation .RequestMapping ;
8- import org .springframework .web .bind .annotation .RequestMethod ;
99import org .springframework .web .bind .annotation .RestController ;
1010
1111import java .io .UnsupportedEncodingException ;
12- import java .util . AbstractMap . SimpleEntry ;
13- import java .util .Collections ;
12+ import java .time . Instant ;
13+ import java .util .Date ;
1414import java .util .Map ;
15- import java . util . stream . Collectors ;
16- import java . util . stream . Stream ;
15+
16+ import static org . springframework . web . bind . annotation . RequestMethod . POST ;
1717
1818@ RestController
1919public class DynamicJWTController {
2020 @ Value ("#{ @environment['jjwtfun.secret'] ?: 'secret' }" )
2121 String secret ;
2222
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 {
23+ @ RequestMapping (value = "/dynamic-builder-general" , method = POST )
24+ public String dynamicBuilderGeneric (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
3725 return Jwts .builder ()
3826 .setClaims (claims )
3927 .signWith (
@@ -42,4 +30,41 @@ public String dynamicBuilder(@RequestBody Map<String, Object> claims) throws Uns
4230 )
4331 .compact ();
4432 }
33+
34+ @ RequestMapping (value = "/dynamic-builder-specific" , method = POST )
35+ public String dynamicBuilderSpecific (@ RequestBody Map <String , Object > claims ) throws UnsupportedEncodingException {
36+ JwtBuilder builder = Jwts .builder ();
37+
38+ claims .forEach ((key , value ) -> {
39+ switch (key ) {
40+ case "iss" :
41+ builder .setIssuer ((String )value );
42+ break ;
43+ case "sub" :
44+ builder .setSubject ((String )value );
45+ break ;
46+ case "aud" :
47+ builder .setAudience ((String )value );
48+ break ;
49+ case "exp" :
50+ builder .setExpiration (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
51+ break ;
52+ case "nbf" :
53+ builder .setNotBefore (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
54+ break ;
55+ case "iat" :
56+ builder .setIssuedAt (Date .from (Instant .ofEpochSecond (Long .parseLong ((String )value ))));
57+ break ;
58+ case "jti" :
59+ builder .setId ((String )value );
60+ break ;
61+ default :
62+ builder .claim (key , value );
63+ }
64+ });
65+
66+ builder .signWith (SignatureAlgorithm .HS256 , secret .getBytes ("UTF-8" ));
67+
68+ return builder .compact ();
69+ }
4570}
0 commit comments