View Javadoc
1   package cn.home1.oss.lib.common.crypto;
2   
3   import io.jsonwebtoken.SignatureAlgorithm;
4   import io.jsonwebtoken.impl.TextCodec;
5   
6   import lombok.Getter;
7   
8   import javax.crypto.SecretKey;
9   import javax.crypto.spec.SecretKeySpec;
10  
11  /**
12   * Created by zhanghaolun on 16/11/17.
13   */
14  public class JwtKey {
15  
16    @Getter
17    private final KeyExpression keyExpression;
18    @Getter
19    private final SignatureAlgorithm signatureAlgorithm;
20    @Getter
21    private final SecretKey signatureKey;
22  
23    public JwtKey(final KeyExpression keyExpression) {
24      this.keyExpression = keyExpression;
25      this.signatureAlgorithm = SignatureAlgorithm.forName(this.keyExpression.getSpec());
26      this.signatureKey = new SecretKeySpec( //
27        TextCodec.BASE64.decode(this.keyExpression.getValue()), //
28        this.signatureAlgorithm.getJcaName() //
29      );
30    }
31  
32    public JwtKey(final String keyExpression) {
33      this(new KeyExpression(keyExpression));
34    }
35  }