下面列出了怎么用io.jsonwebtoken.RequiredTypeException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@SuppressWarnings("unchecked")
public List<String> getAudiences() {
// https://tools.ietf.org/html/rfc7519#page-9
List<String> audiences;
try {
// returns null if not found
audiences = this.body.get(Claims.AUDIENCE, ArrayList.class);
} catch (RequiredTypeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("DefaultOAuthJwtAccessToken:getAudiences treat audience as string, err: " + e.getMessage());
}
// found but class mismatch
audiences = Arrays.asList(new String[]{ this.body.getAudience() });
}
return audiences;
}
@Override
public String getCertificateThumbprint() {
// https://github.com/jwtk/jjwt/issues/404, custom model class not supported
LinkedHashMap<?, ?> certConf = null;
try {
certConf = this.body.get(OAuthJwtAccessToken.CLAIM_CONFIRM, LinkedHashMap.class);
if (certConf == null) {
return null;
}
} catch (RequiredTypeException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("DefaultOAuthJwtAccessToken:getCertificateThumbprint expected data type to be JSON object, err: " + e.getMessage());
}
return null;
}
return (String) certConf.get(OAuthJwtAccessToken.CLAIM_CONFIRM_X509_HASH);
}
private <T> T castClaimValue(Object value, Class<T> requiredType) {
if (value instanceof Integer) {
int intValue = (Integer) value;
if (requiredType == Long.class) {
value = (long) intValue;
} else if (requiredType == Short.class && Short.MIN_VALUE <= intValue && intValue <= Short.MAX_VALUE) {
value = (short) intValue;
} else if (requiredType == Byte.class && Byte.MIN_VALUE <= intValue && intValue <= Byte.MAX_VALUE) {
value = (byte) intValue;
}
}
if (!requiredType.isInstance(value)) {
throw new RequiredTypeException(String.format(CONVERSION_ERROR_MSG, value.getClass(), requiredType));
}
return requiredType.cast(value);
}
/**
* Creates a new device for a token.
* <p>
* The token is expected to contain the device identifier in the <em>sub</em> claim and
* the tenant identifier in the <em>ten</em> claim.
*
* @param token The token asserting the device's identity.
* @throws NullPointerException if the token does not contain a tenant and device identifier.
*/
public Device(final Jws<Claims> token) {
this(Objects.requireNonNull(token).getBody().get("ten", String.class), token.getBody().getSubject());
try {
final Set<?> aut = token.getBody().get("aut", Set.class);
if (aut != null) {
authorities.addAll(aut);
}
} catch (final RequiredTypeException e) {
// token contains no authorities claim
}
}
@Override
public String getCertificateThumbprint() {
LinkedHashMap<?, ?> certConf = null;
try {
certConf = this.body.get(Auth0Jwt.claimConfirm, LinkedHashMap.class);
if (certConf == null) {
return null;
}
} catch (RequiredTypeException e) {
return null;
}
return (String) certConf.get(OAuthJwtAccessToken.CLAIM_CONFIRM_X509_HASH);
}