Skip to content

Commit c1b0d09

Browse files
committed
Add WebFlux support for spring security web jackson module.
Fixes: gh-6303
1 parent a919b4e commit c1b0d09

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed

core/src/main/java/org/springframework/security/jackson2/SecurityJackson2Modules.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
* mapper.registerModule(new CasJackson2Module());
5959
* mapper.registerModule(new WebJackson2Module());
6060
* mapper.registerModule(new WebServletJackson2Module());
61+
* mapper.registerModule(new WebServerJackson2Module());
6162
* </pre>
6263
*
6364
* @author Jitendra Singh.
@@ -69,7 +70,8 @@ public final class SecurityJackson2Modules {
6970
private static final List<String> securityJackson2ModuleClasses = Arrays.asList(
7071
"org.springframework.security.jackson2.CoreJackson2Module",
7172
"org.springframework.security.cas.jackson2.CasJackson2Module",
72-
"org.springframework.security.web.jackson2.WebJackson2Module"
73+
"org.springframework.security.web.jackson2.WebJackson2Module",
74+
"org.springframework.security.web.server.jackson2.WebServerJackson2Module"
7375
);
7476
private static final String webServletJackson2ModuleClass =
7577
"org.springframework.security.web.jackson2.WebServletJackson2Module";
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2015-2018 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+
* http://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.server.jackson2;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21+
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
23+
24+
/**
25+
* Jackson mixin class to serialize/deserialize {@link org.springframework.security.web.server.csrf.DefaultCsrfToken}
26+
* serialization support.
27+
*
28+
* <pre>
29+
* ObjectMapper mapper = new ObjectMapper();
30+
* mapper.registerModule(new WebServerJackson2Module());
31+
* </pre>
32+
*
33+
* @author Boris Finkelshteyn
34+
* @see WebServerJackson2Module
35+
* @since 5.1
36+
*/
37+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
38+
@JsonIgnoreProperties(ignoreUnknown = true)
39+
class DefaultCsrfServerTokenMixin {
40+
41+
/**
42+
* JsonCreator constructor needed by Jackson to create {@link org.springframework.security.web.server.csrf.DefaultCsrfToken}
43+
* object.
44+
*
45+
* @param headerName the name of the header
46+
* @param parameterName the parameter name
47+
* @param token the CSRF token value
48+
*/
49+
@JsonCreator
50+
public DefaultCsrfServerTokenMixin(@JsonProperty("headerName") String headerName,
51+
@JsonProperty("parameterName") String parameterName, @JsonProperty("token") String token) {
52+
}
53+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2015-2018 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+
* http://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.server.jackson2;
18+
19+
import com.fasterxml.jackson.core.Version;
20+
import com.fasterxml.jackson.databind.module.SimpleModule;
21+
import org.springframework.security.jackson2.SecurityJackson2Modules;
22+
import org.springframework.security.web.server.csrf.DefaultCsrfToken;
23+
24+
/**
25+
* Jackson module for spring-security-web-flux. This module register {@link DefaultCsrfServerTokenMixin}
26+
* If no default typing enabled by default then it'll enable it because typing info is needed to
27+
* properly serialize/deserialize objects.
28+
* In order to use this module just add this module into your ObjectMapper configuration.
29+
*
30+
* <pre>
31+
* ObjectMapper mapper = new ObjectMapper();
32+
* mapper.registerModule(new WebServerJackson2Module());
33+
* </pre>
34+
* <b>Note: use {@link SecurityJackson2Modules#getModules(ClassLoader)} to get list of all security modules.</b>
35+
*
36+
* @author Boris Finkelshteyn
37+
* @see SecurityJackson2Modules
38+
* @since 5.1
39+
*/
40+
public class WebServerJackson2Module extends SimpleModule {
41+
42+
public WebServerJackson2Module() {
43+
super(WebServerJackson2Module.class.getName(), new Version(1, 0, 0, null, null, null));
44+
}
45+
46+
@Override
47+
public void setupModule(SetupContext context) {
48+
SecurityJackson2Modules.enableDefaultTyping(context.getOwner());
49+
context.setMixInAnnotations(DefaultCsrfToken.class, DefaultCsrfServerTokenMixin.class);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2015-2016 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+
* http://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.server.jackson2;
18+
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.JsonMappingException;
21+
import org.json.JSONException;
22+
import org.junit.Test;
23+
import org.skyscreamer.jsonassert.JSONAssert;
24+
import org.springframework.security.web.jackson2.AbstractMixinTests;
25+
import org.springframework.security.web.server.csrf.DefaultCsrfToken;
26+
27+
import java.io.IOException;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* @author Boris Finkelshteyn
33+
* @since 5.1
34+
*/
35+
public class DefaultCsrfServerTokenMixinTests extends AbstractMixinTests {
36+
37+
// @formatter:off
38+
private static final String CSRF_JSON = "{"
39+
+ "\"@class\": \"org.springframework.security.web.server.csrf.DefaultCsrfToken\", "
40+
+ "\"headerName\": \"csrf-header\", "
41+
+ "\"parameterName\": \"_csrf\", "
42+
+ "\"token\": \"1\""
43+
+ "}";
44+
// @formatter:on
45+
46+
@Test
47+
public void defaultCsrfTokenSerializedTest() throws JsonProcessingException, JSONException {
48+
DefaultCsrfToken token = new DefaultCsrfToken("csrf-header", "_csrf", "1");
49+
String serializedJson = mapper.writeValueAsString(token);
50+
JSONAssert.assertEquals(CSRF_JSON, serializedJson, true);
51+
}
52+
53+
@Test
54+
public void defaultCsrfTokenDeserializeTest() throws IOException {
55+
DefaultCsrfToken token = mapper.readValue(CSRF_JSON, DefaultCsrfToken.class);
56+
assertThat(token).isNotNull();
57+
assertThat(token.getHeaderName()).isEqualTo("csrf-header");
58+
assertThat(token.getParameterName()).isEqualTo("_csrf");
59+
assertThat(token.getToken()).isEqualTo("1");
60+
}
61+
62+
@Test(expected = JsonMappingException.class)
63+
public void defaultCsrfTokenDeserializeWithoutClassTest() throws IOException {
64+
String tokenJson = "{\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
65+
mapper.readValue(tokenJson, DefaultCsrfToken.class);
66+
}
67+
68+
@Test(expected = JsonMappingException.class)
69+
public void defaultCsrfTokenDeserializeNullValuesTest() throws IOException {
70+
String tokenJson = "{\"@class\": \"org.springframework.security.web.server.csrf.DefaultCsrfToken\", \"headerName\": \"\", \"parameterName\": null, \"token\": \"1\"}";
71+
mapper.readValue(tokenJson, DefaultCsrfToken.class);
72+
}
73+
}

0 commit comments

Comments
 (0)