![JAR search and dependency download from the Maven repository](/logo.png)
com.healthy.common.security.jackson2.OAuth2UserAuthenticationTokenDeserializer Maven / Gradle / Ivy
The newest version!
package com.healthy.common.security.jackson2;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.healthy.common.security.authentication.OAuth2UserAuthenticationToken;
import com.healthy.common.security.userdetails.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.io.IOException;
import java.util.Collection;
/**
* 自定义的 OAuth2UserAuthenticationToken jackson 反序列化器
*
* @author xm.z
*/
public class OAuth2UserAuthenticationTokenDeserializer extends JsonDeserializer {
private static final TypeReference> SIMPLE_GRANTED_AUTHORITY_SET = new TypeReference>() {
};
/**
* This method will create {@link org.springframework.security.core.userdetails.User}
* object. It will ensure successful object creation even if password key is null in
* serialized json, because credentials may be removed from the
* {@link org.springframework.security.core.userdetails.User} by invoking
* {@link org.springframework.security.core.userdetails.User#eraseCredentials()}. In
* that case there won't be any password key in serialized json.
* @param jsonParser the JsonParser
* @param context the DeserializationContext
* @return the user
* @throws IOException if a exception during IO occurs
* @throws JsonProcessingException if an error during JSON processing occurs
*/
@Override
public OAuth2UserAuthenticationToken deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
JsonNode jsonNode = mapper.readTree(jsonParser);
User principal = mapper.treeToValue(jsonNode.get("principal"), User.class);
Collection extends GrantedAuthority> authorities = mapper.convertValue(jsonNode.get("authorities"),
SIMPLE_GRANTED_AUTHORITY_SET);
return new OAuth2UserAuthenticationToken(principal, authorities);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy