|
| 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