Skip to content

Commit

Permalink
fix: switch to verifyWith instead of deprecated setSigningKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Sep 25, 2024
1 parent 99b0fa6 commit 929f05a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;

Expand Down Expand Up @@ -44,7 +45,7 @@ public Authentication getAuthentication(String token) {
}

public String getUsername(String token) {
return Jwts.parser().setSigningKey(key).build().parseSignedClaims(token).getPayload().getSubject();
return Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token).getPayload().getSubject();
}

public String resolveToken(HttpServletRequest req) {
Expand All @@ -57,7 +58,7 @@ public String resolveToken(HttpServletRequest req) {

public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(key).build().parseSignedClaims(token);
Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token);
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new CustomJwtException("Expired or invalid JWT token", HttpStatus.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;

Expand Down Expand Up @@ -44,7 +45,7 @@ public Authentication getAuthentication(String token) {
}

public String getUsername(String token) {
return Jwts.parser().setSigningKey(key).build().parseSignedClaims(token).getPayload().getSubject();
return Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token).getPayload().getSubject();
}

public String resolveToken(HttpServletRequest req) {
Expand All @@ -57,7 +58,7 @@ public String resolveToken(HttpServletRequest req) {

public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(key).build().parseSignedClaims(token);
Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token);
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new CustomJwtException("Expired or invalid JWT token", HttpStatus.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;

Expand Down Expand Up @@ -44,7 +45,7 @@ public Authentication getAuthentication(String token) {
}

public String getUsername(String token) {
return Jwts.parser().setSigningKey(key).build().parseSignedClaims(token).getPayload().getSubject();
return Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token).getPayload().getSubject();
}

public String resolveToken(HttpServletRequest req) {
Expand All @@ -57,7 +58,7 @@ public String resolveToken(HttpServletRequest req) {

public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(key).build().parseSignedClaims(token);
Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token);
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new CustomJwtException("Expired or invalid JWT token", HttpStatus.UNAUTHORIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import javax.crypto.SecretKey;
import java.security.Key;
import java.util.Base64;

Expand Down Expand Up @@ -44,7 +45,7 @@ public Authentication getAuthentication(String token) {
}

public String getUsername(String token) {
return Jwts.parser().setSigningKey(key).build().parseSignedClaims(token).getPayload().getSubject();
return Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token).getPayload().getSubject();
}

public String resolveToken(HttpServletRequest req) {
Expand All @@ -57,7 +58,7 @@ public String resolveToken(HttpServletRequest req) {

public boolean validateToken(String token) {
try {
Jwts.parser().setSigningKey(key).build().parseSignedClaims(token);
Jwts.parser().verifyWith((SecretKey) key).build().parseSignedClaims(token);
return true;
} catch (JwtException | IllegalArgumentException e) {
throw new CustomJwtException("Expired or invalid JWT token", HttpStatus.UNAUTHORIZED);
Expand Down

0 comments on commit 929f05a

Please sign in to comment.