tradeshark commented on issue #11603:
URL: https://github.com/apache/dubbo/issues/11603#issuecomment-2954467164
我遇到的也是反序列化问题,通过 @lizf2014 说的自定义ObjectMapperCodecCustomer能解决序列化和反序列化问题:
```
@Activate(
onClass = {
SECURITY_CONTEXT_HOLDER_CLASS_NAME,
CORE_JACKSON_2_MODULE_CLASS_NAME,
OBJECT_MAPPER_CLASS_NAME
})
public class DubboJacksonCustomizer implements ObjectMapperCodecCustomer {
@Override
public void customize(ObjectMapperCodec objectMapperCodec) {
objectMapperCodec.configureMapper(om -> {
om.addMixIn(JwtAuthenticationToken.class,
JwtAuthenticationTokenMixin.class);
});
}
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonDeserialize(using = JwtAuthenticationTokenDeserializer.class)
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.ANY,
getterVisibility = JsonAutoDetect.Visibility.NONE,
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class JwtAuthenticationTokenMixin {}
static class JwtAuthenticationTokenDeserializer extends
JsonDeserializer<JwtAuthenticationToken> {
public JwtAuthenticationTokenDeserializer() {
}
static final TypeReference<List<GrantedAuthority>> authoritiesRef =
new TypeReference<List<GrantedAuthority>>() {
};
@Override
public JwtAuthenticationToken deserialize(JsonParser parser,
DeserializationContext context) throws
IOException {
ObjectMapper mapper = (ObjectMapper) parser.getCodec();
JsonNode root = mapper.readTree(parser);
return deserialize(parser, mapper, root);
}
private JwtAuthenticationToken deserialize(JsonParser parser,
ObjectMapper mapper, JsonNode root)
throws JsonParseException {
JsonNode principal = JsonNodeUtils.findObjectNode(root,
"principal");
if (!Objects.isNull(principal)) {
String tokenValue = principal.get("tokenValue").textValue();
long issuedAt = principal.get("issuedAt").longValue();
long expiresAt = principal.get("expiresAt").longValue();
Map<String, Object> headers = JsonNodeUtils.findValue(
principal, "headers",
JsonNodeUtils.STRING_OBJECT_MAP, mapper);
Map<String, Object> claims = new java.util.HashMap<>();
claims = mapper.convertValue(principal.get("claims"),
Map.class);
org.springframework.security.oauth2.jwt.Jwt jwt =
new
org.springframework.security.oauth2.jwt.Jwt(tokenValue,
Instant.ofEpochMilli(issuedAt),
Instant.ofEpochMilli(expiresAt), headers,
claims);
List<GrantedAuthority> authorities =
mapper.convertValue(root.get("authorities"),
authoritiesRef);
JwtAuthenticationToken jwtAuthenticationToken = new
JwtAuthenticationToken(jwt, authorities);
jwtAuthenticationToken.setAuthenticated(true);
jwtAuthenticationToken.setDetails(
mapper.convertValue(root.get("details"),
WebAuthenticationDetails.class));
return jwtAuthenticationToken;
}
return null;
}
}
public static class JsonNodeUtils {
static final TypeReference<Set<String>> STRING_SET = new
TypeReference<Set<String>>() {
};
static final TypeReference<Map<String, Object>> STRING_OBJECT_MAP =
new TypeReference<Map<String, Object>>() {
};
static String findStringValue(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode value = jsonNode.findValue(fieldName);
return (value != null && value.isTextual()) ? value.asText() :
null;
}
static <T> T findValue(JsonNode jsonNode, String fieldName,
TypeReference<T> valueTypeReference,
ObjectMapper mapper) {
if (jsonNode == null) {
return null;
}
JsonNode value = jsonNode.findValue(fieldName);
return (value != null && value.isContainerNode()) ?
mapper.convertValue(value, valueTypeReference) : null;
}
static JsonNode findObjectNode(JsonNode jsonNode, String fieldName) {
if (jsonNode == null) {
return null;
}
JsonNode value = jsonNode.findValue(fieldName);
return (value != null && value.isObject()) ? value : null;
}
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]