com.rongjih.jwt.SignerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rj-jwt4j Show documentation
Show all versions of rj-jwt4j Show documentation
Pure Java implementation for JSON Web Token (JWT)
The newest version!
package com.rongjih.jwt;
import com.rongjih.jwt.signer.HS256;
import com.rongjih.jwt.signer.NONE;
import java.util.HashMap;
import java.util.Map;
/**
* The factory to get specific algorithm signer.
*
* @author RJ
* @since JDK1.8
*/
public class SignerFactory {
public static Signer get(Algorithm algorithm) {
if (algorithm == null || algorithm.equals(Algorithm.NONE)) {
return getInstance(Algorithm.NONE);
}
switch (algorithm) {
case HS256:
return new HS256();
default:
throw new SignException("The algorithm '" + algorithm.name() + "' is not support.");
}
}
private static Map signers = new HashMap<>();
private static Signer getInstance(Algorithm algorithm) {
if (signers.containsKey(algorithm)) {
return signers.get(algorithm);
} else {
Signer signer;
switch (algorithm) {
case NONE:
signer = new NONE();
break;
case HS256:
signer = new HS256();
break;
default:
throw new SignException("The algorithm '" + algorithm.name() + "' is not support.");
}
signers.put(algorithm, signer);
return signer;
}
}
}