Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump jjwt.version from 0.11.5 to 0.12.3 #15

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<mapstruct.version>1.5.3.Final</mapstruct.version>
<hibernate-validator.version>8.0.0.Final</hibernate-validator.version>
<jakarta-validation.version>3.0.2</jakarta-validation.version>
<jjwt.version>0.11.5</jjwt.version>
<jjwt.version>0.12.3</jjwt.version>
<liquibase.version>4.23.0</liquibase.version>
<springdoc.version>2.0.2</springdoc.version>
<preliquibase.version>1.4.0</preliquibase.version>
Expand Down Expand Up @@ -73,7 +73,7 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.security.Key;
import javax.crypto.SecretKey;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
Expand All @@ -34,7 +34,7 @@ public class JwtTokenProvider {

private final UserDetailsService userDetailsService;
private final UserService userService;
private Key key;
private SecretKey key;

@PostConstruct
public void init() {
Expand All @@ -44,14 +44,16 @@ public void init() {
public String createAccessToken(final Long userId,
final String username,
final Set<Role> roles) {
Claims claims = Jwts.claims().setSubject(username);
claims.put("id", userId);
claims.put("roles", resolveRoles(roles));
Claims claims = Jwts.claims()
.subject(username)
.add("id", userId)
.add("roles", resolveRoles(roles))
.build();
Instant validity = Instant.now()
.plus(jwtProperties.getAccess(), ChronoUnit.HOURS);
return Jwts.builder()
.setClaims(claims)
.setExpiration(Date.from(validity))
.claims(claims)
.expiration(Date.from(validity))
.signWith(key)
.compact();
}
Expand All @@ -63,13 +65,15 @@ private List<String> resolveRoles(final Set<Role> roles) {
}

public String createRefreshToken(final Long userId, final String username) {
Claims claims = Jwts.claims().setSubject(username);
claims.put("id", userId);
Claims claims = Jwts.claims()
.subject(username)
.add("id", userId)
.build();
Instant validity = Instant.now()
.plus(jwtProperties.getRefresh(), ChronoUnit.DAYS);
return Jwts.builder()
.setClaims(claims)
.setExpiration(Date.from(validity))
.claims(claims)
.expiration(Date.from(validity))
.signWith(key)
.compact();
}
Expand All @@ -94,31 +98,30 @@ public JwtResponse refreshUserTokens(final String refreshToken) {

public boolean validateToken(final String token) {
Jws<Claims> claims = Jwts
.parserBuilder()
.setSigningKey(key)
.parser()
.verifyWith(key)
.build()
.parseClaimsJws(token);
return !claims.getBody().getExpiration().before(new Date());
.parseSignedClaims(token);
return claims.getPayload().getExpiration().after(new Date());
}

private String getId(final String token) {
return Jwts
.parserBuilder()
.setSigningKey(key)
.parser()
.verifyWith(key)
.build()
.parseClaimsJws(token)
.getBody()
.get("id")
.toString();
.parseSignedClaims(token)
.getPayload()
.get("id", String.class);
}

private String getUsername(final String token) {
return Jwts
.parserBuilder()
.setSigningKey(key)
.parser()
.verifyWith(key)
.build()
.parseClaimsJws(token)
.getBody()
.parseSignedClaims(token)
.getPayload()
.getSubject();
}

Expand Down
Loading