Skip to content

Commit 5c55039

Browse files
markusheidensjohnr
authored andcommitted
Add SwitchUserGrantedAuthorityMixIn
Closes gh-11775
1 parent 8320669 commit 5c55039

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.jackson2;
18+
19+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
20+
import com.fasterxml.jackson.annotation.JsonCreator;
21+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
24+
25+
import org.springframework.security.core.Authentication;
26+
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;
27+
28+
/**
29+
* Jackson mixin class to serialize/deserialize {@link SwitchUserGrantedAuthority}.
30+
*
31+
* @author Markus Heiden
32+
* @since 5.8
33+
* @see WebServletJackson2Module
34+
* @see org.springframework.security.jackson2.SecurityJackson2Modules
35+
*/
36+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
37+
@JsonIgnoreProperties(ignoreUnknown = true)
38+
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
39+
isGetterVisibility = JsonAutoDetect.Visibility.NONE)
40+
public abstract class SwitchUserGrantedAuthorityMixIn {
41+
42+
@JsonCreator
43+
SwitchUserGrantedAuthorityMixIn(@JsonProperty("role") String role, @JsonProperty("source") Authentication source) {
44+
}
45+
46+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.security.jackson2.SecurityJackson2Modules;
2424
import org.springframework.security.web.authentication.WebAuthenticationDetails;
25+
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;
2526
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
2627
import org.springframework.security.web.savedrequest.SavedCookie;
2728

@@ -56,6 +57,7 @@ public void setupModule(SetupContext context) {
5657
context.setMixInAnnotations(SavedCookie.class, SavedCookieMixin.class);
5758
context.setMixInAnnotations(DefaultSavedRequest.class, DefaultSavedRequestMixin.class);
5859
context.setMixInAnnotations(WebAuthenticationDetails.class, WebAuthenticationDetailsMixin.class);
60+
context.setMixInAnnotations(SwitchUserGrantedAuthority.class, SwitchUserGrantedAuthorityMixIn.class);
5961
}
6062

6163
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.jackson2;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.skyscreamer.jsonassert.JSONAssert;
22+
23+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
24+
import org.springframework.security.core.Authentication;
25+
import org.springframework.security.core.authority.AuthorityUtils;
26+
import org.springframework.security.jackson2.AbstractMixinTests;
27+
import org.springframework.security.jackson2.SimpleGrantedAuthorityMixinTests;
28+
import org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* @author Markus Heiden
34+
* @since 5.8
35+
*/
36+
public class SwitchUserGrantedAuthorityMixInTest extends AbstractMixinTests {
37+
38+
// language=JSON
39+
private static final String SWITCH_JSON = """
40+
{
41+
"@class": "org.springframework.security.web.authentication.switchuser.SwitchUserGrantedAuthority",
42+
"role": "switched",
43+
"source": {
44+
"@class": "org.springframework.security.authentication.UsernamePasswordAuthenticationToken",
45+
"principal": "principal",
46+
"credentials": "credentials",
47+
"authenticated": true,
48+
"details": null,
49+
"authorities": %s
50+
}
51+
}
52+
""".formatted(SimpleGrantedAuthorityMixinTests.AUTHORITIES_ARRAYLIST_JSON);
53+
SwitchUserGrantedAuthority expected;
54+
55+
Authentication source;
56+
57+
@BeforeEach
58+
public void setupExpected() {
59+
this.source = new UsernamePasswordAuthenticationToken("principal", "credentials",
60+
AuthorityUtils.createAuthorityList("ROLE_USER"));
61+
this.expected = new SwitchUserGrantedAuthority("switched", this.source);
62+
}
63+
64+
@Test
65+
public void serializeWhenPrincipalCredentialsAuthoritiesThenSuccess() throws Exception {
66+
String serializedJson = this.mapper.writeValueAsString(this.expected);
67+
JSONAssert.assertEquals(SWITCH_JSON, serializedJson, true);
68+
}
69+
70+
@Test
71+
public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenMixinTest() throws Exception {
72+
SwitchUserGrantedAuthority deserialized = this.mapper.readValue(SWITCH_JSON, SwitchUserGrantedAuthority.class);
73+
assertThat(deserialized).isNotNull();
74+
assertThat(deserialized.getAuthority()).isEqualTo("switched");
75+
assertThat(deserialized.getSource()).isEqualTo(this.source);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)