Skip to content

Commit 9c7cab8

Browse files
finke-barwinch
authored andcommitted
Add conditionally servlet based support for spring security web jackson module.
1 parent f0402df commit 9c7cab8

File tree

7 files changed

+90
-26
lines changed

7 files changed

+90
-26
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* mapper.registerModule(new CoreJackson2Module());
5858
* mapper.registerModule(new CasJackson2Module());
5959
* mapper.registerModule(new WebJackson2Module());
60+
* mapper.registerModule(new WebServletJackson2Module());
6061
* </pre>
6162
*
6263
* @author Jitendra Singh.
@@ -70,6 +71,8 @@ public final class SecurityJackson2Modules {
7071
"org.springframework.security.cas.jackson2.CasJackson2Module",
7172
"org.springframework.security.web.jackson2.WebJackson2Module"
7273
);
74+
private static final String webServletJackson2ModuleClass =
75+
"org.springframework.security.web.jackson2.WebServletJackson2Module";
7376

7477
private SecurityJackson2Modules() {
7578
}
@@ -109,14 +112,26 @@ private static Module loadAndGetInstance(String className, ClassLoader loader) {
109112
public static List<Module> getModules(ClassLoader loader) {
110113
List<Module> modules = new ArrayList<>();
111114
for (String className : securityJackson2ModuleClasses) {
112-
Module module = loadAndGetInstance(className, loader);
113-
if (module != null) {
114-
modules.add(module);
115-
}
115+
addToModulesList(loader, modules, className);
116+
}
117+
if (ClassUtils.isPresent("javax.servlet.http.Cookie", loader)) {
118+
addToModulesList(loader, modules, webServletJackson2ModuleClass);
116119
}
117120
return modules;
118121
}
119122

123+
/**
124+
* @param loader the ClassLoader to use
125+
* @param modules list of the modules to add
126+
* @param className name of the class to instantiate
127+
*/
128+
private static void addToModulesList(ClassLoader loader, List<Module> modules, String className) {
129+
Module module = loadAndGetInstance(className, loader);
130+
if (module != null) {
131+
modules.add(module);
132+
}
133+
}
134+
120135
/**
121136
* Creates a TypeResolverBuilder that performs whitelisting.
122137
* @return a TypeResolverBuilder that performs whitelisting.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
*
2626
* <pre>
2727
* ObjectMapper mapper = new ObjectMapper();
28-
* mapper.registerModule(new WebJackson2Module());
28+
* mapper.registerModule(new WebServletJackson2Module());
2929
* </pre>
3030
*
3131
* @author Jitendra Singh
32-
* @see WebJackson2Module
32+
* @see WebServletJackson2Module
3333
* @see org.springframework.security.jackson2.SecurityJackson2Modules
3434
* @since 4.2
3535
*/

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
* <p>
3030
* <pre>
3131
* ObjectMapper mapper = new ObjectMapper();
32-
* mapper.registerModule(new WebJackson2Module());
32+
* mapper.registerModule(new WebServletJackson2Module());
3333
* </pre>
3434
*
3535
* @author Jitendra Singh
36-
* @see WebJackson2Module
36+
* @see WebServletJackson2Module
3737
* @see org.springframework.security.jackson2.SecurityJackson2Modules
3838
* @since 4.2
3939
*/

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
*
2525
* <pre>
2626
* ObjectMapper mapper = new ObjectMapper();
27-
* mapper.registerModule(new WebJackson2Module());
27+
* mapper.registerModule(new WebServletJackson2Module());
2828
* </pre>
2929
*
3030
* @author Jitendra Singh.
31-
* @see WebJackson2Module
31+
* @see WebServletJackson2Module
3232
* @see org.springframework.security.jackson2.SecurityJackson2Modules
3333
* @since 4.2
3434
*/

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
*
2424
* <pre>
2525
* ObjectMapper mapper = new ObjectMapper();
26-
* mapper.registerModule(new WebJackson2Module());
26+
* mapper.registerModule(new WebServletJackson2Module());
2727
* </pre>
2828
*
2929
* @author Jitendra Singh
30-
* @see WebJackson2Module
30+
* @see WebServletJackson2Module
3131
* @see org.springframework.security.jackson2.SecurityJackson2Modules
3232
* @since 4.2
3333
*/

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

+4-14
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,17 @@
1616

1717
package org.springframework.security.web.jackson2;
1818

19-
import javax.servlet.http.Cookie;
20-
2119
import org.springframework.security.jackson2.SecurityJackson2Modules;
22-
import org.springframework.security.web.authentication.WebAuthenticationDetails;
2320
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
2421
import org.springframework.security.web.csrf.DefaultCsrfToken;
25-
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
26-
import org.springframework.security.web.savedrequest.SavedCookie;
2722

2823
import com.fasterxml.jackson.core.Version;
29-
import com.fasterxml.jackson.databind.ObjectMapper;
3024
import com.fasterxml.jackson.databind.module.SimpleModule;
3125

3226
/**
33-
* Jackson module for spring-security-web. This module register {@link CookieMixin},
34-
* {@link DefaultCsrfTokenMixin}, {@link DefaultSavedRequestMixin} and {@link WebAuthenticationDetailsMixin}. If no
35-
* default typing enabled by default then it'll enable it because typing info is needed to properly serialize/deserialize objects.
27+
* Jackson module for spring-security-web. This module register {@link DefaultCsrfTokenMixin} and
28+
* {@link PreAuthenticatedAuthenticationTokenMixin}. If no default typing enabled by default then it'll enable
29+
* it because typing info is needed to properly serialize/deserialize objects.
3630
* In order to use this module just add this module into your ObjectMapper configuration.
3731
*
3832
* <pre>
@@ -53,12 +47,8 @@ public WebJackson2Module() {
5347

5448
@Override
5549
public void setupModule(SetupContext context) {
56-
SecurityJackson2Modules.enableDefaultTyping((ObjectMapper) context.getOwner());
57-
context.setMixInAnnotations(Cookie.class, CookieMixin.class);
58-
context.setMixInAnnotations(SavedCookie.class, SavedCookieMixin.class);
50+
SecurityJackson2Modules.enableDefaultTyping(context.getOwner());
5951
context.setMixInAnnotations(DefaultCsrfToken.class, DefaultCsrfTokenMixin.class);
60-
context.setMixInAnnotations(DefaultSavedRequest.class, DefaultSavedRequestMixin.class);
61-
context.setMixInAnnotations(WebAuthenticationDetails.class, WebAuthenticationDetailsMixin.class);
6252
context.setMixInAnnotations(PreAuthenticatedAuthenticationToken.class, PreAuthenticatedAuthenticationTokenMixin.class);
6353
}
6454
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.jackson2;
18+
19+
import javax.servlet.http.Cookie;
20+
21+
import org.springframework.security.jackson2.SecurityJackson2Modules;
22+
import org.springframework.security.web.authentication.WebAuthenticationDetails;
23+
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
24+
import org.springframework.security.web.savedrequest.SavedCookie;
25+
26+
import com.fasterxml.jackson.core.Version;
27+
import com.fasterxml.jackson.databind.module.SimpleModule;
28+
29+
/**
30+
* Jackson module for spring-security-web related to servlet. This module register {@link CookieMixin},
31+
* {@link SavedCookieMixin}, {@link DefaultSavedRequestMixin} and {@link WebAuthenticationDetailsMixin}. If no
32+
* default typing enabled by default then it'll enable it because typing info is needed to properly serialize/deserialize objects.
33+
* In order to use this module just add this module into your ObjectMapper configuration.
34+
*
35+
* <pre>
36+
* ObjectMapper mapper = new ObjectMapper();
37+
* mapper.registerModule(new WebServletJackson2Module());
38+
* </pre>
39+
* <b>Note: use {@link SecurityJackson2Modules#getModules(ClassLoader)} to get list of all security modules.</b>
40+
*
41+
* @author Boris Finkelshteyn
42+
* @see SecurityJackson2Modules
43+
* @since 5.1
44+
*/
45+
public class WebServletJackson2Module extends SimpleModule {
46+
47+
public WebServletJackson2Module() {
48+
super(WebJackson2Module.class.getName(), new Version(1, 0, 0, null, null, null));
49+
}
50+
51+
@Override
52+
public void setupModule(SetupContext context) {
53+
SecurityJackson2Modules.enableDefaultTyping(context.getOwner());
54+
context.setMixInAnnotations(Cookie.class, CookieMixin.class);
55+
context.setMixInAnnotations(SavedCookie.class, SavedCookieMixin.class);
56+
context.setMixInAnnotations(DefaultSavedRequest.class, DefaultSavedRequestMixin.class);
57+
context.setMixInAnnotations(WebAuthenticationDetails.class, WebAuthenticationDetailsMixin.class);
58+
}
59+
}

0 commit comments

Comments
 (0)