Add JSON Serialization
Fixes gh-3812
This commit is contained in:
committed by
Rob Winch
parent
4d02a5c0a0
commit
d77ca17e95
+11
@@ -54,6 +54,17 @@ public class WebAuthenticationDetails implements Serializable {
|
||||
this.sessionId = (session != null) ? session.getId() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor to add Jackson2 serialize/deserialize support
|
||||
*
|
||||
* @param remoteAddress remote address of current request
|
||||
* @param sessionId session id
|
||||
*/
|
||||
private WebAuthenticationDetails(final String remoteAddress, final String sessionId) {
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
// ~ Methods
|
||||
// ========================================================================================================
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Jackson deserializer for {@link Cookie}. This is needed because in most cases we don't
|
||||
* set {@link Cookie#domain} property. So when jackson deserialize that json {@link Cookie#setDomain(String)}
|
||||
* throws {@link NullPointerException}. This is registered with {@link CookieMixin} but you can also use it with
|
||||
* your own mixin.
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see CookieMixin
|
||||
* @since 4.2
|
||||
*/
|
||||
public class CookieDeserializer extends JsonDeserializer<Cookie> {
|
||||
|
||||
@Override
|
||||
public Cookie deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
|
||||
JsonNode jsonNode = mapper.readTree(jp);
|
||||
Cookie cookie = new Cookie(readJsonNode(jsonNode, "name").asText(), readJsonNode(jsonNode, "value").asText());
|
||||
cookie.setComment(readJsonNode(jsonNode, "comment").asText());
|
||||
cookie.setDomain(readJsonNode(jsonNode, "domain").asText());
|
||||
cookie.setMaxAge(readJsonNode(jsonNode, "maxAge").asInt(-1));
|
||||
cookie.setSecure(readJsonNode(jsonNode, "secure").asBoolean());
|
||||
cookie.setVersion(readJsonNode(jsonNode, "version").asInt());
|
||||
cookie.setPath(readJsonNode(jsonNode, "path").asText());
|
||||
cookie.setHttpOnly(readJsonNode(jsonNode, "httpOnly").asBoolean());
|
||||
return cookie;
|
||||
}
|
||||
|
||||
private JsonNode readJsonNode(JsonNode jsonNode, String field) {
|
||||
return jsonNode.has(field) && !(jsonNode.get(field) instanceof NullNode) ? jsonNode.get(field) : MissingNode.getInstance();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
/**
|
||||
* Mixin class to serialize/deserialize {@link javax.servlet.http.Cookie}
|
||||
*
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see WebJackson2Module
|
||||
* @see org.springframework.security.jackson2.SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
|
||||
@JsonDeserialize(using = CookieDeserializer.class)
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
public abstract class CookieMixin {
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
/**
|
||||
* Jackson mixin class to serialize/deserialize {@link org.springframework.security.web.csrf.DefaultCsrfToken}
|
||||
* serialization support.
|
||||
*
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see WebJackson2Module
|
||||
* @see org.springframework.security.jackson2.SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class DefaultCsrfTokenMixin {
|
||||
|
||||
/**
|
||||
* JsonCreator constructor needed by Jackson to create {@link org.springframework.security.web.csrf.DefaultCsrfToken}
|
||||
* object.
|
||||
*
|
||||
* @param headerName
|
||||
* @param parameterName
|
||||
* @param token
|
||||
*/
|
||||
@JsonCreator
|
||||
public DefaultCsrfTokenMixin(@JsonProperty("headerName") String headerName,
|
||||
@JsonProperty("parameterName") String parameterName, @JsonProperty("token") String token) {
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
||||
|
||||
/**
|
||||
* Jackson mixin class to serialize/deserialize {@link DefaultSavedRequest}. This mixin use
|
||||
* {@link org.springframework.security.web.savedrequest.DefaultSavedRequest.Builder} to
|
||||
* deserialized json.In order to use this mixin class you also need to register
|
||||
* {@link CookieMixin}.
|
||||
* <p>
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see WebJackson2Module
|
||||
* @see org.springframework.security.jackson2.SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
|
||||
@JsonDeserialize(builder = DefaultSavedRequest.Builder.class)
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
public abstract class DefaultSavedRequestMixin {
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
|
||||
/**
|
||||
* Jackson mixin class to serialize/deserialize {@link org.springframework.security.web.savedrequest.SavedCookie}
|
||||
* serialization support.
|
||||
*
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
*
|
||||
* @author Jitendra Singh.
|
||||
* @see WebJackson2Module
|
||||
* @see org.springframework.security.jackson2.SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY,
|
||||
getterVisibility = JsonAutoDetect.Visibility.NONE)
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public abstract class SavedCookieMixin {
|
||||
|
||||
@JsonCreator
|
||||
public SavedCookieMixin(@JsonProperty("name") String name, @JsonProperty("value") String value,
|
||||
@JsonProperty("comment") String comment, @JsonProperty("domain") String domain,
|
||||
@JsonProperty("maxAge") int maxAge, @JsonProperty("path") String path,
|
||||
@JsonProperty("secure") boolean secure, @JsonProperty("version") int version) {
|
||||
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.*;
|
||||
|
||||
/**
|
||||
* Jackson mixin class to serialize/deserialize {@link org.springframework.security.web.authentication.WebAuthenticationDetails}.
|
||||
*
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see WebJackson2Module
|
||||
* @see org.springframework.security.jackson2.SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.NONE,
|
||||
isGetterVisibility = JsonAutoDetect.Visibility.NONE, creatorVisibility = JsonAutoDetect.Visibility.ANY)
|
||||
public class WebAuthenticationDetailsMixin {
|
||||
|
||||
@JsonCreator
|
||||
WebAuthenticationDetailsMixin(@JsonProperty("remoteAddress") String remoteAddress,
|
||||
@JsonProperty("sessionId") String sessionId) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.core.Version;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
import org.springframework.security.web.csrf.DefaultCsrfToken;
|
||||
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
||||
import org.springframework.security.web.savedrequest.SavedCookie;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
/**
|
||||
* Jackson module for spring-security-web. This module register {@link CookieMixin},
|
||||
* {@link DefaultCsrfTokenMixin}, {@link DefaultSavedRequestMixin} and {@link WebAuthenticationDetailsMixin}. If no
|
||||
* default typing enabled by default then it'll enable it because typing info is needed to properly serialize/deserialize objects.
|
||||
* In order to use this module just add this module into your ObjectMapper configuration.
|
||||
*
|
||||
* <pre>
|
||||
* ObjectMapper mapper = new ObjectMapper();
|
||||
* mapper.registerModule(new WebJackson2Module());
|
||||
* </pre>
|
||||
* <b>Note: use {@link SecurityJacksonModules#getModules()} to get list of all security modules.</b>
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @see SecurityJacksonModules
|
||||
* @since 4.2
|
||||
*/
|
||||
public class WebJackson2Module extends SimpleModule {
|
||||
|
||||
public WebJackson2Module() {
|
||||
super(WebJackson2Module.class.getName(), new Version(1, 0, 0, null, null, null));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupModule(SetupContext context) {
|
||||
SecurityJacksonModules.enableDefaultTyping((ObjectMapper) context.getOwner());
|
||||
context.setMixInAnnotations(Cookie.class, CookieMixin.class);
|
||||
context.setMixInAnnotations(SavedCookie.class, SavedCookieMixin.class);
|
||||
context.setMixInAnnotations(DefaultCsrfToken.class, DefaultCsrfTokenMixin.class);
|
||||
context.setMixInAnnotations(DefaultSavedRequest.class, DefaultSavedRequestMixin.class);
|
||||
context.setMixInAnnotations(WebAuthenticationDetails.class, WebAuthenticationDetailsMixin.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/**
|
||||
* Mix-in classes to provide Jackson serialization support.
|
||||
*
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
package org.springframework.security.web.jackson2;
|
||||
+174
-30
@@ -16,11 +16,14 @@
|
||||
|
||||
package org.springframework.security.web.savedrequest;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.security.web.PortResolver;
|
||||
import org.springframework.security.web.util.UrlUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -84,13 +87,7 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
Assert.notNull(portResolver, "PortResolver required");
|
||||
|
||||
// Cookies
|
||||
Cookie[] cookies = request.getCookies();
|
||||
|
||||
if (cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
this.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
addCookies(request.getCookies());
|
||||
|
||||
// Headers
|
||||
Enumeration<String> names = request.getHeaderNames();
|
||||
@@ -110,27 +107,10 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
}
|
||||
|
||||
// Locales
|
||||
Enumeration<Locale> locales = request.getLocales();
|
||||
|
||||
while (locales.hasMoreElements()) {
|
||||
Locale locale = (Locale) locales.nextElement();
|
||||
this.addLocale(locale);
|
||||
}
|
||||
addLocales(request.getLocales());
|
||||
|
||||
// Parameters
|
||||
Map<String, String[]> parameters = request.getParameterMap();
|
||||
|
||||
for (String paramName : parameters.keySet()) {
|
||||
Object paramValues = parameters.get(paramName);
|
||||
if (paramValues instanceof String[]) {
|
||||
this.addParameter(paramName, (String[]) paramValues);
|
||||
}
|
||||
else {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("ServletRequest.getParameterMap() returned non-String array");
|
||||
}
|
||||
}
|
||||
}
|
||||
addParameters(request.getParameterMap());
|
||||
|
||||
// Primitives
|
||||
this.method = request.getMethod();
|
||||
@@ -145,9 +125,36 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
this.servletPath = request.getServletPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor invoked through Builder
|
||||
*/
|
||||
private DefaultSavedRequest(Builder builder) {
|
||||
this.contextPath = builder.contextPath;
|
||||
this.method = builder.method;
|
||||
this.pathInfo = builder.pathInfo;
|
||||
this.queryString = builder.queryString;
|
||||
this.requestURI = builder.requestURI;
|
||||
this.requestURL = builder.requestURL;
|
||||
this.scheme = builder.scheme;
|
||||
this.serverName = builder.serverName;
|
||||
this.servletPath = builder.servletPath;
|
||||
this.serverPort = builder.serverPort;
|
||||
}
|
||||
|
||||
// ~ Methods
|
||||
// ========================================================================================================
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
private void addCookies(Cookie[] cookies) {
|
||||
if (cookies != null) {
|
||||
for (Cookie cookie : cookies) {
|
||||
this.addCookie(cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addCookie(Cookie cookie) {
|
||||
cookies.add(new SavedCookie(cookie));
|
||||
}
|
||||
@@ -163,10 +170,38 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
private void addLocales(Enumeration<Locale> locales) {
|
||||
while (locales.hasMoreElements()) {
|
||||
Locale locale = locales.nextElement();
|
||||
this.addLocale(locale);
|
||||
}
|
||||
}
|
||||
|
||||
private void addLocale(Locale locale) {
|
||||
locales.add(locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
private void addParameters(Map<String, String[]> parameters) {
|
||||
if (!ObjectUtils.isEmpty(parameters)) {
|
||||
for (String paramName : parameters.keySet()) {
|
||||
Object paramValues = parameters.get(paramName);
|
||||
if (paramValues instanceof String[]) {
|
||||
this.addParameter(paramName, (String[]) paramValues);
|
||||
} else {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("ServletRequest.getParameterMap() returned non-String array");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addParameter(String name, String[] values) {
|
||||
parameters.put(name, values);
|
||||
}
|
||||
@@ -176,10 +211,9 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
* <p>
|
||||
* All URL arguments are considered but not cookies, locales, headers or parameters.
|
||||
*
|
||||
* @param request the actual request to be matched against this one
|
||||
* @param request the actual request to be matched against this one
|
||||
* @param portResolver used to obtain the server port of the request
|
||||
* @return true if the request is deemed to match this one.
|
||||
*
|
||||
*/
|
||||
public boolean doesRequestMatch(HttpServletRequest request, PortResolver portResolver) {
|
||||
|
||||
@@ -341,8 +375,7 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(log + ": arg1=" + arg1 + "; arg2=" + arg2
|
||||
+ " (property not equals)");
|
||||
@@ -355,4 +388,115 @@ public class DefaultSavedRequest implements SavedRequest {
|
||||
public String toString() {
|
||||
return "DefaultSavedRequest[" + getRedirectUrl() + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 4.2
|
||||
*/
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@JsonPOJOBuilder(withPrefix = "set")
|
||||
public static class Builder {
|
||||
|
||||
private List<SavedCookie> cookies = null;
|
||||
private List<Locale> locales = null;
|
||||
private Map<String, List<String>> headers = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
|
||||
private Map<String, String[]> parameters = new TreeMap<String, String[]>();
|
||||
private String contextPath;
|
||||
private String method;
|
||||
private String pathInfo;
|
||||
private String queryString;
|
||||
private String requestURI;
|
||||
private String requestURL;
|
||||
private String scheme;
|
||||
private String serverName;
|
||||
private String servletPath;
|
||||
private int serverPort = 80;
|
||||
|
||||
public Builder setCookies(List<SavedCookie> cookies) {
|
||||
this.cookies = cookies;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLocales(List<Locale> locales) {
|
||||
this.locales = locales;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setHeaders(Map<String, List<String>> header) {
|
||||
this.headers.putAll(header);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setParameters(Map<String, String[]> parameters) {
|
||||
this.parameters = parameters;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMethod(String method) {
|
||||
this.method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setPathInfo(String pathInfo) {
|
||||
this.pathInfo = pathInfo;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setQueryString(String queryString) {
|
||||
this.queryString = queryString;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRequestURI(String requestURI) {
|
||||
this.requestURI = requestURI;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRequestURL(String requestURL) {
|
||||
this.requestURL = requestURL;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setScheme(String scheme) {
|
||||
this.scheme = scheme;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setServerName(String serverName) {
|
||||
this.serverName = serverName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setServletPath(String servletPath) {
|
||||
this.servletPath = servletPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setServerPort(int serverPort) {
|
||||
this.serverPort = serverPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultSavedRequest build() {
|
||||
DefaultSavedRequest savedRequest = new DefaultSavedRequest(this);
|
||||
if(!ObjectUtils.isEmpty(this.cookies)) {
|
||||
for (SavedCookie cookie : this.cookies) {
|
||||
savedRequest.addCookie(cookie.getCookie());
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.locales))
|
||||
savedRequest.locales.addAll(this.locales);
|
||||
savedRequest.addParameters(this.parameters);
|
||||
|
||||
this.headers.remove(HEADER_IF_MODIFIED_SINCE);
|
||||
this.headers.remove(HEADER_IF_NONE_MATCH);
|
||||
if (!ObjectUtils.isEmpty(this.headers))
|
||||
savedRequest.headers.putAll(this.headers);
|
||||
return savedRequest;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Jitenra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public abstract class AbstractMixinTests {
|
||||
|
||||
ObjectMapper mapper;
|
||||
|
||||
protected ObjectMapper buildObjectMapper() {
|
||||
if (ObjectUtils.isEmpty(mapper)) {
|
||||
mapper = new ObjectMapper();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules());
|
||||
}
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
public class CookieMixinTests {
|
||||
|
||||
String cookieJson = "{\"@class\": \"javax.servlet.http.Cookie\", \"name\": \"demo\", \"value\": \"cookie1\"," +
|
||||
"\"comment\": null, \"maxAge\": -1, \"path\": null, \"secure\": false, \"version\": 0, \"isHttpOnly\": false, \"domain\": null}";
|
||||
|
||||
ObjectMapper buildObjectMapper() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModules(SecurityJacksonModules.getModules());
|
||||
return mapper;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeCookie() throws JsonProcessingException, JSONException {
|
||||
Cookie cookie = new Cookie("demo", "cookie1");
|
||||
String actualString = buildObjectMapper().writeValueAsString(cookie);
|
||||
JSONAssert.assertEquals(cookieJson, actualString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeCookie() throws IOException {
|
||||
Cookie cookie = buildObjectMapper().readValue(cookieJson, Cookie.class);
|
||||
assertThat(cookie).isNotNull();
|
||||
assertThat(cookie.getName()).isEqualTo("demo");
|
||||
assertThat(cookie.getDomain()).isEqualTo("");
|
||||
assertThat(cookie.isHttpOnly()).isEqualTo(false);
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.security.web.csrf.DefaultCsrfToken;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultCsrfTokenMixinTests {
|
||||
|
||||
ObjectMapper objectMapper;
|
||||
String defaultCsrfTokenJson;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModules(SecurityJacksonModules.getModules());
|
||||
defaultCsrfTokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", " +
|
||||
"\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCsrfTokenSerializedTest() throws JsonProcessingException, JSONException {
|
||||
DefaultCsrfToken token = new DefaultCsrfToken("csrf-header", "_csrf", "1");
|
||||
String serializedJson = objectMapper.writeValueAsString(token);
|
||||
JSONAssert.assertEquals(defaultCsrfTokenJson, serializedJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultCsrfTokenDeserializeTest() throws IOException {
|
||||
DefaultCsrfToken token = objectMapper.readValue(defaultCsrfTokenJson, DefaultCsrfToken.class);
|
||||
assertThat(token).isNotNull();
|
||||
assertThat(token.getHeaderName()).isEqualTo("csrf-header");
|
||||
assertThat(token.getParameterName()).isEqualTo("_csrf");
|
||||
assertThat(token.getToken()).isEqualTo("1");
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void defaultCsrfTokenDeserializeWithoutClassTest() throws IOException {
|
||||
String tokenJson = "{\"headerName\": \"csrf-header\", \"parameterName\": \"_csrf\", \"token\": \"1\"}";
|
||||
objectMapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
}
|
||||
|
||||
@Test(expected = JsonMappingException.class)
|
||||
public void defaultCsrfTokenDeserializeNullValuesTest() throws IOException {
|
||||
String tokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", \"headerName\": \"\", \"parameterName\": null, \"token\": \"1\"}";
|
||||
objectMapper.readValue(tokenJson, DefaultCsrfToken.class);
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.security.web.PortResolverImpl;
|
||||
import org.springframework.security.web.savedrequest.DefaultSavedRequest;
|
||||
import org.springframework.security.web.savedrequest.SavedCookie;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultSavedRequestMixinTests extends AbstractMixinTests {
|
||||
|
||||
String defaultSavedRequestJson = "{" +
|
||||
"\"@class\": \"org.springframework.security.web.savedrequest.DefaultSavedRequest\", \"cookies\": [\"java.util.ArrayList\", [{\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", \"name\": \"SESSION\", \"value\": \"123456789\", \"comment\": null, \"maxAge\": -1, \"path\": null, \"secure\":false, \"version\": 0, \"domain\": null}]]," +
|
||||
"\"locales\": [\"java.util.ArrayList\", [\"en\"]], \"headers\": {\"@class\": \"java.util.TreeMap\", \"x-auth-token\": [\"java.util.ArrayList\", [\"12\"]]}, \"parameters\": {\"@class\": \"java.util.TreeMap\"}," +
|
||||
"\"contextPath\": \"\", \"method\": \"\", \"pathInfo\": null, \"queryString\": null, \"requestURI\": \"\", \"requestURL\": \"http://localhost\", \"scheme\": \"http\", " +
|
||||
"\"serverName\": \"localhost\", \"servletPath\": \"\", \"serverPort\": 80"+
|
||||
"}";
|
||||
|
||||
@Test
|
||||
public void matchRequestBuildWithConstructorAndBuilder() {
|
||||
DefaultSavedRequest request = new DefaultSavedRequest.Builder()
|
||||
.setCookies(Collections.singletonList(new SavedCookie(new Cookie("SESSION", "123456789"))))
|
||||
.setHeaders(Collections.singletonMap("x-auth-token", Collections.singletonList("12")))
|
||||
.setScheme("http").setRequestURL("http://localhost").setServerName("localhost").setRequestURI("")
|
||||
.setLocales(Collections.singletonList(new Locale("en"))).setContextPath("").setMethod("")
|
||||
.setServletPath("").build();
|
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
||||
mockRequest.setCookies(new Cookie("SESSION", "123456789"));
|
||||
mockRequest.addHeader("x-auth-token", "12");
|
||||
|
||||
assert request.doesRequestMatch(mockRequest, new PortResolverImpl());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeDefaultRequestBuildWithConstructorTest() throws IOException, JSONException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setCookies(new Cookie("SESSION", "123456789"));
|
||||
request.addHeader("x-auth-token", "12");
|
||||
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(new DefaultSavedRequest(request, new PortResolverImpl()));
|
||||
JSONAssert.assertEquals(defaultSavedRequestJson, actualString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeDefaultRequestBuildWithBuilderTest() throws IOException, JSONException {
|
||||
DefaultSavedRequest request = new DefaultSavedRequest.Builder()
|
||||
.setCookies(Collections.singletonList(new SavedCookie(new Cookie("SESSION", "123456789"))))
|
||||
.setHeaders(Collections.singletonMap("x-auth-token", Collections.singletonList("12")))
|
||||
.setScheme("http").setRequestURL("http://localhost").setServerName("localhost").setRequestURI("")
|
||||
.setLocales(Collections.singletonList(new Locale("en"))).setContextPath("").setMethod("")
|
||||
.setServletPath("").build();
|
||||
String actualString = buildObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(request);
|
||||
JSONAssert.assertEquals(defaultSavedRequestJson, actualString, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeDefaultSavedRequest() throws IOException {
|
||||
DefaultSavedRequest request = (DefaultSavedRequest) buildObjectMapper().readValue(defaultSavedRequestJson, Object.class);
|
||||
assertThat(request).isNotNull();
|
||||
assertThat(request.getCookies()).hasSize(1);
|
||||
assertThat(request.getLocales()).hasSize(1).contains(new Locale("en"));
|
||||
assertThat(request.getHeaderNames()).hasSize(1).contains("x-auth-token");
|
||||
assertThat(request.getHeaderValues("x-auth-token")).hasSize(1).contains("12");
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
import org.springframework.security.web.savedrequest.SavedCookie;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jitendra Singh.
|
||||
*/
|
||||
public class SavedCookieMixinTests extends AbstractMixinTests {
|
||||
|
||||
private String expectedSavedCookieJson;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
expectedSavedCookieJson = "{\"@class\": \"org.springframework.security.web.savedrequest.SavedCookie\", " +
|
||||
"\"name\": \"session\", \"value\": \"123456\", \"comment\": null, \"domain\": null, \"maxAge\": -1, " +
|
||||
"\"path\": null, \"secure\": false, \"version\": 0}";
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void serializeWithDefaultConfigurationTest() throws JsonProcessingException, JSONException {
|
||||
SavedCookie savedCookie = new SavedCookie(new Cookie("session", "123456"));
|
||||
String actualJson = buildObjectMapper().writeValueAsString(savedCookie);
|
||||
JSONAssert.assertEquals(expectedSavedCookieJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeWithOverrideConfigurationTest() throws JsonProcessingException, JSONException {
|
||||
SavedCookie savedCookie = new SavedCookie(new Cookie("session", "123456"));
|
||||
ObjectMapper mapper = buildObjectMapper();
|
||||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PUBLIC_ONLY)
|
||||
.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
|
||||
String actualJson = mapper.writeValueAsString(savedCookie);
|
||||
JSONAssert.assertEquals(expectedSavedCookieJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serializeSavedCookieWithList() throws JsonProcessingException, JSONException {
|
||||
List<SavedCookie> savedCookies = new ArrayList<SavedCookie>();
|
||||
savedCookies.add(new SavedCookie(new Cookie("session", "123456")));
|
||||
String expectedJson = String.format("[\"java.util.ArrayList\", [%s]]", expectedSavedCookieJson);
|
||||
String actualJson = buildObjectMapper().writeValueAsString(savedCookies);
|
||||
JSONAssert.assertEquals(expectedJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeSavedCookieWithList() throws IOException, JSONException {
|
||||
String expectedJson = String.format("[\"java.util.ArrayList\", [%s]]", expectedSavedCookieJson);
|
||||
List<SavedCookie> savedCookies = (List<SavedCookie>)buildObjectMapper().readValue(expectedJson, Object.class);
|
||||
assertThat(savedCookies).isNotNull().hasSize(1);
|
||||
assertThat(savedCookies.get(0).getName()).isEqualTo("session");
|
||||
assertThat(savedCookies.get(0).getValue()).isEqualTo("123456");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeSavedCookieJsonTest() throws IOException {
|
||||
SavedCookie savedCookie = (SavedCookie) buildObjectMapper().readValue(expectedSavedCookieJson, Object.class);
|
||||
assertThat(savedCookie).isNotNull();
|
||||
assertThat(savedCookie.getName()).isEqualTo("session");
|
||||
assertThat(savedCookie.getValue()).isEqualTo("123456");
|
||||
assertThat(savedCookie.isSecure()).isEqualTo(false);
|
||||
assertThat(savedCookie.getVersion()).isEqualTo(0);
|
||||
assertThat(savedCookie.getComment()).isNull();
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.web.jackson2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.security.jackson2.SecurityJacksonModules;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Jitendra Singh
|
||||
* @since 4.2
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class WebAuthenticationDetailsMixinTests {
|
||||
|
||||
ObjectMapper mapper;
|
||||
String webAuthenticationDetailsJson = "{\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\","
|
||||
+ "\"sessionId\": \"1\", \"remoteAddress\": \"/localhost\"}";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.mapper = new ObjectMapper();
|
||||
this.mapper.registerModules(SecurityJacksonModules.getModules());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildWebAuthenticationDetailsUsingDifferentConstructors()
|
||||
throws IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setRemoteAddr("localhost");
|
||||
request.setSession(new MockHttpSession(null, "1"));
|
||||
|
||||
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
|
||||
|
||||
WebAuthenticationDetails authenticationDetails = this.mapper.readValue(webAuthenticationDetailsJson,
|
||||
WebAuthenticationDetails.class);
|
||||
assertThat(details.equals(authenticationDetails));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webAuthenticationDetailsSerializeTest()
|
||||
throws JsonProcessingException, JSONException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.setRemoteAddr("/localhost");
|
||||
request.setSession(new MockHttpSession(null, "1"));
|
||||
WebAuthenticationDetails details = new WebAuthenticationDetails(request);
|
||||
String actualJson = this.mapper.writeValueAsString(details);
|
||||
JSONAssert.assertEquals(webAuthenticationDetailsJson, actualJson, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webAuthenticationDetailsDeserializeTest()
|
||||
throws IOException, JSONException {
|
||||
WebAuthenticationDetails details = this.mapper.readValue(webAuthenticationDetailsJson,
|
||||
WebAuthenticationDetails.class);
|
||||
assertThat(details).isNotNull();
|
||||
assertThat(details.getRemoteAddress()).isEqualTo("/localhost");
|
||||
assertThat(details.getSessionId()).isEqualTo("1");
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -11,7 +11,8 @@ dependencies {
|
||||
|
||||
optional "org.springframework:spring-webmvc:$springVersion",
|
||||
"org.springframework:spring-jdbc:$springVersion",
|
||||
"org.springframework:spring-tx:$springVersion"
|
||||
"org.springframework:spring-tx:$springVersion",
|
||||
"com.fasterxml.jackson.core:jackson-databind:$jacksonDatavindVersion"
|
||||
|
||||
provided "javax.servlet:javax.servlet-api:$servletApiVersion"
|
||||
|
||||
@@ -20,7 +21,8 @@ dependencies {
|
||||
"org.slf4j:jcl-over-slf4j:$slf4jVersion",
|
||||
"org.codehaus.groovy:groovy-all:$groovyVersion",
|
||||
powerMockDependencies,
|
||||
spockDependencies
|
||||
spockDependencies,
|
||||
"org.skyscreamer:jsonassert:$jsonassertVersion"
|
||||
|
||||
testRuntime "org.hsqldb:hsqldb:$hsqlVersion"
|
||||
}
|
||||
Reference in New Issue
Block a user