Skip to content

Commit 7910865

Browse files
committed
Added support for java.time Instant instead of java.util.Date
1 parent 44faaca commit 7910865

18 files changed

+249
-221
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ dist: trusty
44
sudo: required
55
language: java
66
jdk:
7-
- openjdk7
87
- oraclejdk8
98
- oraclejdk9
109

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@
109109
<version>${jackson.version}</version>
110110
</dependency>
111111

112+
<dependency>
113+
<groupId>com.fasterxml.jackson.datatype</groupId>
114+
<artifactId>jackson-datatype-jsr310</artifactId>
115+
<version>${jackson.version}</version>
116+
</dependency>
117+
112118
<!-- Optional Dependencies: -->
113119
<dependency>
114120
<groupId>org.bouncycastle</groupId>

src/main/java/io/jsonwebtoken/Claims.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.jsonwebtoken;
1717

18-
import java.util.Date;
18+
import java.time.Instant;
1919
import java.util.Map;
2020

2121
/**
@@ -111,13 +111,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
111111
*
112112
* @return the JWT {@code exp} value or {@code null} if not present.
113113
*/
114-
Date getExpiration();
114+
Instant getExpiration();
115115

116116
/**
117117
* {@inheritDoc}
118118
*/
119119
@Override //only for better/targeted JavaDoc
120-
Claims setExpiration(Date exp);
120+
Claims setExpiration(Instant exp);
121121

122122
/**
123123
* Returns the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
@@ -127,13 +127,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
127127
*
128128
* @return the JWT {@code nbf} value or {@code null} if not present.
129129
*/
130-
Date getNotBefore();
130+
Instant getNotBefore();
131131

132132
/**
133133
* {@inheritDoc}
134134
*/
135135
@Override //only for better/targeted JavaDoc
136-
Claims setNotBefore(Date nbf);
136+
Claims setNotBefore(Instant nbf);
137137

138138
/**
139139
* Returns the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
@@ -143,13 +143,13 @@ public interface Claims extends Map<String, Object>, ClaimsMutator<Claims> {
143143
*
144144
* @return the JWT {@code nbf} value or {@code null} if not present.
145145
*/
146-
Date getIssuedAt();
146+
Instant getIssuedAt();
147147

148148
/**
149149
* {@inheritDoc}
150150
*/
151151
@Override //only for better/targeted JavaDoc
152-
Claims setIssuedAt(Date iat);
152+
Claims setIssuedAt(Instant iat);
153153

154154
/**
155155
* Returns the JWTs <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">

src/main/java/io/jsonwebtoken/ClaimsMutator.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package io.jsonwebtoken;
1717

18-
import java.util.Date;
18+
import java.time.Instant;
1919

2020
/**
2121
* Mutation (modifications) to a {@link io.jsonwebtoken.Claims Claims} instance.
@@ -63,7 +63,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
6363
* @param exp the JWT {@code exp} value or {@code null} to remove the property from the JSON map.
6464
* @return the {@code Claims} instance for method chaining.
6565
*/
66-
T setExpiration(Date exp);
66+
T setExpiration(Instant exp);
6767

6868
/**
6969
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
@@ -74,7 +74,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
7474
* @param nbf the JWT {@code nbf} value or {@code null} to remove the property from the JSON map.
7575
* @return the {@code Claims} instance for method chaining.
7676
*/
77-
T setNotBefore(Date nbf);
77+
T setNotBefore(Instant nbf);
7878

7979
/**
8080
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
@@ -85,7 +85,7 @@ public interface ClaimsMutator<T extends ClaimsMutator> {
8585
* @param iat the JWT {@code iat} value or {@code null} to remove the property from the JSON map.
8686
* @return the {@code Claims} instance for method chaining.
8787
*/
88-
T setIssuedAt(Date iat);
88+
T setIssuedAt(Instant iat);
8989

9090
/**
9191
* Sets the JWT <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">

src/main/java/io/jsonwebtoken/Clock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.jsonwebtoken;
22

3-
import java.util.Date;
3+
import java.time.Instant;
44

55
/**
66
* A clock represents a time source that can be used when creating and verifying JWTs.
@@ -14,5 +14,5 @@ public interface Clock {
1414
*
1515
* @return the clock's current timestamp at the instant the method is invoked.
1616
*/
17-
Date now();
17+
Instant now();
1818
}

src/main/java/io/jsonwebtoken/JwtBuilder.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package io.jsonwebtoken;
1717

1818
import java.security.Key;
19-
import java.util.Date;
19+
import java.time.Instant;
2020
import java.util.Map;
2121

2222
/**
@@ -198,16 +198,16 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
198198
* <p>A JWT obtained after this timestamp should not be used.</p>
199199
*
200200
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
201-
* the Claims {@link Claims#setExpiration(java.util.Date) expiration} field with the specified value. This allows
201+
* the Claims {@link Claims#setExpiration(java.time.Instant) expiration} field with the specified value. This allows
202202
* you to write code like this:</p>
203203
*
204204
* <pre>
205-
* String jwt = Jwts.builder().setExpiration(new Date(System.currentTimeMillis() + 3600000)).compact();
205+
* String jwt = Jwts.builder().setExpiration(Instant.ofEpochMilli(System.currentTimeMillis() + 3600000)).compact();
206206
* </pre>
207207
*
208208
* <p>instead of this:</p>
209209
* <pre>
210-
* Claims claims = Jwts.claims().setExpiration(new Date(System.currentTimeMillis() + 3600000));
210+
* Claims claims = Jwts.claims().setExpiration(Instant.ofEpochMilli(System.currentTimeMillis() + 3600000));
211211
* String jwt = Jwts.builder().setClaims(claims).compact();
212212
* </pre>
213213
* <p>if desired.</p>
@@ -217,7 +217,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
217217
* @since 0.2
218218
*/
219219
@Override //only for better/targeted JavaDoc
220-
JwtBuilder setExpiration(Date exp);
220+
JwtBuilder setExpiration(Instant exp);
221221

222222
/**
223223
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.5">
@@ -226,16 +226,16 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
226226
* <p>A JWT obtained before this timestamp should not be used.</p>
227227
*
228228
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
229-
* the Claims {@link Claims#setNotBefore(java.util.Date) notBefore} field with the specified value. This allows
229+
* the Claims {@link Claims#setNotBefore(java.time.Instant) notBefore} field with the specified value. This allows
230230
* you to write code like this:</p>
231231
*
232232
* <pre>
233-
* String jwt = Jwts.builder().setNotBefore(new Date()).compact();
233+
* String jwt = Jwts.builder().setNotBefore(Instant.now()).compact();
234234
* </pre>
235235
*
236236
* <p>instead of this:</p>
237237
* <pre>
238-
* Claims claims = Jwts.claims().setNotBefore(new Date());
238+
* Claims claims = Jwts.claims().setNotBefore(Instant.now());
239239
* String jwt = Jwts.builder().setClaims(claims).compact();
240240
* </pre>
241241
* <p>if desired.</p>
@@ -245,7 +245,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
245245
* @since 0.2
246246
*/
247247
@Override //only for better/targeted JavaDoc
248-
JwtBuilder setNotBefore(Date nbf);
248+
JwtBuilder setNotBefore(Instant nbf);
249249

250250
/**
251251
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.6">
@@ -254,7 +254,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
254254
* <p>The value is the timestamp when the JWT was created.</p>
255255
*
256256
* <p>This is a convenience method. It will first ensure a Claims instance exists as the JWT body and then set
257-
* the Claims {@link Claims#setIssuedAt(java.util.Date) issuedAt} field with the specified value. This allows
257+
* the Claims {@link Claims#setIssuedAt(java.time.Instant) issuedAt} field with the specified value. This allows
258258
* you to write code like this:</p>
259259
*
260260
* <pre>
@@ -263,7 +263,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
263263
*
264264
* <p>instead of this:</p>
265265
* <pre>
266-
* Claims claims = Jwts.claims().setIssuedAt(new Date());
266+
* Claims claims = Jwts.claims().setIssuedAt(Instant.now());
267267
* String jwt = Jwts.builder().setClaims(claims).compact();
268268
* </pre>
269269
* <p>if desired.</p>
@@ -273,7 +273,7 @@ public interface JwtBuilder extends ClaimsMutator<JwtBuilder> {
273273
* @since 0.2
274274
*/
275275
@Override //only for better/targeted JavaDoc
276-
JwtBuilder setIssuedAt(Date iat);
276+
JwtBuilder setIssuedAt(Instant iat);
277277

278278
/**
279279
* Sets the JWT Claims <a href="https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-25#section-4.1.7">

src/main/java/io/jsonwebtoken/JwtParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.jsonwebtoken.impl.DefaultClock;
1919

2020
import java.security.Key;
21-
import java.util.Date;
21+
import java.time.Instant;
2222

2323
/**
2424
* A parser for reading JWT strings, used to convert them into a {@link Jwt} object representing the expanded JWT.
@@ -87,7 +87,7 @@ public interface JwtParser {
8787
* @see MissingClaimException
8888
* @see IncorrectClaimException
8989
*/
90-
JwtParser requireIssuedAt(Date issuedAt);
90+
JwtParser requireIssuedAt(Instant issuedAt);
9191

9292
/**
9393
* Ensures that the specified {@code exp} exists in the parsed JWT. If missing or if the parsed
@@ -99,7 +99,7 @@ public interface JwtParser {
9999
* @see MissingClaimException
100100
* @see IncorrectClaimException
101101
*/
102-
JwtParser requireExpiration(Date expiration);
102+
JwtParser requireExpiration(Instant expiration);
103103

104104
/**
105105
* Ensures that the specified {@code nbf} exists in the parsed JWT. If missing or if the parsed
@@ -111,7 +111,7 @@ public interface JwtParser {
111111
* @see MissingClaimException
112112
* @see IncorrectClaimException
113113
*/
114-
JwtParser requireNotBefore(Date notBefore);
114+
JwtParser requireNotBefore(Instant notBefore);
115115

116116
/**
117117
* Ensures that the specified {@code claimName} exists in the parsed JWT. If missing or if the parsed

src/main/java/io/jsonwebtoken/impl/DefaultClaims.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.jsonwebtoken.Claims;
1919
import io.jsonwebtoken.RequiredTypeException;
2020

21-
import java.util.Date;
21+
import java.time.Instant;
2222
import java.util.Map;
2323

2424
public class DefaultClaims extends JwtMap implements Claims {
@@ -65,35 +65,35 @@ public Claims setAudience(String aud) {
6565
}
6666

6767
@Override
68-
public Date getExpiration() {
69-
return get(Claims.EXPIRATION, Date.class);
68+
public Instant getExpiration() {
69+
return get(Claims.EXPIRATION, Instant.class);
7070
}
7171

7272
@Override
73-
public Claims setExpiration(Date exp) {
74-
setDate(Claims.EXPIRATION, exp);
73+
public Claims setExpiration(Instant exp) {
74+
setInstant(Claims.EXPIRATION, exp);
7575
return this;
7676
}
7777

7878
@Override
79-
public Date getNotBefore() {
80-
return get(Claims.NOT_BEFORE, Date.class);
79+
public Instant getNotBefore() {
80+
return get(Claims.NOT_BEFORE, Instant.class);
8181
}
8282

8383
@Override
84-
public Claims setNotBefore(Date nbf) {
85-
setDate(Claims.NOT_BEFORE, nbf);
84+
public Claims setNotBefore(Instant nbf) {
85+
setInstant(Claims.NOT_BEFORE, nbf);
8686
return this;
8787
}
8888

8989
@Override
90-
public Date getIssuedAt() {
91-
return get(Claims.ISSUED_AT, Date.class);
90+
public Instant getIssuedAt() {
91+
return get(Claims.ISSUED_AT, Instant.class);
9292
}
9393

9494
@Override
95-
public Claims setIssuedAt(Date iat) {
96-
setDate(Claims.ISSUED_AT, iat);
95+
public Claims setIssuedAt(Instant iat) {
96+
setInstant(Claims.ISSUED_AT, iat);
9797
return this;
9898
}
9999

@@ -117,15 +117,15 @@ public <T> T get(String claimName, Class<T> requiredType) {
117117
Claims.ISSUED_AT.equals(claimName) ||
118118
Claims.NOT_BEFORE.equals(claimName)
119119
) {
120-
value = getDate(claimName);
120+
value = getInstant(claimName);
121121
}
122122

123123
return castClaimValue(value, requiredType);
124124
}
125125

126126
private <T> T castClaimValue(Object value, Class<T> requiredType) {
127-
if (requiredType == Date.class && value instanceof Long) {
128-
value = new Date((Long)value);
127+
if (requiredType == Instant.class && value instanceof Number) {
128+
value = Instant.ofEpochSecond(Long.parseLong(value.toString()));
129129
}
130130

131131
if (value instanceof Integer) {

src/main/java/io/jsonwebtoken/impl/DefaultClock.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import io.jsonwebtoken.Clock;
44

5-
import java.util.Date;
5+
import java.time.Instant;
66

77
/**
88
* Default {@link Clock} implementation.
@@ -17,12 +17,12 @@ public class DefaultClock implements Clock {
1717
public static final Clock INSTANCE = new DefaultClock();
1818

1919
/**
20-
* Simply returns <code>new {@link Date}()</code>.
20+
* Simply returns <code>new {@link Instant}()</code>.
2121
*
22-
* @return a new {@link Date} instance.
22+
* @return a new {@link Instant} instance.
2323
*/
2424
@Override
25-
public Date now() {
26-
return new Date();
25+
public Instant now() {
26+
return Instant.now();
2727
}
2828
}

0 commit comments

Comments
 (0)