Skip to content

Commit bd345fb

Browse files
committed
Polish gh-11758
1 parent 5c55039 commit bd345fb

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

web/src/main/java/org/springframework/security/web/jackson2/SwitchUserGrantedAuthorityMixIn.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,15 +29,15 @@
2929
* Jackson mixin class to serialize/deserialize {@link SwitchUserGrantedAuthority}.
3030
*
3131
* @author Markus Heiden
32-
* @since 5.8
32+
* @since 6.3
3333
* @see WebServletJackson2Module
3434
* @see org.springframework.security.jackson2.SecurityJackson2Modules
3535
*/
36-
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
37-
@JsonIgnoreProperties(ignoreUnknown = true)
36+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
3837
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
3938
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
40-
public abstract class SwitchUserGrantedAuthorityMixIn {
39+
@JsonIgnoreProperties(ignoreUnknown = true)
40+
abstract class SwitchUserGrantedAuthorityMixIn {
4141

4242
@JsonCreator
4343
SwitchUserGrantedAuthorityMixIn(@JsonProperty("role") String role, @JsonProperty("source") Authentication source) {

web/src/main/java/org/springframework/security/web/jackson2/WebServletJackson2Module.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2016 the original author or authors.
2+
* Copyright 2015-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,12 +27,12 @@
2727
import org.springframework.security.web.savedrequest.SavedCookie;
2828

2929
/**
30-
* Jackson module for spring-security-web related to servlet. This module register
31-
* {@link CookieMixin}, {@link SavedCookieMixin}, {@link DefaultSavedRequestMixin} and
32-
* {@link WebAuthenticationDetailsMixin}. If no default typing enabled by default then
33-
* it'll enable it because typing info is needed to properly serialize/deserialize
34-
* objects. In order to use this module just add this module into your ObjectMapper
35-
* configuration.
30+
* Jackson module for spring-security-web related to servlet. This module registers
31+
* {@link CookieMixin}, {@link SavedCookieMixin}, {@link DefaultSavedRequestMixin},
32+
* {@link WebAuthenticationDetailsMixin}, and {@link SwitchUserGrantedAuthorityMixIn}. If
33+
* no default typing is enabled by default then it will be enabled, because typing info is
34+
* needed to properly serialize/deserialize objects. In order to use this module just add
35+
* this module into your ObjectMapper configuration.
3636
*
3737
* <pre>
3838
* ObjectMapper mapper = new ObjectMapper();

web/src/test/java/org/springframework/security/web/jackson2/SwitchUserGrantedAuthorityMixInTest.java renamed to web/src/test/java/org/springframework/security/web/jackson2/SwitchUserGrantedAuthorityMixInTests.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,9 +31,9 @@
3131

3232
/**
3333
* @author Markus Heiden
34-
* @since 5.8
34+
* @since 6.3
3535
*/
36-
public class SwitchUserGrantedAuthorityMixInTest extends AbstractMixinTests {
36+
public class SwitchUserGrantedAuthorityMixInTests extends AbstractMixinTests {
3737

3838
// language=JSON
3939
private static final String SWITCH_JSON = """
@@ -50,25 +50,24 @@ public class SwitchUserGrantedAuthorityMixInTest extends AbstractMixinTests {
5050
}
5151
}
5252
""".formatted(SimpleGrantedAuthorityMixinTests.AUTHORITIES_ARRAYLIST_JSON);
53-
SwitchUserGrantedAuthority expected;
5453

55-
Authentication source;
54+
private Authentication source;
5655

5756
@BeforeEach
58-
public void setupExpected() {
57+
public void setUp() {
5958
this.source = new UsernamePasswordAuthenticationToken("principal", "credentials",
6059
AuthorityUtils.createAuthorityList("ROLE_USER"));
61-
this.expected = new SwitchUserGrantedAuthority("switched", this.source);
6260
}
6361

6462
@Test
6563
public void serializeWhenPrincipalCredentialsAuthoritiesThenSuccess() throws Exception {
66-
String serializedJson = this.mapper.writeValueAsString(this.expected);
64+
SwitchUserGrantedAuthority expected = new SwitchUserGrantedAuthority("switched", this.source);
65+
String serializedJson = this.mapper.writeValueAsString(expected);
6766
JSONAssert.assertEquals(SWITCH_JSON, serializedJson, true);
6867
}
6968

7069
@Test
71-
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws Exception {
70+
public void deserializeWhenSourceIsUsernamePasswordAuthenticationTokenThenSuccess() throws Exception {
7271
SwitchUserGrantedAuthority deserialized = this.mapper.readValue(SWITCH_JSON, SwitchUserGrantedAuthority.class);
7372
assertThat(deserialized).isNotNull();
7473
assertThat(deserialized.getAuthority()).isEqualTo("switched");

0 commit comments

Comments
 (0)