4
4
import io .jsonwebtoken .JwtParser ;
5
5
import io .jsonwebtoken .Jwts ;
6
6
import io .jsonwebtoken .SignatureAlgorithm ;
7
+ import io .jsonwebtoken .io .Encoders ;
7
8
import jakarta .annotation .PostConstruct ;
8
9
import lombok .extern .slf4j .Slf4j ;
9
10
import org .lowcoder .domain .user .model .User ;
12
13
import org .springframework .stereotype .Component ;
13
14
import org .springframework .web .server .ServerWebExchange ;
14
15
15
- import java .util .Random ;
16
-
17
16
import java .util .Date ;
18
17
19
18
@ Component
@@ -25,12 +24,17 @@ public class JWTUtils {
25
24
26
25
private JwtParser jwtParser ;
27
26
27
+ private String base64EncodedSecret ;
28
+
28
29
private final String TOKEN_HEADER = "Authorization" ;
29
30
private final String TOKEN_PREFIX = "Bearer " ;
30
31
31
32
@ PostConstruct
32
33
public void setup (){
33
- this .jwtParser = Jwts .parser ().setSigningKey (authProperties .getApiKey ().getSecret ());
34
+ base64EncodedSecret = Encoders .BASE64 .encode (authProperties .getApiKey ().getSecret ().getBytes ());
35
+ this .jwtParser = Jwts .parserBuilder ()
36
+ .setSigningKey (base64EncodedSecret )
37
+ .build ();
34
38
}
35
39
36
40
public String createToken (User user ) {
@@ -39,10 +43,9 @@ public String createToken(User user) {
39
43
.setIssuedAt (new Date ());
40
44
claims .put ("userId" , user .getId () );
41
45
claims .put ("createdBy" , user .getName ());
42
- String randomFactor = String .valueOf (new Random ().nextLong (100000000L ));
43
46
return Jwts .builder ()
44
47
.setClaims (claims )
45
- .signWith (SignatureAlgorithm .HS256 , authProperties . getApiKey (). getSecret () + randomFactor )
48
+ .signWith (SignatureAlgorithm .HS256 , base64EncodedSecret )
46
49
.compact ();
47
50
}
48
51
0 commit comments