mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-09 04:49:02 +08:00
🔥 Removing code or files. 删除关于 OAuth2.0 Jackson 序列化部分
This commit is contained in:
1 parent
f57f29d9ee
commit
f51a3f2ae3
14 files changed
-587
No files matched your search
-5
@@ -220,9 +220,4 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String AES = "aes";
|
||||
|
||||
/**
|
||||
* 是否开启 redis json 格式化
|
||||
*/
|
||||
boolean JSON_FORMAT = false;
|
||||
|
||||
}
|
||||
-117
@@ -1,117 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.util.OAuth2Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
public final class AccessTokenJackson2Deserializer extends StdDeserializer<OAuth2AccessToken> {
|
||||
|
||||
public AccessTokenJackson2Deserializer() {
|
||||
super(OAuth2AccessToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
|
||||
throws IOException {
|
||||
return myDeserializeWithType(p);
|
||||
}
|
||||
|
||||
private OAuth2AccessToken myDeserializeWithType(JsonParser jp) throws IOException {
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
String tokenValue = jsonNode.get(OAuth2AccessToken.ACCESS_TOKEN).asText();
|
||||
String tokenType = jsonNode.get(OAuth2AccessToken.TOKEN_TYPE).asText();
|
||||
String refreshToken = jsonNode.get(OAuth2AccessToken.REFRESH_TOKEN).asText();
|
||||
long expiresIn = jsonNode.get(OAuth2AccessToken.EXPIRES_IN).asLong();
|
||||
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(tokenValue);
|
||||
accessToken.setTokenType(tokenType);
|
||||
accessToken.setExpiration(new Date(System.currentTimeMillis() + expiresIn * 1000));
|
||||
if (refreshToken != null) {
|
||||
accessToken.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
|
||||
}
|
||||
// scope
|
||||
JsonNode scopeNode = jsonNode.get(OAuth2AccessToken.SCOPE);
|
||||
accessToken.setScope(parseScope(scopeNode));
|
||||
// 扩展字段
|
||||
accessToken.setAdditionalInformation(parseAdditionalInformation(mapper, jsonNode));
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OAuth2AccessToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
|
||||
return myDeserializeWithType(jp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 scope
|
||||
* @param scopeNode scope json
|
||||
* @return String Set
|
||||
*/
|
||||
private static Set<String> parseScope(JsonNode scopeNode) {
|
||||
// 空处理
|
||||
if (scopeNode == null || scopeNode.isNull()) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
// 字符串 x xx xxx 格式
|
||||
if (scopeNode.isTextual()) {
|
||||
return OAuth2Utils.parseParameterList(scopeNode.asText());
|
||||
}
|
||||
// 集合模型
|
||||
Set<String> scope = new TreeSet<>();
|
||||
Iterator<JsonNode> elements = scopeNode.elements();
|
||||
while (elements.hasNext()) {
|
||||
JsonNode next = elements.next();
|
||||
scope.add(next.asText());
|
||||
}
|
||||
return scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析扩展字段
|
||||
* @param mapper jackson 工具
|
||||
* @param jsonNode json String
|
||||
* @return map
|
||||
* @see com.pig4cloud.pigx.common.security.component.PigxTokenEnhancer
|
||||
*/
|
||||
private Map<String, Object> parseAdditionalInformation(ObjectMapper mapper, JsonNode jsonNode) {
|
||||
LinkedHashMap<String, Object> additionalInformation = new LinkedHashMap<>();
|
||||
|
||||
// 处理 user_info
|
||||
JsonNode userNode = jsonNode.get(SecurityConstants.DETAILS_USER);
|
||||
User user = mapper.convertValue(userNode, User.class);
|
||||
int deptId = userNode.get(SecurityConstants.DETAILS_DEPT_ID).asInt();
|
||||
int tenantId = userNode.get(SecurityConstants.DETAILS_TENANT_ID).asInt();
|
||||
int userId = userNode.get(SecurityConstants.DETAILS_USER_ID).asInt();
|
||||
String phone = userNode.get(SecurityConstants.DETAILS_PHONE).asText();
|
||||
String avatar = userNode.get(SecurityConstants.DETAILS_AVATAR).asText();
|
||||
PigxUser pigxUser = new PigxUser(userId, deptId, phone, avatar, tenantId, user.getUsername(), "N/A",
|
||||
user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(),
|
||||
user.getAuthorities());
|
||||
additionalInformation.put(SecurityConstants.DETAILS_USER, pigxUser);
|
||||
|
||||
// 处理 license 等扩展字段 【去掉OAuth2AccessToken 字段避免重复】
|
||||
additionalInformation.put(SecurityConstants.DETAILS_LICENSE,
|
||||
jsonNode.get(SecurityConstants.DETAILS_LICENSE).asText());
|
||||
additionalInformation.put(SecurityConstants.ACTIVE, jsonNode.get(SecurityConstants.ACTIVE).asBoolean());
|
||||
return additionalInformation;
|
||||
}
|
||||
|
||||
}
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 序列化
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
public final class AccessTokenJackson2Serializer extends StdSerializer<OAuth2AccessToken> {
|
||||
|
||||
public AccessTokenJackson2Serializer() {
|
||||
super(OAuth2AccessToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serializeWithType(OAuth2AccessToken value, JsonGenerator gen, SerializerProvider serializers,
|
||||
TypeSerializer typeSer) throws IOException {
|
||||
mySerializeWithType(value, gen, serializers, typeSer);
|
||||
}
|
||||
|
||||
private void mySerializeWithType(OAuth2AccessToken token, JsonGenerator gen, SerializerProvider serializers,
|
||||
TypeSerializer typeSer) throws IOException {
|
||||
gen.writeStartObject();
|
||||
if (typeSer != null) {
|
||||
gen.writeStringField(typeSer.getPropertyName(), token.getClass().getName());
|
||||
}
|
||||
Map<String, Object> tokenMap = new LinkedHashMap<>();
|
||||
tokenMap.put(OAuth2AccessToken.ACCESS_TOKEN, token.getValue());
|
||||
tokenMap.put(OAuth2AccessToken.TOKEN_TYPE, token.getTokenType());
|
||||
OAuth2RefreshToken refreshToken = token.getRefreshToken();
|
||||
if (refreshToken != null) {
|
||||
tokenMap.put(OAuth2AccessToken.REFRESH_TOKEN, refreshToken.getValue());
|
||||
}
|
||||
Date expiration = token.getExpiration();
|
||||
if (expiration != null) {
|
||||
long now = System.currentTimeMillis();
|
||||
tokenMap.put(OAuth2AccessToken.EXPIRES_IN, (expiration.getTime() - now) / 1000);
|
||||
}
|
||||
Set<String> scope = token.getScope();
|
||||
if (scope != null && !scope.isEmpty()) {
|
||||
StringBuilder scopes = new StringBuilder();
|
||||
for (String s : scope) {
|
||||
Assert.hasLength(s, "Scopes cannot be null or empty. Got " + scope);
|
||||
scopes.append(s);
|
||||
scopes.append(" ");
|
||||
}
|
||||
tokenMap.put(OAuth2AccessToken.SCOPE, scopes.substring(0, scopes.length() - 1));
|
||||
}
|
||||
Map<String, Object> additionalInformation = token.getAdditionalInformation();
|
||||
tokenMap.putAll(additionalInformation);
|
||||
for (String key : tokenMap.keySet()) {
|
||||
gen.writeObjectField(key, tokenMap.get(key));
|
||||
}
|
||||
gen.writeEndObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(OAuth2AccessToken token, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||
mySerializeWithType(token, gen, provider, null);
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
|
||||
/**
|
||||
* AccessTokenMixIn
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
|
||||
@JsonSerialize(using = AccessTokenJackson2Serializer.class)
|
||||
@JsonDeserialize(using = AccessTokenJackson2Deserializer.class)
|
||||
public class AccessTokenMixIn {
|
||||
|
||||
}
|
||||
-72
@@ -1,72 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
|
||||
import com.pig4cloud.pigx.common.security.mobile.MobileAuthenticationToken;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.jackson2.CoreJackson2Module;
|
||||
import org.springframework.security.jackson2.SimpleGrantedAuthorityMixin;
|
||||
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy;
|
||||
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* jackson 序列化
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
public class JacksonRedisTokenStoreSerializationStrategy extends BaseRedisTokenStoreSerializationStrategy {
|
||||
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public JacksonRedisTokenStoreSerializationStrategy() {
|
||||
this.mapper = new ObjectMapper();
|
||||
this.mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,
|
||||
JsonTypeInfo.As.PROPERTY);
|
||||
this.mapper.addMixIn(OAuth2AccessToken.class, AccessTokenMixIn.class);
|
||||
this.mapper.addMixIn(OAuth2Authentication.class, OAuth2AuthenticationMixin.class);
|
||||
this.mapper.addMixIn(PreAuthenticatedAuthenticationToken.class, PreAuthenticatedAuthenticationTokenMixin.class);
|
||||
this.mapper.addMixIn(MobileAuthenticationToken.class, MobileAuthenticationTokenMixin.class);
|
||||
this.mapper.addMixIn(SimpleGrantedAuthority.class, SimpleGrantedAuthorityMixin.class);
|
||||
this.mapper.registerModule(new CoreJackson2Module());
|
||||
this.mapper.addMixIn(DefaultExpiringOAuth2RefreshToken.class, OAuth2RefreshTokenMixin.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <T> T deserializeInternal(byte[] bytes, Class<T> clazz) {
|
||||
try {
|
||||
return mapper.readValue(bytes, clazz);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String deserializeStringInternal(byte[] bytes) {
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] serializeInternal(Object object) {
|
||||
try {
|
||||
return mapper.writeValueAsBytes(object);
|
||||
}
|
||||
catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte[] serializeInternal(String data) {
|
||||
return data.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.pig4cloud.pigx.common.security.mobile.MobileAuthenticationToken;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/10/21
|
||||
* <p>
|
||||
* MobileAuthenticationToken 反序列化处理
|
||||
*/
|
||||
public class MobileAuthenticationTokenDeserializer extends StdDeserializer<MobileAuthenticationToken> {
|
||||
|
||||
protected MobileAuthenticationTokenDeserializer() {
|
||||
super(MobileAuthenticationToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MobileAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
|
||||
Set<GrantedAuthority> authorities = getAuthorities(mapper, jsonNode);
|
||||
|
||||
User principal = mapper.convertValue(jsonNode.get("principal"), User.class);
|
||||
|
||||
return new MobileAuthenticationToken(principal, authorities);
|
||||
}
|
||||
|
||||
private Set<GrantedAuthority> getAuthorities(ObjectMapper mapper, JsonNode parentNode) {
|
||||
JsonNode authoritiesNode = parentNode.get("authorities");
|
||||
Set<GrantedAuthority> authorities = new HashSet<>(authoritiesNode.size());
|
||||
for (Iterator<JsonNode> authoritiesIt = authoritiesNode.get(1).elements(); authoritiesIt.hasNext();) {
|
||||
JsonNode authorityNode = authoritiesIt.next();
|
||||
SimpleGrantedAuthority authority = mapper.convertValue(authorityNode, SimpleGrantedAuthority.class);
|
||||
authorities.add(authority);
|
||||
}
|
||||
return authorities;
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/10/21
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
|
||||
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
@JsonDeserialize(using = MobileAuthenticationTokenDeserializer.class)
|
||||
public class MobileAuthenticationTokenMixin {
|
||||
|
||||
}
|
||||
-93
@@ -1,93 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Request;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* OAuth2Authentication 反序列化
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
public class OAuth2AuthenticationDeserializer extends StdDeserializer<OAuth2Authentication> {
|
||||
|
||||
protected OAuth2AuthenticationDeserializer() {
|
||||
super(OAuth2Authentication.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OAuth2Authentication deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
Set<GrantedAuthority> authorities = getAuthorities(mapper, jsonNode);
|
||||
JsonNode requestNode = jsonNode.get("storedRequest");
|
||||
Map<String, String> requestParameters = getMap(mapper, requestNode.get("requestParameters"));
|
||||
String clientId = requestNode.get("clientId").asText();
|
||||
boolean approved = requestNode.get("approved").asBoolean();
|
||||
Set<String> scope = getSet(mapper, requestNode.get("scope"));
|
||||
Set<String> resourceIds = getSet(mapper, requestNode.get("resourceIds"));
|
||||
String redirectUri = requestNode.get("redirectUri").asText(null);
|
||||
Set<String> responseTypes = getSet(mapper, requestNode.get("responseTypes"));
|
||||
Map<String, Serializable> extensionProperties = getMap(mapper, jsonNode.get("extensions"), Serializable.class);
|
||||
// 构造 OAuth2Request
|
||||
OAuth2Request request = new OAuth2Request(requestParameters, clientId, authorities, approved, scope,
|
||||
resourceIds, redirectUri, responseTypes, extensionProperties);
|
||||
JsonNode userAuthenticationNode = jsonNode.get("userAuthentication");
|
||||
// UsernamePasswordAuthenticationToken 反序列化
|
||||
Authentication authentication = null;
|
||||
if (userAuthenticationNode != null && !userAuthenticationNode.isMissingNode()) {
|
||||
authentication = (Authentication) mapper.convertValue(userAuthenticationNode, Object.class);
|
||||
}
|
||||
|
||||
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(request, authentication);
|
||||
oAuth2Authentication.setAuthenticated(true);
|
||||
return oAuth2Authentication;
|
||||
|
||||
}
|
||||
|
||||
private static Set<String> getSet(ObjectMapper mapper, JsonNode jsonNode) {
|
||||
return getSet(mapper, jsonNode, String.class);
|
||||
}
|
||||
|
||||
private static <T> Set<T> getSet(ObjectMapper mapper, JsonNode jsonNode, Class<T> valueClass) {
|
||||
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(Set.class, valueClass);
|
||||
return mapper.convertValue(jsonNode, collectionType);
|
||||
}
|
||||
|
||||
private static Map<String, String> getMap(ObjectMapper mapper, JsonNode jsonNode) {
|
||||
return getMap(mapper, jsonNode, String.class);
|
||||
}
|
||||
|
||||
private static <T> Map<String, T> getMap(ObjectMapper mapper, JsonNode jsonNode, Class<T> valueClass) {
|
||||
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, valueClass);
|
||||
return mapper.convertValue(jsonNode, mapType);
|
||||
}
|
||||
|
||||
private Set<GrantedAuthority> getAuthorities(ObjectMapper mapper, JsonNode parentNode) {
|
||||
JsonNode authoritiesNode = parentNode.get("authorities");
|
||||
Set<GrantedAuthority> authorities = new HashSet<>(authoritiesNode.size());
|
||||
for (Iterator<JsonNode> authoritiesIt = authoritiesNode.get(1).elements(); authoritiesIt.hasNext();) {
|
||||
JsonNode authorityNode = authoritiesIt.next();
|
||||
SimpleGrantedAuthority authority = mapper.convertValue(authorityNode, SimpleGrantedAuthority.class);
|
||||
authorities.add(authority);
|
||||
}
|
||||
return authorities;
|
||||
}
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
|
||||
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
@JsonDeserialize(using = OAuth2AuthenticationDeserializer.class)
|
||||
public class OAuth2AuthenticationMixin {
|
||||
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* DefaultExpiringOAuth2RefreshToken 反序列化
|
||||
*
|
||||
* @author dream.lu
|
||||
*/
|
||||
public class OAuth2RefreshTokenDeserializer extends StdDeserializer<DefaultExpiringOAuth2RefreshToken> {
|
||||
|
||||
protected OAuth2RefreshTokenDeserializer() {
|
||||
super(DefaultExpiringOAuth2RefreshToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DefaultExpiringOAuth2RefreshToken deserialize(JsonParser jp, DeserializationContext ctxt)
|
||||
throws IOException, JsonProcessingException {
|
||||
// {"@class":"org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken","value":"a94d2f04-d9be-49f9-9d9c-2bc7b34c8618","expiration":["java.util.Date",1605237988760]}
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
String value = jsonNode.get("value").asText();
|
||||
JsonNode expirationNode = jsonNode.get("expiration");
|
||||
Date date = mapper.convertValue(expirationNode, Date.class);
|
||||
return new DefaultExpiringOAuth2RefreshToken(value, date);
|
||||
}
|
||||
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
|
||||
@JsonDeserialize(using = OAuth2RefreshTokenDeserializer.class)
|
||||
public class OAuth2RefreshTokenMixin {
|
||||
|
||||
}
|
||||
-3
@@ -29,9 +29,6 @@ public class PigxTokenStoreAutoConfiguration {
|
||||
@Bean
|
||||
public TokenStore tokenStore() {
|
||||
PigxRedisTokenStore tokenStore = new PigxRedisTokenStore(connectionFactory, resolver);
|
||||
if (SecurityConstants.JSON_FORMAT) {
|
||||
tokenStore.setSerializationStrategy(new JacksonRedisTokenStoreSerializationStrategy());
|
||||
}
|
||||
tokenStore.setPrefix(SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX);
|
||||
tokenStore.setAuthenticationKeyGenerator(new DefaultAuthenticationKeyGenerator() {
|
||||
@Override
|
||||
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/10/21
|
||||
* <p>
|
||||
* PreAuthenticatedAuthenticationToken 反序列化处理
|
||||
*/
|
||||
public class PreAuthenticatedAuthenticationTokenDeserializer
|
||||
extends StdDeserializer<PreAuthenticatedAuthenticationToken> {
|
||||
|
||||
protected PreAuthenticatedAuthenticationTokenDeserializer() {
|
||||
super(PreAuthenticatedAuthenticationToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreAuthenticatedAuthenticationToken deserialize(JsonParser jp, DeserializationContext ctxt)
|
||||
throws IOException, JsonProcessingException {
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
|
||||
Set<GrantedAuthority> authorities = getAuthorities(mapper, jsonNode);
|
||||
|
||||
User principal = mapper.convertValue(jsonNode.get("principal"), User.class);
|
||||
|
||||
return new PreAuthenticatedAuthenticationToken(principal, "N/A", authorities);
|
||||
}
|
||||
|
||||
private Set<GrantedAuthority> getAuthorities(ObjectMapper mapper, JsonNode parentNode) {
|
||||
JsonNode authoritiesNode = parentNode.get("authorities");
|
||||
Set<GrantedAuthority> authorities = new HashSet<>(authoritiesNode.size());
|
||||
for (Iterator<JsonNode> authoritiesIt = authoritiesNode.get(1).elements(); authoritiesIt.hasNext();) {
|
||||
JsonNode authorityNode = authoritiesIt.next();
|
||||
SimpleGrantedAuthority authority = mapper.convertValue(authorityNode, SimpleGrantedAuthority.class);
|
||||
authorities.add(authority);
|
||||
}
|
||||
return authorities;
|
||||
}
|
||||
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.pig4cloud.pigx.common.security.serializer;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/10/21
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "@class")
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
|
||||
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
@JsonDeserialize(using = PreAuthenticatedAuthenticationTokenDeserializer.class)
|
||||
public class PreAuthenticatedAuthenticationTokenMixin {
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user