Skip to content

Commit

Permalink
Merge pull request #15 from IlyaLisov/dependabot/maven/jjwt.version-0…
Browse files Browse the repository at this point in the history
….12.3

Bump jjwt.version from 0.11.5 to 0.12.3
  • Loading branch information
IlyaLisov authored Jan 15, 2024
2 parents d6eb3ae + c4b5b82 commit 08840ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
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.5.Final</mapstruct.version>
<hibernate-validator.version>8.0.1.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

0 comments on commit 08840ca

Please sign in to comment.