update version and refactoring
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
package com.ard333.springbootwebfluxjjwt.security;
|
||||
|
||||
import com.ard333.springbootwebfluxjjwt.model.User;
|
||||
import java.io.Serializable;
|
||||
import java.util.Base64;
|
||||
import java.security.Key;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -17,18 +20,23 @@ import org.springframework.stereotype.Component;
|
||||
* @author ard333
|
||||
*/
|
||||
@Component
|
||||
public class JWTUtil implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
public class JWTUtil {
|
||||
|
||||
@Value("${springbootwebfluxjjwt.jjwt.secret}")
|
||||
private String secret;
|
||||
|
||||
@Value("${springbootwebfluxjjwt.jjwt.expiration}")
|
||||
private String expirationTime;
|
||||
|
||||
|
||||
private Key key;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
this.key = Keys.hmacShaKeyFor(secret.getBytes());
|
||||
}
|
||||
|
||||
public Claims getAllClaimsFromToken(String token) {
|
||||
return Jwts.parser().setSigningKey(Base64.getEncoder().encodeToString(secret.getBytes())).parseClaimsJws(token).getBody();
|
||||
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
|
||||
}
|
||||
|
||||
public String getUsernameFromToken(String token) {
|
||||
@@ -55,12 +63,13 @@ public class JWTUtil implements Serializable {
|
||||
|
||||
final Date createdDate = new Date();
|
||||
final Date expirationDate = new Date(createdDate.getTime() + expirationTimeLong * 1000);
|
||||
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
.setSubject(username)
|
||||
.setIssuedAt(createdDate)
|
||||
.setExpiration(expirationDate)
|
||||
.signWith(SignatureAlgorithm.HS512, Base64.getEncoder().encodeToString(secret.getBytes()))
|
||||
.signWith(key)
|
||||
.compact();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user