mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-24 13:02:13 +00:00
Fix UsernamePasswordAuthenticationTokenDeserializer to handle customized object mapper inclusion settings
Resolves #4698
This commit is contained in:
parent
673a2adf26
commit
fe40e6d65a
@ -41,6 +41,7 @@ import org.springframework.security.core.GrantedAuthority;
|
|||||||
*
|
*
|
||||||
* @author Jitendra Singh
|
* @author Jitendra Singh
|
||||||
* @author Greg Turnquist
|
* @author Greg Turnquist
|
||||||
|
* @author Onur Kagan Ozcan
|
||||||
* @see UsernamePasswordAuthenticationTokenMixin
|
* @see UsernamePasswordAuthenticationTokenMixin
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
@ -69,7 +70,7 @@ class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<U
|
|||||||
}
|
}
|
||||||
JsonNode credentialsNode = readJsonNode(jsonNode, "credentials");
|
JsonNode credentialsNode = readJsonNode(jsonNode, "credentials");
|
||||||
Object credentials;
|
Object credentials;
|
||||||
if (credentialsNode.isNull()) {
|
if (credentialsNode.isNull() || credentialsNode.isMissingNode()) {
|
||||||
credentials = null;
|
credentials = null;
|
||||||
} else {
|
} else {
|
||||||
credentials = credentialsNode.asText();
|
credentials = credentialsNode.asText();
|
||||||
@ -83,7 +84,7 @@ class UsernamePasswordAuthenticationTokenDeserializer extends JsonDeserializer<U
|
|||||||
token = new UsernamePasswordAuthenticationToken(principal, credentials);
|
token = new UsernamePasswordAuthenticationToken(principal, credentials);
|
||||||
}
|
}
|
||||||
JsonNode detailsNode = readJsonNode(jsonNode, "details");
|
JsonNode detailsNode = readJsonNode(jsonNode, "details");
|
||||||
if (detailsNode.isNull()) {
|
if (detailsNode.isNull() || detailsNode.isMissingNode()) {
|
||||||
token.setDetails(null);
|
token.setDetails(null);
|
||||||
} else {
|
} else {
|
||||||
token.setDetails(detailsNode);
|
token.setDetails(detailsNode);
|
||||||
|
@ -29,11 +29,16 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
|
|||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.User;
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static com.fasterxml.jackson.annotation.JsonInclude.Include.ALWAYS;
|
||||||
|
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_ABSENT;
|
||||||
|
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
|
||||||
|
import static com.fasterxml.jackson.annotation.JsonInclude.Value.construct;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jitendra Singh
|
* @author Jitendra Singh
|
||||||
* @author Greg Turnquist
|
* @author Greg Turnquist
|
||||||
|
* @author Onur Kagan Ozcan
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixinTests {
|
public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixinTests {
|
||||||
@ -163,6 +168,20 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin
|
|||||||
assertThat(deserialized).isEqualTo(original);
|
assertThat(deserialized).isEqualTo(original);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void serializingThenDeserializingWithConfiguredObjectMapperShouldWork() throws IOException {
|
||||||
|
// given
|
||||||
|
this.mapper.setDefaultPropertyInclusion(construct(ALWAYS, NON_NULL)).setSerializationInclusion(NON_ABSENT);
|
||||||
|
UsernamePasswordAuthenticationToken original = new UsernamePasswordAuthenticationToken("Frodo", null);
|
||||||
|
|
||||||
|
// when
|
||||||
|
String serialized = this.mapper.writeValueAsString(original);
|
||||||
|
UsernamePasswordAuthenticationToken deserialized =
|
||||||
|
this.mapper.readValue(serialized, UsernamePasswordAuthenticationToken.class);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(deserialized).isEqualTo(original);
|
||||||
|
}
|
||||||
|
|
||||||
private UsernamePasswordAuthenticationToken createToken() {
|
private UsernamePasswordAuthenticationToken createToken() {
|
||||||
User user = createDefaultUser();
|
User user = createDefaultUser();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user