| | |
| | | import com.auth0.jwt.algorithms.Algorithm; |
| | | import com.auth0.jwt.interfaces.Claim; |
| | | import com.auth0.jwt.interfaces.DecodedJWT; |
| | | import com.example.jz.modle.entity.User; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Calendar; |
| | |
| | | Claim username = verify.getClaim("username"); |
| | | return username.asString(); |
| | | } |
| | | public String createToken(User user) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.add(Calendar.SECOND, tokenEcpiration); |
| | | |
| | | return JWT.create() |
| | | .withKeyId(user.getId().toString()) |
| | | .withHeader(new HashMap<>()) |
| | | .withClaim("username", user.getLoginUsername()) |
| | | .withExpiresAt(calendar.getTime()) |
| | | .sign(Algorithm.HMAC256(tokenSignKey)); |
| | | } |
| | | |
| | | } |
| | | |