Skip to content

Commit 89963ee

Browse files
committed
Creating a Spring Security Key for Signing a JWT Token
1 parent 1fea1bf commit 89963ee

3 files changed

Lines changed: 14 additions & 26 deletions

File tree

spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/jwtsignkey/jwtconfig/JwtUtils.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.jsonwebtoken.Jwts;
77
import io.jsonwebtoken.MalformedJwtException;
88
import io.jsonwebtoken.UnsupportedJwtException;
9+
import io.jsonwebtoken.io.Decoders;
910
import io.jsonwebtoken.security.Keys;
1011
import io.jsonwebtoken.security.SignatureException;
1112
import org.slf4j.Logger;
@@ -14,6 +15,7 @@
1415
import org.springframework.security.core.Authentication;
1516
import org.springframework.stereotype.Component;
1617

18+
import javax.crypto.SecretKey;
1719
import java.nio.charset.StandardCharsets;
1820
import java.security.Key;
1921
import java.util.Date;
@@ -28,15 +30,17 @@ public class JwtUtils {
2830
@Value("${baeldung.app.jwtExpirationMs}")
2931
private int jwtExpirationMs;
3032

33+
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtSecret));
34+
3135
public String generateJwtToken(Authentication authentication) {
3236

3337
UserDetailsImpl userPrincipal = (UserDetailsImpl) authentication.getPrincipal();
3438

3539
return Jwts.builder()
36-
.subject((userPrincipal.getUsername()))
37-
.issuedAt(new Date())
38-
.expiration(new Date((new Date()).getTime() + jwtExpirationMs))
39-
.signWith(getSigningKey())
40+
.subject((userPrincipal.getUsername()))
41+
.issuedAt(new Date())
42+
.expiration(new Date((new Date()).getTime() + jwtExpirationMs))
43+
.signWith(key)
4044
.compact();
4145

4246
}
@@ -48,18 +52,18 @@ private Key getSigningKey() {
4852

4953
public String getUserNameFromJwtToken(String token) {
5054
return Jwts.parser()
51-
.setSigningKey(getSigningKey())
52-
.build()
53-
.parseSignedClaims(token)
54-
.getPayload()
55+
.verifyWith(key)
56+
.build()
57+
.parseSignedClaims(token)
58+
.getPayload()
5559
.getSubject();
5660

5761
}
5862

5963
public boolean validateJwtToken(String authToken) {
6064
try {
6165
Jwts.parser()
62-
.setSigningKey(getSigningKey())
66+
. verifyWith(key)
6367
.build()
6468
.parseSignedClaims(authToken);
6569
return true;

spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/jwtsignkey/response/JwtResponse.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ public void setTokenType(String tokenType) {
2727
this.type = tokenType;
2828
}
2929

30-
public String getToken() {
31-
return token;
32-
}
33-
34-
public void setToken(String token) {
35-
this.token = token;
36-
}
37-
38-
public String getType() {
39-
return type;
40-
}
41-
42-
public void setType(String type) {
43-
this.type = type;
44-
}
45-
4630
public String getUsername() {
4731
return username;
4832
}

spring-security-modules/spring-security-core-2/src/main/java/com/baeldung/jwtsignkey/securityconfig/SecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SecurityConfiguration {
3333
@Autowired
3434
private AuthEntryPointJwt unauthorizedHandler;
3535

36-
private static final String[] WHITE_LIST_URL = { "/h2-console/**","/signin", "/signup", "/user-dashboard" };
36+
private static final String[] WHITE_LIST_URL = { "/h2-console/**", "/signin", "/signup", "/user-dashboard" };
3737

3838
@Bean
3939
public AuthTokenFilter authenticationJwtTokenFilter() {

0 commit comments

Comments
 (0)