Class AccessKeyUtil

java.lang.Object
cn.vorbote.simplejwt.AccessKeyUtil

public class AccessKeyUtil extends Object
JWT Token implementation, easy to use.
Author:
vorbote thills@vorbote.cn
  • Constructor Details

    • AccessKeyUtil

      public AccessKeyUtil(@NonNull @NonNull JwtAlgorithm algorithm, @NonNull @NonNull String secret, @NonNull @NonNull String issuer)
  • Method Details

    • getSecret

      protected String getSecret()
    • setSecret

      protected void setSecret(String secret)
    • getIssuer

      protected String getIssuer()
    • setIssuer

      protected void setIssuer(String issuer)
    • getAlgorithm

      protected JwtAlgorithm getAlgorithm()
    • createToken

      public String createToken(TimeSpan expire, String subject, String[] audience, Map<String,Object> claims)
      Create a new Token. All the items in claims will be set as a String into this JSON Web Token.
      Parameters:
      expire - Specify when will the token be expired. (Unit: Second)
      subject - Specify the users will be faced.
      audience - Specify who will receive this token.
      claims - Give some info need to be transformed by token, can be null when you don't need to pass any information.
      Returns:
      A token string.
    • createTokenWithBean

      public String createTokenWithBean(TimeSpan expire, String subject, String[] audience, Object bean) throws Exception
      Create a new token with all data(except null data) in the bean.
      Parameters:
      expire - After this time span, this jwt will be expired and is not be able to use again.
      subject - The subject of this jwt.
      audience - The audience of this jwt.
      bean - The bean contains any possible users' data.
      Returns:
      A token generated by the data of the bean.
      Throws:
      Exception - Any possible exception may occurred in runtime, please check the upriver methods.
      See Also:
      • Class.getDeclaredFields()
      • Field.get(Object)
      • Field.setAccessible(boolean)
      • JWTCreator.Builder.withClaim(String, Boolean)
      • JWTCreator.Builder.withClaim(String, Date)
      • JWTCreator.Builder.withClaim(String, Double)
      • JWTCreator.Builder.withClaim(String, Integer)
      • JWTCreator.Builder.withClaim(String, List)
      • JWTCreator.Builder.withClaim(String, Long)
      • JWTCreator.Builder.withClaim(String, Map)
      • JWTCreator.Builder.withClaim(String, String)
    • verify

      public void verify(String token)
      Check whether the token is valid. This method will happen nothing when the token is valid, or throw some exception when token is invalid.
      Parameters:
      token - The token.
    • info

      public com.auth0.jwt.interfaces.DecodedJWT info(String token)
      Decode the token, and you can easily get some info from this token.
      Parameters:
      token - The token.
      Returns:
      The decoded jwt token.
      Throws:
      com.auth0.jwt.exceptions.AlgorithmMismatchException - If the algorithm stated in the token's header it's not equal to the one defined in the JWTVerifier.
      com.auth0.jwt.exceptions.SignatureVerificationException - If the signature is invalid.
      com.auth0.jwt.exceptions.TokenExpiredException - If the token has expired.
      com.auth0.jwt.exceptions.InvalidClaimException - If a claim contained a different value than the expected one.
      com.auth0.jwt.exceptions.JWTVerificationException - If any of the verification steps fail
      See Also:
      • JWTVerifier.verify(String)
    • renew

      @Deprecated public String renew(String token, int expireAfter)
      Deprecated.
      Renew the token.
      Parameters:
      token - The original token.
      expireAfter - The time period (seconds) when the new token expired.
      Returns:
      The renewed token.
    • renew

      public String renew(String token, TimeSpan expireAfter)
      Renew the token.
      Parameters:
      token - The original token.
      expireAfter - The time period (seconds) when the new token expired.
      Returns:
      The renewed token.
    • renewWithBean

      public String renewWithBean(String token, TimeSpan expireAfter, Class<?> requiredType) throws Exception
      Renew this token with the data in the bean. This method will auto distract the data needed by required type.
      Parameters:
      token - The original token.
      expireAfter - Expire after this time.
      requiredType - The required type's class.
      Returns:
      A new token.
      Throws:
      Exception - This method is using lots of methods could cause some exception, please see its upriver methods.
      See Also:
    • getBean

      public <T> T getBean(String token, Class<T> requiredType) throws Exception
      Get the bean in the token. In this method, you have to make sure that your stored info is in the format of key-value pair and which is stored in the claims. And the key must be the same with that field in the required type (such as the field name in required is declared as private String name;, then your key must be name). Meanwhile, the setter for this field is required either. Be advised, the field log and logger is thought as a helper field, thus, it will not be put into the token.
      Type Parameters:
      T - The type of the bean.
      Parameters:
      token - The user token.
      requiredType - The class of user.
      Returns:
      The user bean.
      Throws:
      Exception - This method is using lots of methods could cause some exception, please see its upriver methods.
      See Also: