Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring-security codec ignore error #12192

Merged
merged 14 commits into from
Apr 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
Expand Down Expand Up @@ -52,6 +53,12 @@ private void setSecurityContext(Invocation invocation) {

Authentication authentication = context.getAuthentication();

invocation.setObjectAttachment(SecurityNames.SECURITY_AUTHENTICATION_CONTEXT_KEY, mapper.serialize(authentication));
String content = mapper.serialize(authentication);

if (StringUtils.isBlank(content)) {
return;
}

invocation.setObjectAttachment(SecurityNames.SECURITY_AUTHENTICATION_CONTEXT_KEY, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ private void getSecurityContext(Invocation invocation) {
if (StringUtils.isBlank(authenticationJSON)) {
return;
}

Authentication authentication = mapper.deserialize(authenticationJSON, Authentication.class);

if (authentication == null) {
return;
}

SecurityContextHolder.getContext().setAuthentication(authentication);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.springframework.security.jackson2.CoreJackson2Module;
Expand All @@ -30,6 +33,8 @@

public class ObjectMapperCodec {

private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ObjectMapperCodec.class);

private final ObjectMapper mapper = new ObjectMapper();

public ObjectMapperCodec() {
Expand All @@ -38,17 +43,16 @@ public ObjectMapperCodec() {

public <T> T deserialize(byte[] bytes, Class<T> clazz) {
try {

if (bytes == null || bytes.length == 0) {
return null;
}

return mapper.readValue(bytes, clazz);

} catch (Exception exception) {
throw new RuntimeException(
String.format("objectMapper! deserialize error %s", exception));
logger.warn(LoggerCodeConstants.COMMON_JSON_CONVERT_EXCEPTION, "objectMapper! deserialize error, you can try to customize the ObjectMapperCodecCustomer.","","", exception);
}
return null;
}

public <T> T deserialize(String content, Class<T> clazz) {
Expand All @@ -68,8 +72,10 @@ public String serialize(Object object) {
return mapper.writeValueAsString(object);

} catch (Exception ex) {
throw new RuntimeException(String.format("objectMapper! serialize error %s", ex));
logger.warn(LoggerCodeConstants.COMMON_JSON_CONVERT_EXCEPTION, "objectMapper! serialize error, you can try to customize the ObjectMapperCodecCustomer.","","", ex);

}
return null;
}

public ObjectMapperCodec addModule(SimpleModule simpleModule) {
Expand Down